diff options
Diffstat (limited to 'utils/default.nix')
-rw-r--r-- | utils/default.nix | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/utils/default.nix b/utils/default.nix new file mode 100644 index 00000000..d00357af --- /dev/null +++ b/utils/default.nix | |||
@@ -0,0 +1,57 @@ | |||
1 | { lib }: | ||
2 | rec { | ||
3 | inherit (builtins) readDir; | ||
4 | inherit (lib) filterAttrs hasSuffix removeSuffix mapAttrs' nameValuePair isFunction functionArgs setFunctionArgs id; | ||
5 | mapFilterAttrs = seive: f: attrs: filterAttrs seive (mapAttrs' f attrs); | ||
6 | recImport = { dir, _import ? name: _base: import "${toString dir}/${name}" }: | ||
7 | mapFilterAttrs | ||
8 | (_: v: v != null) | ||
9 | (n: v: | ||
10 | if n != "default.nix" && hasSuffix ".nix" n && v == "regular" | ||
11 | then | ||
12 | let name = removeSuffix ".nix" n; in nameValuePair (name) (_import n name) | ||
13 | else | ||
14 | if v == "directory" | ||
15 | then | ||
16 | nameValuePair n (_import n n) | ||
17 | else | ||
18 | nameValuePair ("") (null)) | ||
19 | (readDir dir); | ||
20 | |||
21 | types.attrNameSet = attr: | ||
22 | let | ||
23 | elemType = lib.types.enum (builtins.attrNames attr); | ||
24 | in lib.types.mkOptionType rec { | ||
25 | name = "attrNameSet"; | ||
26 | description = "set of names taken from an attribute set"; | ||
27 | check = lib.isList; | ||
28 | emptyValue = { value = {}; }; | ||
29 | getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]); | ||
30 | getSubModules = elemType.getSubModules; | ||
31 | substSubModules = m: lib.types.listOf (elemType.substSubModules m); | ||
32 | functor = (lib.types.defaultFunctor name) // { wrapped = elemType; }; | ||
33 | merge = loc: defs: map (x: x.value) (lib.lists.filter (x: x ? value) (lib.lists.unique (lib.lists.concatLists (lib.lists.imap1 (n: def: | ||
34 | lib.lists.imap1 (m: def': | ||
35 | (lib.modules.mergeDefinitions | ||
36 | (loc ++ ["[definition ${toString n}-entry ${toString m}]"]) | ||
37 | elemType | ||
38 | [{ inherit (def) file; value = def'; }] | ||
39 | ).optionalValue | ||
40 | ) def.value | ||
41 | ) defs)))); | ||
42 | }; | ||
43 | |||
44 | overrideModuleArgs = | ||
45 | let | ||
46 | overrideModuleArgs = module: appOverride: if isFunction module then overrideModuleArgs' module appOverride else module; | ||
47 | overrideModuleArgs' = module: appOverride: setFunctionArgs (inputs: overrideModuleArgs (module (appOverride inputs)) id) (functionArgs module); | ||
48 | in overrideModuleArgs; | ||
49 | |||
50 | overrideModuleOutput = | ||
51 | let | ||
52 | overrideModuleOutput = module: appOverride: if isFunction module then overrideModuleOutput' module appOverride else appOverride module; | ||
53 | overrideModuleOutput' = module: appOverride: setFunctionArgs (inputs: overrideModuleOutput (module inputs) appOverride) (functionArgs module); | ||
54 | in overrideModuleOutput; | ||
55 | |||
56 | overrideModule = module: appInput: appOutput: overrideModuleOutput (overrideModuleArgs module appInput) appOutput; | ||
57 | } | ||