mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-10 19:55:41 +03:00
lib.filesystem.packagesFromDirectoryRecursive: refactor (#359941)
No functional changes. - Centralize the logic classifying files/directories of interest, instead of being spread between `directoryEntryIsPackage` and `directoryEntryToAttrPair`. - Replace a composition of `mapAttrs'` and `filterAttrs` with `concatMapAttrs`. - Simplify future improvements, such as creating nested scopes for subdirs, or ignoring unsupported files.
This commit is contained in:
parent
e30e364900
commit
8a59b79070
1 changed files with 27 additions and 46 deletions
|
@ -18,7 +18,10 @@ let
|
||||||
;
|
;
|
||||||
|
|
||||||
inherit (lib.filesystem)
|
inherit (lib.filesystem)
|
||||||
|
pathIsDirectory
|
||||||
|
pathIsRegularFile
|
||||||
pathType
|
pathType
|
||||||
|
packagesFromDirectoryRecursive
|
||||||
;
|
;
|
||||||
|
|
||||||
inherit (lib.strings)
|
inherit (lib.strings)
|
||||||
|
@ -360,52 +363,30 @@ in
|
||||||
directory,
|
directory,
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
|
assert pathIsDirectory directory;
|
||||||
let
|
let
|
||||||
# Determine if a directory entry from `readDir` indicates a package or
|
inherit (lib.path) append;
|
||||||
# directory of packages.
|
defaultPath = append directory "package.nix";
|
||||||
directoryEntryIsPackage = basename: type:
|
|
||||||
type == "directory" || hasSuffix ".nix" basename;
|
|
||||||
|
|
||||||
# List directory entries that indicate packages in the given `path`.
|
|
||||||
packageDirectoryEntries = path:
|
|
||||||
filterAttrs directoryEntryIsPackage (readDir path);
|
|
||||||
|
|
||||||
# Transform a directory entry (a `basename` and `type` pair) into a
|
|
||||||
# package.
|
|
||||||
directoryEntryToAttrPair = subdirectory: basename: type:
|
|
||||||
let
|
|
||||||
path = subdirectory + "/${basename}";
|
|
||||||
in
|
|
||||||
if type == "regular"
|
|
||||||
then
|
|
||||||
{
|
|
||||||
name = removeSuffix ".nix" basename;
|
|
||||||
value = callPackage path { };
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if type == "directory"
|
|
||||||
then
|
|
||||||
{
|
|
||||||
name = basename;
|
|
||||||
value = packagesFromDirectory path;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw
|
|
||||||
''
|
|
||||||
lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString subdirectory}
|
|
||||||
'';
|
|
||||||
|
|
||||||
# Transform a directory into a package (if there's a `package.nix`) or
|
|
||||||
# set of packages (otherwise).
|
|
||||||
packagesFromDirectory = path:
|
|
||||||
let
|
|
||||||
defaultPackagePath = path + "/package.nix";
|
|
||||||
in
|
|
||||||
if pathExists defaultPackagePath
|
|
||||||
then callPackage defaultPackagePath { }
|
|
||||||
else mapAttrs'
|
|
||||||
(directoryEntryToAttrPair path)
|
|
||||||
(packageDirectoryEntries path);
|
|
||||||
in
|
in
|
||||||
packagesFromDirectory directory;
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue