-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathkdl.nix
More file actions
224 lines (205 loc) · 5.37 KB
/
kdl.nix
File metadata and controls
224 lines (205 loc) · 5.37 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{ lib, ... }:
let
fold-args =
lib.foldl
(
self: arg:
if lib.isAttrs arg then
self // { properties = self.properties // arg; }
else
self // { arguments = self.arguments ++ [ arg ]; }
)
{
arguments = [ ];
properties = { };
};
node = name: args: children: {
inherit name;
inherit (fold-args (lib.toList args)) arguments properties;
inherit children;
};
plain = name: node name [ ];
leaf = name: args: node name args [ ];
magic-leaf = node-name: {
${node-name} = [ ];
__functor = self: arg: {
inherit (self) __functor;
${node-name} = self.${node-name} ++ lib.toList arg;
};
};
flag = name: node name [ ] [ ];
serialize.string = lib.flip lib.pipe [
(lib.escape [
"\\"
"\""
])
# including newlines will cause the serialized output to contain additional indentation
# so we escape them
(lib.replaceStrings [ "\n" ] [ "\\n" ])
(v: "\"${v}\"")
];
serialize.path = serialize.string;
serialize.int = toString;
serialize.float = toString;
serialize.bool = v: if v then "true" else "false";
serialize.null = lib.const "null";
serialize.value = v: serialize.${builtins.typeOf v} v;
# this is not a complete list of valid identifiers
# but it is good enough for niri
# if this rejects a valid ident, literally nothing bad happens
# essentially, this regex boils down to any sequence of letters, numbers or +/-
# but not something that looks like a number (e.g. 0, -4, +12)
bare-ident = "[A-Za-z][A-Za-z0-9+-]*|[+-]|[+-][A-Za-z+-][A-Za-z0-9+-]*";
serialize.ident = v: if lib.strings.match bare-ident v != null then v else serialize.string v;
serialize.prop =
{
name,
value,
}:
"${serialize.ident name}=${serialize.value value}";
single-indent = " ";
should-collapse =
children:
let
length = lib.length children;
in
length == 0 || (length == 1 && should-collapse (lib.head children).children);
serialize.node = serialize.node-with "";
serialize.node-with =
indent:
{
name,
arguments,
properties,
children,
}:
indent
+ lib.concatStringsSep " " (
lib.flatten [
(serialize.ident name)
(map serialize.value arguments)
(map serialize.prop (lib.attrsToList properties))
(
if lib.length children == 0 then
[ ]
else if should-collapse children then
"{ ${serialize.nodes children}; }"
else
"{\n${serialize.nodes-with (indent + single-indent) children}\n${indent}}"
)
]
);
serialize.nodes = serialize.nodes-with "";
serialize.nodes-with =
indent:
lib.flip lib.pipe [
(map (serialize.node-with indent))
(lib.concatStringsSep "\n")
];
kdl-value = lib.types.nullOr (
lib.types.oneOf [
lib.types.str
lib.types.int
lib.types.float
lib.types.bool
]
);
kdl-node = lib.types.submodule {
options.name = lib.mkOption {
type = lib.types.str;
};
options.arguments = lib.mkOption {
type = lib.types.listOf kdl-value;
default = [ ];
};
options.properties = lib.mkOption {
type = lib.types.attrsOf kdl-value;
default = { };
};
options.children = lib.mkOption {
type = kdl-document;
default = [ ];
};
};
kdl-leaf = lib.mkOptionType {
name = "kdl-leaf";
description = "kdl leaf";
descriptionClass = "noun";
check =
v: lib.isAttrs v && lib.length (builtins.attrNames (builtins.removeAttrs v [ "__functor" ])) == 1;
merge = lib.mergeUniqueOption {
message = "";
merge =
loc: defs:
let
def = builtins.head defs;
name = builtins.head (builtins.attrNames (builtins.removeAttrs def.value [ "__functor" ]));
args = kdl-args.merge (loc ++ name) [
{
inherit (def) file;
value = def.value.${name};
}
];
in
{
${name} = args;
};
};
};
kdl-args =
let
arg = lib.types.either (lib.types.attrsOf kdl-value) kdl-value;
args = lib.types.either (lib.types.listOf arg) arg;
in
lib.mkOptionType {
name = "kdl-args";
description = "kdl arguments";
descriptionClass = "noun";
inherit (lib.types.uniq args) check merge;
};
kdl-nodes = lib.types.listOf kdl-node // {
name = "kdl-nodes";
description = "kdl nodes";
descriptionClass = "noun";
};
kdl-document = lib.mkOptionType {
name = "kdl-document";
description = "kdl document";
descriptionClass = "noun";
check = v: builtins.isList v || builtins.isAttrs v;
merge =
loc: defs:
kdl-nodes.merge loc (
map (def: {
inherit (def) file;
value =
let
value' = lib.remove null (lib.flatten def.value);
in
lib.warnIf (def.value != value')
"kdl document defined in `${def.file}` for `${lib.showOption loc}` is not normalized. please ensure that it is a flat list of nodes."
value';
}) defs
);
};
in
{
inherit
node
plain
leaf
magic-leaf
flag
serialize
;
types = {
inherit
kdl-value
kdl-node
kdl-nodes
kdl-leaf
kdl-args
kdl-document
;
};
}