Skip to content

Commit

Permalink
lib.packagesFromDirectoryRecursive: default to creating new scopes wh…
Browse files Browse the repository at this point in the history
…en `newScope` is provided
  • Loading branch information
nbraud committed Dec 3, 2024
1 parent c682f05 commit ecca66e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
27 changes: 20 additions & 7 deletions lib/filesystem.nix
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ in
```
packagesFromDirectoryRecursive :: (args :: {
callPackage :: Path -> {} -> a,
newScope? :: AttrSet -> scope,
directory :: Path,
recurseIntoDirectory? :: (args -> AttrSet) -> args -> AttrSet,
...
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
=> { ... }
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ecca66e

Please sign in to comment.