Skip to content

Commit 7746ea9

Browse files
authored
Revert "lib.filesystem.packagesFromDirectoryRecursive: refactor" (#360996)
2 parents f32579d + 940db57 commit 7746ea9

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

lib/filesystem.nix

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

2020
inherit (lib.filesystem)
21-
pathIsDirectory
22-
pathIsRegularFile
2321
pathType
24-
packagesFromDirectoryRecursive
2522
;
2623

2724
inherit (lib.strings)
@@ -363,30 +360,52 @@ in
363360
directory,
364361
...
365362
}:
366-
assert pathIsDirectory directory;
367363
let
368-
inherit (lib.path) append;
369-
defaultPath = append directory "package.nix";
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);
370409
in
371-
if pathIsRegularFile defaultPath then
372-
# if `${directory}/package.nix` exists, call it directly
373-
callPackage defaultPath {}
374-
else lib.concatMapAttrs (name: type:
375-
# otherwise, for each directory entry
376-
let path = append directory name; in
377-
if type == "directory" then {
378-
# recurse into directories
379-
"${name}" = packagesFromDirectoryRecursive {
380-
inherit callPackage;
381-
directory = path;
382-
};
383-
} else if type == "regular" && hasSuffix ".nix" name then {
384-
# call .nix files
385-
"${lib.removeSuffix ".nix" name}" = callPackage path {};
386-
} else if type == "regular" then {
387-
# ignore non-nix files
388-
} else throw ''
389-
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path}
390-
''
391-
) (builtins.readDir directory);
410+
packagesFromDirectory directory;
392411
}

0 commit comments

Comments
 (0)