summary refs log tree commit diff
path: root/lib/fileset
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-04 23:19:56 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-10-04 23:21:45 +0200
commit2541635c1377528f7503f65537fbf96d96aa9d22 (patch)
tree03366f1afb7ce80ba69ec6e57de2b97cfa80f5b0 /lib/fileset
parent1486c62d2c0b3b06e9d9778bca88769e5ac11131 (diff)
downloadnixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar.gz
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar.bz2
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar.lz
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar.xz
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.tar.zst
nixpkgs-2541635c1377528f7503f65537fbf96d96aa9d22.zip
lib.fileset: Refactor for performance and future re-use
Diffstat (limited to 'lib/fileset')
-rw-r--r--lib/fileset/internal.nix22
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/fileset/internal.nix b/lib/fileset/internal.nix
index d18e37e9d6e..6d8165691e1 100644
--- a/lib/fileset/internal.nix
+++ b/lib/fileset/internal.nix
@@ -461,6 +461,22 @@ rec {
     else
       nonEmpty;
 
+  # Transforms the filesetTree of a file set to a shorter base path, e.g.
+  # _shortenTreeBase [ "foo" ] (_create /foo/bar null)
+  # => { bar = null; }
+  _shortenTreeBase = targetBaseComponents: fileset:
+    let
+      recurse = index:
+        # If we haven't reached the required depth yet
+        if index < length fileset._internalBaseComponents then
+          # Create an attribute set and recurse as the value, this can be lazily evaluated this way
+          { ${elemAt fileset._internalBaseComponents index} = recurse (index + 1); }
+        else
+          # Otherwise we reached the appropriate depth, here's the original tree
+          fileset._internalTree;
+    in
+    recurse (length targetBaseComponents);
+
   # Computes the union of a list of filesets.
   # The filesets must already be coerced and validated to be in the same filesystem root
   # Type: [ Fileset ] -> Fileset
@@ -497,11 +513,7 @@ rec {
       # So the tree under `/foo/bar` gets nested under `{ bar = ...; ... }`,
       # while the tree under `/foo/baz` gets nested under `{ baz = ...; ... }`
       # Therefore allowing combined operations over them.
-      trees = map (fileset:
-        setAttrByPath
-          (drop (length commonBaseComponents) fileset._internalBaseComponents)
-          fileset._internalTree
-        ) filesetsWithBase;
+      trees = map (_shortenTreeBase commonBaseComponents) filesetsWithBase;
 
       # Folds all trees together into a single one using _unionTree
       # We do not use a fold here because it would cause a thunk build-up