-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathnixos-modules.nix
402 lines (345 loc) · 15.8 KB
/
nixos-modules.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{
self,
moduleWithSystem,
...
}: {
flake = {config, ...}: {
nixosModules.default = config.nixosModules.update-systemd-resolved;
nixosModules.update-systemd-resolved =
moduleWithSystem
(
perSystem @ {config}: nixos @ {
config,
lib,
pkgs,
...
}: let
inherit (lib) mkOption mkPackageOption types;
cfg = config.programs.update-systemd-resolved;
# Convert to a format systemd understands
bool2str = bool:
if bool
then "yes"
else "no";
enumOrBool = enum: types.coercedTo types.bool bool2str (types.enum ((lib.toList enum) ++ ["yes" "no"]));
mkResolvedConfDesc = name: ''
See the description of `${name}` in {manpage}`resolved.conf(5)` for
the meaning of this option and its available values.
'';
mkResolvedConfDescWithDefault = name: ''
${mkResolvedConfDesc name}
In addition to the values documented there, this option also
accepts the value "default", signifying that this link should use
the global value for `${name}` configured in `resolved.conf`.
'';
mkResolvectlDesc = cmd: ''
See {manpage}`resolvectl(1)`'s coverage of {command}`${cmd}` for a
description of this feature.
'';
dnsModule = types.submodule ({name, ...}: {
options = {
address = mkOption {
type = types.nonEmptyStr;
default = name;
description = ''
The IPv4 or IPv6 address of the DNS server.
'';
};
port = mkOption {
type = types.nullOr types.port;
default = null;
description = ''
The port number of the DNS server.
'';
};
interface = mkOption {
type = types.nullOr types.nonEmptyStr;
default = null;
description = ''
Network interface name or index (note that this is as
detailed as {manpage}`resolved.conf(5)` gets about the
meaning of the interface component of a DNS server
specification).
'';
};
sni = mkOption {
type = types.nullOr types.nonEmptyStr;
default = null;
description = ''
Server name indication to send when using DNS-over-TLS.
'';
};
__toString = mkOption {
type = types.functionTo types.str;
readOnly = true;
description = ''
String representation of the DNS server.
'';
default = self: let
looksLikeIPv6 = lib.hasInfix ":" self.address;
hasPort = self.port != null;
address =
if looksLikeIPv6 && hasPort
then "[${self.address}]"
else self.address;
in
lib.concatStrings ([
address
]
++ lib.optional hasPort ":${toString self.port}"
++ lib.optional (self.interface != null) "%${self.interface}"
++ lib.optional (self.sni != null) "#${self.sni}");
};
};
});
dnsType = types.coercedTo types.nonEmptyStr (address: {inherit address;}) dnsModule;
in {
options = {
programs.update-systemd-resolved = {
package = mkPackageOption perSystem.config.packages "update-systemd-resolved" {};
servers = mkOption {
default = {};
description = ''
Attribute set of `update-systemd-resolved` configurations.
Intended to be included in
{option}`services.openvpn.servers.<name>.config` entries.
'';
type = types.attrsOf (types.submodule ({
name,
config,
...
}: {
options = {
openvpnServerName = mkOption {
type = types.str;
default = name;
description = ''
`<name>` in
{option}`services.openvpn.servers.<name>.config`.
'';
};
includeAutomatically = mkOption {
type = types.bool;
default = false;
description = ''
Whether to include the generated configuration in
{option}`services.openvpn.servers.<name>.config`.
'';
};
pushSettings = mkOption {
type = types.bool;
default = false;
description = ''
Whether to push {command}`update-system-resolved`
settings with OpenVPN's {command}`push` directive.
Enable this if the target OpenVPN instance is a server;
disable it if the target instance is a client.
'';
};
config = mkOption {
type = types.lines;
readOnly = true;
description = ''
The configuration text for inclusion in
{option}`services.openvpn.servers.<name>.config`.
'';
};
configFile = mkOption {
type = types.path;
readOnly = true;
default = pkgs.writeText "update-systemd-resolved-${name}.conf" config.config;
defaultText = "${toString builtins.storeDir}/<hash>-update-systemd-resolved-<name>.conf";
description = ''
A configuration file containing
{option}`programs.update-systemd-resolved.servers.<name>.config`
for inclusion in {option}`services.openvpn.servers.<name>.config`
via the {command}`config` directive.
'';
};
settings = mkOption {
default = {};
description = ''
DNS-related settings for this VPN's link.
'';
type = types.submodule ({...}: {
options = {
# TODO DNS6
dns = mkOption {
type = types.attrsOf dnsType;
default = {};
example = {
resolver-the-first = {
address = "1.2.3.4";
port = 5353;
};
resolver-the-second = "2.3.4.5";
"3.4.5.6" = {};
};
description = ''
Attribute set naming DNS servers to configure for
this VPN's link.
${mkResolvedConfDesc "DNS"}
'';
};
domain = mkOption {
type = types.nullOr types.nonEmptyStr;
default = null;
description = ''
Main domain to configure for this link.
${mkResolvedConfDesc "Domains"}
'';
};
searchDomains = mkOption {
type = types.listOf types.nonEmptyStr;
default = [];
description = ''
List of search domains to configure for this
link.
${mkResolvedConfDesc "Domains"}
'';
};
routeOnlyDomains = mkOption {
type = types.listOf types.nonEmptyStr;
default = [];
description = ''
List of route-only domains to configure for this
link.
${mkResolvedConfDesc "Domains"}
'';
};
defaultRoute = mkOption {
type = enumOrBool null;
default = true;
description = ''
Whether to use the DNS servers configured for
this link to resolve queries for domains not
explicitly assigned to the servers on any other
link.
${mkResolvectlDesc "default-route"}
'';
};
dnsOverTLS = mkOption {
type = enumOrBool [null "default" "opportunistic"];
default = null;
description = ''
Whether to enable DNS-over-TLS for this link.
${mkResolvedConfDescWithDefault "DNSOverTLS"}
'';
};
dnssec = mkOption {
type = enumOrBool [null "allow-downgrade" "default"];
default = null;
description = ''
Whether to enable DNSSEC for this link.
${mkResolvedConfDescWithDefault "DNSSEC"}
'';
};
dnssecNegativeTrustAnchors = mkOption {
type = types.listOf types.nonEmptyStr;
default = [];
description = ''
DNSSEC negative trust anchors to configure for
this link. See the `NEGATIVE TRUST ANCHORS`
section in {manpage}`dnssec-trust-anchors.d` for
a description of negative trust anchors and how
to specify them.
'';
};
flushCaches = mkOption {
type = enumOrBool null;
default = null;
description = ''
Whether to flush `systemd-resolved`'s cache upon
starting the VPN.
${mkResolvectlDesc "flush-caches"}
'';
};
llmnr = mkOption {
type = enumOrBool [null "default" "resolve"];
default = null;
description = ''
Whether to enable LLMNR for this link.
${mkResolvedConfDescWithDefault "LLMNR"}
'';
};
multicastDNS = mkOption {
type = enumOrBool [null "default" "resolve"];
default = null;
description = ''
Whether to enable multicast DNS for this link.
${mkResolvedConfDescWithDefault "MulticastDNS"}
'';
};
resetServerFeatures = mkOption {
type = enumOrBool null;
default = null;
description = ''
Whether to reset learned server features when
bringing up the VPN link.
${mkResolvectlDesc "reset-server-features"}
'';
};
resetStatistics = mkOption {
type = enumOrBool null;
default = null;
description = ''
Whether to reset the statistics counters shown in
{command}`resolvectl statistics` to zero when
bringing up the VPN link.
${mkResolvectlDesc "reset-statistics"}
'';
};
};
});
};
};
config = {
config = let
renderDHCPOption = let
client = option: value: let
ucOption = lib.toUpper option;
in "dhcp-option ${ucOption} ${toString value}";
server = option: value: "push \"${client option value}\"";
in
if config.pushSettings
then server
else client;
maybeRenderDHCPOption = option: value:
lib.optionalString (value != null) (renderDHCPOption option value);
renderDHCPOptionList = option:
lib.concatMapStringsSep "\n" (renderDHCPOption option);
in ''
config ${cfg.package}/share/doc/openvpn/update-systemd-resolved.conf
${renderDHCPOptionList "dns" (builtins.attrValues config.settings.dns)}
${maybeRenderDHCPOption "domain" config.settings.domain}
${renderDHCPOptionList "domain-route" (config.settings.routeOnlyDomains)}
${renderDHCPOptionList "domain-search" (config.settings.searchDomains)}
${maybeRenderDHCPOption "dnssec" config.settings.dnssec}
${renderDHCPOptionList "dnssec-negative-trust-anchors" config.settings.dnssecNegativeTrustAnchors}
${maybeRenderDHCPOption "default-route" config.settings.defaultRoute}
${maybeRenderDHCPOption "dns-over-tls" config.settings.dnsOverTLS}
${maybeRenderDHCPOption "flush-caches" config.settings.flushCaches}
${maybeRenderDHCPOption "llmnr" config.settings.llmnr}
${maybeRenderDHCPOption "multicast-dns" config.settings.multicastDNS}
${maybeRenderDHCPOption "reset-server-features" config.settings.resetServerFeatures}
${maybeRenderDHCPOption "reset-statistics" config.settings.resetStatistics}
'';
};
}));
};
};
};
config = {
services.openvpn.servers = lib.mapAttrs' (name: value: {
name = value.openvpnServerName;
value = {
config = lib.mkAfter ''
config ${value.configFile}
'';
};
}) (lib.filterAttrs (_: s: s.includeAutomatically) cfg.servers);
};
}
);
};
}