summary refs log tree commit diff
path: root/lib/sources.nix
diff options
context:
space:
mode:
authorWill Fancher <elvishjerricco@gmail.com>2018-01-02 00:29:20 -0500
committerWill Fancher <elvishjerricco@gmail.com>2018-01-09 18:59:03 -0500
commit7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0 (patch)
tree34976dca4b69bc09c9330b280c92f235f41c0136 /lib/sources.nix
parent866c511a0afb01115786769f72c8ecafcbb84268 (diff)
downloadnixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar.gz
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar.bz2
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar.lz
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar.xz
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.tar.zst
nixpkgs-7e5f37d73b1d35cf6d91beec8e5729c7f72ca8e0.zip
Added `lib.cleanSourceWith` as composable version of `filterSource`
Diffstat (limited to 'lib/sources.nix')
-rw-r--r--lib/sources.nix31
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/sources.nix b/lib/sources.nix
index 0fd56d58ddd..703f5a71da6 100644
--- a/lib/sources.nix
+++ b/lib/sources.nix
@@ -26,14 +26,35 @@ rec {
     (type == "symlink" && lib.hasPrefix "result" baseName)
   );
 
-  cleanSource = builtins.filterSource cleanSourceFilter;
+  cleanSource = src: cleanSourceWith { filter = cleanSourceFilter; inherit src; };
+
+  # Like `builtins.filterSource`, except it will compose with itself,
+  # allowing you to chain multiple calls together without any
+  # intermediate copies being put in the nix store.
+  #
+  #     lib.cleanSourceWith f (lib.cleanSourceWith g ./.)     # Succeeds!
+  #     builtins.filterSource f (builtins.filterSource g ./.) # Fails!
+  cleanSourceWith = { filter, src }:
+    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;
+    in {
+      inherit origSrc;
+      filter = filter';
+      outPath = builtins.filterSource filter' origSrc;
+      _isLibCleanSourceWith = true;
+    };
 
   # Filter sources by a list of regular expressions.
   #
   # E.g. `src = sourceByRegex ./my-subproject [".*\.py$" "^database.sql$"]`
-  sourceByRegex = src: regexes: builtins.filterSource (path: type:
-    let relPath = lib.removePrefix (toString src + "/") (toString path);
-    in lib.any (re: builtins.match re relPath != null) regexes) src;
+  sourceByRegex = src: regexes: cleanSourceWith {
+    filter = (path: type:
+      let relPath = lib.removePrefix (toString src + "/") (toString path);
+      in lib.any (re: builtins.match re relPath != null) regexes);
+    inherit src;
+  };
 
   # Get all files ending with the specified suffices from the given
   # directory or its descendants.  E.g. `sourceFilesBySuffices ./dir
@@ -42,7 +63,7 @@ rec {
     let filter = name: type:
       let base = baseNameOf (toString name);
       in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
-    in builtins.filterSource filter path;
+    in cleanSourceWith { inherit filter; src = path; };
 
 
   # Get the commit id of a git repo