-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdns.nix
303 lines (268 loc) · 8.75 KB
/
dns.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{ lib, nixus, config, ... }:
let
inherit (lib) types;
cfg = config.dns;
recordTypes = {
A = {
options.address = lib.mkOption {
type = types.str;
};
stringCoerce = s: { address = s; };
toData = v: v.address;
};
AAAA = {
options.address = lib.mkOption {
type = types.str;
};
stringCoerce = s: { address = s; };
toData = v: v.address;
};
NS = {
options.domain = lib.mkOption {
type = types.str;
};
stringCoerce = s: { domain = s; };
toData = v: v.domain;
};
CNAME = {
options.domain = lib.mkOption {
type = types.str;
};
stringCoerce = s: { domain = s; };
toData = v: v.domain;
};
CAA = {
options.flags.issuerCritical = lib.mkOption {
type = types.bool;
default = false;
};
options.tag = lib.mkOption {
type = types.enum [ "issue" "issuewild" "iodef" ];
};
options.value = lib.mkOption {
type = types.str;
};
stringCoerce = s: { tag = "issue"; value = s; };
toData = v: "${if v.flags.issuerCritical then "128" else "0"} ${v.tag} \"${lib.escape [ "\"" ] v.value}\"";
};
MX = {
options.preference = lib.mkOption {
type = types.int;
default = 10;
};
options.domain = lib.mkOption {
type = types.str;
};
stringCoerce = s: { domain = s; };
toData = v: "${toString v.preference} ${v.domain}";
};
TXT = {
options.text = lib.mkOption {
type = types.str;
};
stringCoerce = s: { text = s; };
toData = v: "\"${lib.escape [ "\"" ] v.text}\"";
};
SRV = {
options.priority = lib.mkOption {
type = types.int;
default = 0;
};
options.weight = lib.mkOption {
type = types.int;
default = 100;
};
options.port = lib.mkOption {
type = types.port;
};
options.target = lib.mkOption {
type = types.str;
};
toData = v: "${toString v.priority} ${toString v.weight} ${toString v.port} ${v.target}";
};
};
/*
{
"com" = {
"infinisil" = {
_zone = "infinisil.com";
"sub" = {
_zone = "sub.infinisil.com";
};
};
};
}
*/
zones =
let
zoneAttr = zone: lib.setAttrByPath (lib.reverseList (lib.splitString "." zone)) { _zone = zone; };
result = lib.foldl' (a: e: lib.recursiveUpdate a (zoneAttr e)) {} (lib.attrNames cfg.zones);
in result;
getZone = domain:
assert lib.hasSuffix "." domain;
let
go = zones: path:
if path != [] && zones ? ${lib.head path} then go zones.${lib.head path} (lib.tail path)
else zones._zone or null;
elements = lib.reverseList (lib.init (lib.splitString "." domain));
in go zones elements;
recordSubmodule = { name, ... }: let zone = getZone name; in {
options = lib.mapAttrs (_: value:
let
module = types.submodule ({ options, config, ... }: {
options = value.options // {
ttl = lib.mkOption {
type = types.int;
};
zone = lib.mkOption {
type = types.str;
};
};
config.zone = lib.mkIf (zone != null) (lib.mkDefault zone);
config.ttl = lib.mkIf options.zone.isDefined (lib.mkDefault cfg.zones.${config.zone}.ttl);
});
type =
if value ? stringCoerce then
with types; coercedTo (either str attrs) lib.singleton (listOf (coercedTo str value.stringCoerce module))
else
with types; coercedTo attrs lib.singleton (listOf module);
in lib.mkOption {
type = type;
default = [];
}
) recordTypes;
};
recordList = lib.concatLists (lib.mapAttrsToList (name: types:
lib.concatLists (lib.mapAttrsToList (type: records:
# TODO: Maybe remove duplicates?
map (record: {
inherit name type;
inherit (record) ttl zone;
data = recordTypes.${type}.toData record;
}) records
) types)
) cfg.records);
recordsByZone = lib.mapAttrs (_: map (record: removeAttrs record [ "zone" ]))
(lib.groupBy (record: record.zone) recordList);
soaRecord = zoneCfg:
let
inherit (zoneCfg) soa;
serial = if soa.serial == null then "@NIXUS_ZONE_SERIAL@" else toString soa.serial;
in {
name = zoneCfg.name + ".";
type = "SOA";
ttl = soa.ttl;
# TODO: Email escaping and transforming
data = "${soa.master} ${soa.email} ${serial} ${toString soa.refresh} ${toString soa.retry} ${toString soa.expire} ${toString soa.negativeTtl}";
};
nodeConfigs = lib.mapAttrs (node: zones: {
configuration = {
networking.firewall.allowedUDPPorts = [ 53 ];
services.bind = {
enable = true;
zones = lib.listToAttrs (lib.forEach zones (zone: lib.nameValuePair zone.name {
master = true;
file = zone.zonefile;
}));
};
};
}) (lib.groupBy (z: z.primaryNode) (lib.attrValues cfg.zones));
in {
# Records that automatically get set to the appropriate zone
options.dns.records = lib.mkOption {
type = types.attrsOf (types.submodule recordSubmodule);
default = {};
};
options.dns.zones = lib.mkOption {
default = {};
type = types.attrsOf (types.submodule ({ name, config, ... }: {
options.name = lib.mkOption {
type = types.str;
default = name;
};
options.primaryNode = lib.mkOption {
type = types.str;
description = ''
Nixus node for the primary server
'';
};
options.ttl = lib.mkOption {
type = types.int;
description = ''
The TTL to use for records in this zone if the records themselves don't specify it.
'';
};
options.records = lib.mkOption {
type = types.listOf (types.submodule {
options.name = lib.mkOption {
type = types.str;
description = "Record owner name";
};
options.type = lib.mkOption {
type = types.str;
description = "Record type";
};
options.ttl = lib.mkOption {
type = types.int;
description = "Record TTL";
};
options.data = lib.mkOption {
type = types.str;
description = "Record data";
};
});
default = [ (soaRecord config) ] ++ recordsByZone.${config.name};
};
options.zonefile = lib.mkOption {
type = types.path;
default = nixus.pkgs.runCommand "${config.name}.zone" {
contents = lib.concatMapStrings (record:
"${record.name} ${toString record.ttl} IN ${record.type} ${record.data}\n"
) config.records;
passAsFile = [ "contents" ];
} ''
substitute "$contentsPath" "$out" --subst-var-by NIXUS_ZONE_SERIAL "$(date +%s)"
${lib.getBin nixus.pkgs.bind}/bin/named-checkzone ${lib.escapeShellArg config.name} "$out"
'';
};
options.soa = {
ttl = lib.mkOption {
type = types.int;
description = "TTL of the SOA record itself.";
};
master = lib.mkOption {
type = types.str;
description = "The primary master name server for this zone.";
};
email = lib.mkOption {
type = types.str;
description = "Email address of the person responsible for this zone.";
};
# TODO: Don't require these 4 fields unless there are secondary servers
serial = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = "Serial number for this zone. Should be updated if any records change so that secondary servers are refreshed. Null indicates a serial number automatically generated from the current unix epoch.";
};
refresh = lib.mkOption {
type = types.int;
description = "Number of seconds after which secondary name servers should query the master for the SOA record, to detect zone changes";
};
retry = lib.mkOption {
type = types.int;
description = "Number of seconds after which secondary name servers should retry to request the serial number from the master if the master does not respond.";
};
expire = lib.mkOption {
type = types.int;
description = "Number of seconds after which secondary name servers should stop answering request for this zone if the master does not respond.";
};
negativeTtl = lib.mkOption {
type = types.int;
description = "How long negative responses should be cached for.";
};
};
config.soa.ttl = lib.mkDefault config.ttl;
}));
};
config.nodes = nodeConfigs;
}