diff --git a/lib/filesystem.nix b/lib/filesystem.nix index 444e3d0c4cb8ac..49399d92f3069f 100644 --- a/lib/filesystem.nix +++ b/lib/filesystem.nix @@ -314,6 +314,7 @@ in ``` packagesFromDirectoryRecursive :: (args :: { callPackage :: Path -> {} -> a, + newScope? :: AttrSet -> scope, directory :: Path, recurseIntoDirectory? :: (args -> AttrSet) -> args -> AttrSet, ... @@ -326,6 +327,12 @@ in : The function used to convert a Nix file's path into a leaf of the attribute set. It is typically the `callPackage` function, taken from either `pkgs` or a new scope corresponding to the `directory`. + `newScope` + : If present, this function is used by the default `recurseIntoDirectory` to generate a new scope. + The arguments are updated with the scope's `callPackage` and `newScope` functions, so packages can require + anything in their scope, or in an ancestor of their scope. + This argument has no effect when `recurseIntoDirectory` is provided. + `directory` : The directory to read package files from. @@ -355,12 +362,6 @@ in ```nix packagesFromDirectoryRecursive { inherit (pkgs) callPackage newScope; - recurseIntoDirectory = f: { newScope, ... }@args: - lib.recurseIntoAttrset (lib.makeScope newScope (self: - f (args // { - inherit (self) callPackage newScope; - }) - )); directory = ./my-packages; } => { ... } @@ -387,7 +388,19 @@ in { callPackage, directory, - recurseIntoDirectory ? lib.id, + recurseIntoDirectory ? + # recurseIntoDirectory can modify the function used when processing directory entries; see nixdoc above + if args ? newScope then + # when `newScope` is provided, create a new scope when recursing into a directory (incl. the top one) + processDir: { newScope, ... }@args: + lib.recurseIntoAttrs (lib.makeScope newScope (self: + processDir (args // { + inherit (self) callPackage newScope; + }) + )) + else + # otherwise, no modification is necessary + lib.id, ... }@args: let diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 0c30b2874a86f9..d9c45362728854 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -2529,12 +2529,6 @@ runTests { in { expr = lib.filterAttrsRecursive (name: value: !lib.elem name [ "callPackage" "newScope" "overrideScope" "packages" ]) (packagesFromDirectoryRecursive { inherit (emptyScope) callPackage newScope; - recurseIntoDirectory = f: { newScope, ... }@args: - recurseIntoAttrs (makeScope newScope (self: - f (args // { - inherit (self) callPackage newScope; - }) - )); directory = ./packages-from-directory/scope; }); expected = lib.recurseIntoAttrs {