Skip to content

Commit 1db8650

Browse files
lib.packagesFromDirectoryRecursive: refactor, again
This is a second take on #359941, which was reverted by 940db57. Co-authored-by: Silvan Mosberger <[email protected]>
1 parent a75d28a commit 1db8650

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

lib/filesystem.nix

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ let
1818
;
1919

2020
inherit (lib.filesystem)
21+
pathIsDirectory
22+
pathIsRegularFile
2123
pathType
24+
packagesFromDirectoryRecursive
2225
;
2326

2427
inherit (lib.strings)
@@ -361,51 +364,28 @@ in
361364
...
362365
}:
363366
let
364-
# Determine if a directory entry from `readDir` indicates a package or
365-
# directory of packages.
366-
directoryEntryIsPackage = basename: type:
367-
type == "directory" || hasSuffix ".nix" basename;
368-
369-
# List directory entries that indicate packages in the given `path`.
370-
packageDirectoryEntries = path:
371-
filterAttrs directoryEntryIsPackage (readDir path);
372-
373-
# Transform a directory entry (a `basename` and `type` pair) into a
374-
# package.
375-
directoryEntryToAttrPair = subdirectory: basename: type:
376-
let
377-
path = subdirectory + "/${basename}";
378-
in
379-
if type == "regular"
380-
then
381-
{
382-
name = removeSuffix ".nix" basename;
383-
value = callPackage path { };
384-
}
385-
else
386-
if type == "directory"
387-
then
388-
{
389-
name = basename;
390-
value = packagesFromDirectory path;
391-
}
392-
else
393-
throw
394-
''
395-
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString subdirectory}
396-
'';
397-
398-
# Transform a directory into a package (if there's a `package.nix`) or
399-
# set of packages (otherwise).
400-
packagesFromDirectory = path:
401-
let
402-
defaultPackagePath = path + "/package.nix";
403-
in
404-
if pathExists defaultPackagePath
405-
then callPackage defaultPackagePath { }
406-
else mapAttrs'
407-
(directoryEntryToAttrPair path)
408-
(packageDirectoryEntries path);
367+
inherit (lib.path) append;
368+
defaultPath = append directory "package.nix";
409369
in
410-
packagesFromDirectory directory;
370+
if pathExists defaultPath then
371+
# if `${directory}/package.nix` exists, call it directly
372+
callPackage defaultPath {}
373+
else lib.concatMapAttrs (name: type:
374+
# otherwise, for each directory entry
375+
let path = append directory name; in
376+
if type == "directory" then {
377+
# recurse into directories
378+
"${name}" = packagesFromDirectoryRecursive {
379+
inherit callPackage;
380+
directory = path;
381+
};
382+
} else if type == "regular" && hasSuffix ".nix" name then {
383+
# call .nix files
384+
"${lib.removeSuffix ".nix" name}" = callPackage path {};
385+
} else if type == "regular" then {
386+
# ignore non-nix files
387+
} else throw ''
388+
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
389+
''
390+
) (builtins.readDir directory);
411391
}

0 commit comments

Comments
 (0)