2022-01-11 18:44:40 +01:00
|
|
|
{ lib, ... }:
|
|
|
|
|
|
|
|
let
|
2022-09-05 18:16:09 +02:00
|
|
|
inherit (builtins) attrValues readDir pathExists;
|
|
|
|
inherit (lib) id filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix mapAttrs' trace fix fold isAttrs;
|
2022-01-11 18:44:40 +01:00
|
|
|
in rec {
|
|
|
|
mapModules' = dir: fn: dirfn:
|
2022-09-05 18:16:09 +02:00
|
|
|
filterAttrs
|
|
|
|
(name: type: type != null && !(hasPrefix "_" name))
|
|
|
|
(mapAttrs'
|
|
|
|
(name: type:
|
|
|
|
let path = "${toString dir}/${name}"; in
|
|
|
|
if type == "directory"
|
|
|
|
then nameValuePair name (dirfn path)
|
|
|
|
else if
|
|
|
|
type == "regular" &&
|
|
|
|
name != "default.nix" &&
|
|
|
|
hasSuffix ".nix" name
|
|
|
|
then nameValuePair (removeSuffix ".nix" name) (fn path)
|
|
|
|
else nameValuePair "" null
|
|
|
|
)
|
|
|
|
(readDir dir));
|
2022-01-11 18:44:40 +01:00
|
|
|
|
2022-09-05 18:16:09 +02:00
|
|
|
mapModules = dir: fn: mapModules' dir fn (path: if pathExists "${path}/default.nix" then fn path else null);
|
2022-01-11 18:44:40 +01:00
|
|
|
mapModulesRec = dir: fn: mapModules' dir fn (path: mapModulesRec path fn);
|
2022-09-05 18:16:09 +02:00
|
|
|
mapModulesRec' = dir: fn: fix (f: attrs: fold (x: xs: (if isAttrs x then f x else [x]) ++ xs) [] (attrValues attrs)) (mapModulesRec dir fn);
|
2022-01-11 18:44:40 +01:00
|
|
|
}
|