Skip to content

Commit

Permalink
lib.packagesFromDirectoryRecursive: use explicit recursion, support n…
Browse files Browse the repository at this point in the history
…ested scopes (WiP)
  • Loading branch information
nbraud committed Dec 1, 2024
1 parent 5a3902d commit 259a007
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions lib/filesystem.nix
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,16 @@ in
}
=> { ... }
lib.makeScope pkgs.newScope (
self: packagesFromDirectoryRecursive {
callPackage = self.callPackage;
directory = ./my-packages;
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 @@ -361,32 +367,38 @@ in
{
callPackage,
directory,
recurseIntoDirectory ? lib.id,
...
}:
assert pathIsDirectory directory;
}@args:
if !pathIsDirectory directory then
throw "path ${toString directory} not a directory"
else
let
inherit (lib.path) append;
defaultPath = append directory "package.nix";
in
if pathIsRegularFile defaultPath then
# if `${directory}/package.nix` exists, call it directly
callPackage defaultPath {}
else lib.concatMapAttrs (name: type:
# otherwise, for each directory entry
let path = append directory name; in
if type == "directory" then {
# recurse into directories
"${name}" = packagesFromDirectoryRecursive {
inherit callPackage;
directory = path;
};
} else if type == "regular" && hasSuffix ".nix" name then {
# call .nix files
"${lib.removeSuffix ".nix" name}" = callPackage path {};
} else if type == "regular" then {
# ignore non-nix files
} else throw ''
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
''
) (builtins.readDir directory);
else let
f = { callPackage, ... }@newArgs:
lib.concatMapAttrs (name: type:
# otherwise, for each directory entry
let path = append directory name; in
if type == "directory" then {
# recurse into directories
"${name}" = packagesFromDirectoryRecursive (newArgs // {
directory = path;
});
} else if type == "regular" && hasSuffix ".nix" name then {
# call .nix files
"${lib.removeSuffix ".nix" name}" = callPackage path {};
} else if type == "regular" then {
# ignore non-nix files
} else throw ''
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
''
) (builtins.readDir directory);
in
recurseIntoDirectory f args;
}

0 comments on commit 259a007

Please sign in to comment.