feat(lib): add mapModules to map func over modules
This commit is contained in:
parent
b81735b885
commit
19fd153a6d
1 changed files with 30 additions and 0 deletions
|
@ -13,4 +13,34 @@ lib.makeExtensible (self: rec {
|
||||||
rev = input.shortRev or "dirty";
|
rev = input.shortRev or "dirty";
|
||||||
in
|
in
|
||||||
"unstable-${date}_${rev}";
|
"unstable-${date}_${rev}";
|
||||||
|
|
||||||
|
mapModules = f: dir:
|
||||||
|
let
|
||||||
|
filter = name: type:
|
||||||
|
let
|
||||||
|
isPublic = !(lib.hasPrefix "_" name);
|
||||||
|
isSomething = type != null;
|
||||||
|
isModule =
|
||||||
|
let
|
||||||
|
path = "${toString dir}/${name}";
|
||||||
|
isDefault = type == "directory" && builtins.pathExists "${path}/default.nix";
|
||||||
|
isFile = type == "regular" && lib.hasSuffix ".nix" name && name != "default.nix";
|
||||||
|
in
|
||||||
|
isDefault || isFile;
|
||||||
|
in
|
||||||
|
isPublic && isSomething && isModule;
|
||||||
|
|
||||||
|
mkModule = name: _:
|
||||||
|
let
|
||||||
|
path = "${toString dir}/${name}";
|
||||||
|
normalizedName =
|
||||||
|
if name == "default.nix"
|
||||||
|
then name
|
||||||
|
else lib.removeSuffix ".nix" name;
|
||||||
|
in
|
||||||
|
lib.nameValuePair normalizedName (f path);
|
||||||
|
in
|
||||||
|
lib.mapAttrs' mkModule (lib.filterAttrs filter (builtins.readDir dir));
|
||||||
|
|
||||||
|
mapModules' = f: dir: lib.attrValues (mapModules f dir);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue