/
hoistLists.nix
40 lines (34 loc) · 1.02 KB
/
hoistLists.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
40
{ lib, super }:
from: to:
# Example from / to
# - Lifting `imports` from: _imports, to: imports
#
# Note:
# underscore used as mere convention to signalling to the user the "private"
# nature, they won't be part of the final view presented to the user
let
inherit (builtins)
removeAttrs
;
inherit (lib)
catAttrs
concatLists
;
inherit (super.utils)
concatMapAttrsWith
;
# merge attributes shallowly, but concat values of a specific key into a list in that key
# Type: ((key : String) -> { ... } -> { ... }) -> { ${key} : [ a ], ... }
mergeAttrsButConcatOn = key: x: y:
x // y // {
${key} = concatLists (catAttrs key [ x y ]);
};
in
cursor:
let toplevel = cursor == [ ]; in
concatMapAttrsWith (mergeAttrsButConcatOn (if toplevel then to else from))
(file: value: if ! value ? ${from} then { ${file} = value; } else {
${file} = removeAttrs value [ from ];
# top level ${from} declarations are omitted from merging
${if toplevel then to else from} = value.${from};
})