diff options
author | Alyssa Ross <hi@alyssa.is> | 2021-06-30 13:44:37 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2021-06-30 13:44:37 +0000 |
commit | 16f676d6bbd4f930b0017ccb20cebe5a29e17545 (patch) | |
tree | 0d554b4f16bdef502035b743457fc29ffcc5e7f7 /nixpkgs/lib | |
parent | e7caacc4c122b7641bb66a4c2595289347a4996c (diff) | |
parent | 3a8d7958a610cd3fec3a6f424480f91a1b259185 (diff) | |
download | nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar.gz nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar.bz2 nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar.lz nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar.xz nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.tar.zst nixlib-16f676d6bbd4f930b0017ccb20cebe5a29e17545.zip |
Merge commit '3a8d7958a610cd3fec3a6f424480f91a1b259185'
Diffstat (limited to 'nixpkgs/lib')
-rw-r--r-- | nixpkgs/lib/default.nix | 4 | ||||
-rw-r--r-- | nixpkgs/lib/modules.nix | 4 | ||||
-rw-r--r-- | nixpkgs/lib/sources.nix | 117 | ||||
-rw-r--r-- | nixpkgs/lib/systems/doubles.nix | 2 | ||||
-rw-r--r-- | nixpkgs/lib/tests/release.nix | 4 | ||||
-rwxr-xr-x | nixpkgs/lib/tests/sources.sh | 59 |
6 files changed, 166 insertions, 24 deletions
diff --git a/nixpkgs/lib/default.nix b/nixpkgs/lib/default.nix index ccae0bbc3ab4..ccfee2ebe303 100644 --- a/nixpkgs/lib/default.nix +++ b/nixpkgs/lib/default.nix @@ -115,8 +115,8 @@ let mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions pushDownProperties dischargeProperties filterOverrides sortProperties fixupOptionType mkIf mkAssert mkMerge mkOverride - mkOptionDefault mkDefault mkForce mkVMOverride mkStrict - mkFixStrictness mkOrder mkBefore mkAfter mkAliasDefinitions + mkOptionDefault mkDefault mkForce mkVMOverride + mkOrder mkBefore mkAfter mkAliasDefinitions mkAliasAndWrapDefinitions fixMergeModules mkRemovedOptionModule mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule mkAliasOptionModule doRename; diff --git a/nixpkgs/lib/modules.nix b/nixpkgs/lib/modules.nix index 99b9a8a31eac..58c6cda58e49 100644 --- a/nixpkgs/lib/modules.nix +++ b/nixpkgs/lib/modules.nix @@ -713,10 +713,6 @@ rec { mkForce = mkOverride 50; mkVMOverride = mkOverride 10; # used by ‘nixos-rebuild build-vm’ - mkStrict = builtins.trace "`mkStrict' is obsolete; use `mkOverride 0' instead." (mkOverride 0); - - mkFixStrictness = id; # obsolete, no-op - mkOrder = priority: content: { _type = "order"; inherit priority content; diff --git a/nixpkgs/lib/sources.nix b/nixpkgs/lib/sources.nix index 1a821f55056b..407829b547b0 100644 --- a/nixpkgs/lib/sources.nix +++ b/nixpkgs/lib/sources.nix @@ -1,6 +1,7 @@ # Functions for copying sources to the Nix store. { lib }: +# Tested in lib/tests/sources.sh let inherit (builtins) hasContext @@ -11,14 +12,13 @@ let tryEval ; inherit (lib) + boolToString filter getAttr isString pathExists readFile ; -in -rec { # Returns the type of a path: regular (for file), symlink, or directory pathType = p: getAttr (baseNameOf p) (readDir (dirOf p)); @@ -84,18 +84,36 @@ rec { # cleanSourceWith = { filter ? _path: _type: true, src, name ? null }: let - isFiltered = src ? _isLibCleanSourceWith; - origSrc = if isFiltered then src.origSrc else src; - filter' = if isFiltered then name: type: filter name type && src.filter name type else filter; - name' = if name != null then name else if isFiltered then src.name else "source"; - in { - inherit origSrc; - filter = filter'; - outPath = builtins.path { filter = filter'; path = origSrc; name = name'; }; - _isLibCleanSourceWith = true; - name = name'; + orig = toSourceAttributes src; + in fromSourceAttributes { + inherit (orig) origSrc; + filter = path: type: filter path type && orig.filter path type; + name = if name != null then name else orig.name; }; + /* + Add logging to a source, for troubleshooting the filtering behavior. + Type: + sources.trace :: sourceLike -> Source + */ + trace = + # Source to debug. The returned source will behave like this source, but also log its filter invocations. + src: + let + attrs = toSourceAttributes src; + in + fromSourceAttributes ( + attrs // { + filter = path: type: + let + r = attrs.filter path type; + in + builtins.trace "${attrs.name}.filter ${path} = ${boolToString r}" r; + } + ) // { + satisfiesSubpathInvariant = src ? satisfiesSubpathInvariant && src.satisfiesSubpathInvariant; + }; + # Filter sources by a list of regular expressions. # # E.g. `src = sourceByRegex ./my-subproject [".*\.py$" "^database.sql$"]` @@ -110,14 +128,26 @@ rec { inherit src; }; - # Get all files ending with the specified suffices from the given - # directory or its descendants. E.g. `sourceFilesBySuffices ./dir - # [".xml" ".c"]'. - sourceFilesBySuffices = path: exts: + /* + Get all files ending with the specified suffices from the given + source directory or its descendants, omitting files that do not match + any suffix. The result of the example below will include files like + `./dir/module.c` and `./dir/subdir/doc.xml` if present. + + Type: sourceLike -> [String] -> Source + + Example: + sourceFilesBySuffices ./. [ ".xml" ".c" ] + */ + sourceFilesBySuffices = + # Path or source containing the files to be returned + src: + # A list of file suffix strings + exts: let filter = name: type: let base = baseNameOf (toString name); in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts; - in cleanSourceWith { inherit filter; src = path; }; + in cleanSourceWith { inherit filter src; }; pathIsGitRepo = path: (tryEval (commitIdFromGitRepo path)).success; @@ -177,4 +207,57 @@ rec { pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir); canCleanSource = src: src ? _isLibCleanSourceWith || !(pathHasContext (toString src)); + + # -------------------------------------------------------------------------- # + # Internal functions + # + + # toSourceAttributes : sourceLike -> SourceAttrs + # + # Convert any source-like object into a simple, singular representation. + # We don't expose this representation in order to avoid having a fifth path- + # like class of objects in the wild. + # (Existing ones being: paths, strings, sources and x//{outPath}) + # So instead of exposing internals, we build a library of combinator functions. + toSourceAttributes = src: + let + isFiltered = src ? _isLibCleanSourceWith; + in + { + # The original path + origSrc = if isFiltered then src.origSrc else src; + filter = if isFiltered then src.filter else _: _: true; + name = if isFiltered then src.name else "source"; + }; + + # fromSourceAttributes : SourceAttrs -> Source + # + # Inverse of toSourceAttributes for Source objects. + fromSourceAttributes = { origSrc, filter, name }: + { + _isLibCleanSourceWith = true; + inherit origSrc filter name; + outPath = builtins.path { inherit filter name; path = origSrc; }; + }; + +in { + inherit + pathType + pathIsDirectory + pathIsRegularFile + + pathIsGitRepo + commitIdFromGitRepo + + cleanSource + cleanSourceWith + cleanSourceFilter + pathHasContext + canCleanSource + + sourceByRegex + sourceFilesBySuffices + + trace + ; } diff --git a/nixpkgs/lib/systems/doubles.nix b/nixpkgs/lib/systems/doubles.nix index c6d90ba9850d..61ba7dad7cc7 100644 --- a/nixpkgs/lib/systems/doubles.nix +++ b/nixpkgs/lib/systems/doubles.nix @@ -96,5 +96,5 @@ in { embedded = filterDoubles predicates.isNone; - mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "armv7a-linux" "aarch64-linux" "powerpc64-linux" "powerpc64le-linux" "aarch64-darwin"]; + mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "armv7a-linux" "aarch64-linux" "powerpc64-linux" "powerpc64le-linux" "aarch64-darwin" "riscv64-linux"]; } diff --git a/nixpkgs/lib/tests/release.nix b/nixpkgs/lib/tests/release.nix index 800d8a65c14f..c3b05251f709 100644 --- a/nixpkgs/lib/tests/release.nix +++ b/nixpkgs/lib/tests/release.nix @@ -26,7 +26,11 @@ pkgs.runCommandNoCC "nixpkgs-lib-tests" { nix-store --init cp -r ${../.} lib + echo "Running lib/tests/modules.sh" bash lib/tests/modules.sh + echo "Running lib/tests/sources.sh" + TEST_LIB=$PWD/lib bash lib/tests/sources.sh + touch $out '' diff --git a/nixpkgs/lib/tests/sources.sh b/nixpkgs/lib/tests/sources.sh new file mode 100755 index 000000000000..71fee719cb21 --- /dev/null +++ b/nixpkgs/lib/tests/sources.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Use +# || die +die() { + echo >&2 "test case failed: " "$@" + exit 1 +} + +if test -n "${TEST_LIB:-}"; then + export NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")" +else + export NIX_PATH=nixpkgs="$(cd $(dirname ${BASH_SOURCE[0]})/../..; pwd)" +fi + +work="$(mktemp -d)" +clean_up() { + rm -rf "$work" +} +trap clean_up EXIT +cd $work + +touch {README.md,module.o,foo.bar} + +# nix-instantiate doesn't write out the source, only computing the hash, so +# this uses the experimental nix command instead. + +dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ + cleanSource ./. +}")')" +(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +. +./foo.bar +./README.md +EOF +) || die "cleanSource 1" + + +dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ + cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } +}")')" +(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +. +./module.o +./README.md +EOF +) || die "cleanSourceWith 1" + +dir="$(nix eval --raw '(with import <nixpkgs/lib>; "${ + cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } +}")')" +(cd $dir; find) | sort -f | diff -U10 - <(cat <<EOF +. +./README.md +EOF +) || die "cleanSourceWith + cleanSource" + +echo >&2 tests ok |