-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.nix
39 lines (36 loc) · 1.43 KB
/
lib.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# functions to map a function over modules
# used to import all modules at once
# from hlissner
lib: rec {
mapModules = dir: func:
lib.filterAttrs (_: v: v != null) (lib.mapAttrs'
(n: v: let
path = "${toString dir}/${n}";
in
if v == "directory" && builtins.pathExists "${path}/default.nix"
then lib.nameValuePair n (func path)
else if v == "regular" && n != "default.nix" && lib.hasSuffix ".nix" n
then lib.nameValuePair (lib.removeSuffix ".nix" n) (func path)
else lib.nameValuePair "" null)
(builtins.readDir dir));
mapModules' = dir: func: builtins.attrValues (mapModules dir func);
mapModulesRec = dir: func:
lib.filterAttrs (_: v: v != null) (lib.mapAttrs'
(n: v: let
path = "${toString dir}/${n}";
in
if v == "directory"
then lib.nameValuePair n (mapModulesRec path func)
else if v == "regular" && n != "default.nix" && lib.hasSuffix ".nix" n
then lib.nameValuePair (lib.removeSuffix ".nix" n) (func path)
else lib.nameValuePair "" null)
(builtins.readDir dir));
mapModulesRec' = dir: func: let
dirs =
lib.mapAttrsToList (k: _: "${dir}/${k}")
(lib.filterAttrs (_: v: v == "directory") (builtins.readDir dir));
files = builtins.attrValues (mapModules dir lib.id);
paths = files ++ builtins.concatLists (builtins.map (d: mapModulesRec' d lib.id) dirs);
in
builtins.map func paths;
}