This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
forked from nyiyui/qrystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnixos-modules.nix
471 lines (469 loc) · 18.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
args@{ self, system, nixpkgsFor, libFor, nixosLibFor, ldflags, packages, ...
}: {
nixosModules = let
peerOption = { lib }:
with lib;
with types;
{ name }:
(submodule {
options = (if name then {
name = mkOption {
type = str;
description = "name of the peer";
};
} else
{ }) // {
host = mkOption {
type = str;
default = "";
description = "Endpoint= in wg-quick(8) config";
};
allowedIPs = mkOption {
type = nullOr (listOf str);
description =
"AllowedIPs= in wg-quick(8) config. If null, an available one is automagically allocated.";
default = null;
};
canForward = mkOption {
type = bool;
description = "Peer can forward for other peers.";
default = false;
};
canSee = mkOption {
type = nullOr (oneOf [
(submodule {
options = {
only = mkOption {
type = listOf str;
description = "peer can only see these peers";
};
};
})
(enum [
"any"
]) # TODO: any option is not yet supported in cs config
]);
default = null;
};
allowedSRVs = mkOption {
type = nullOr (listOf (submodule {
options = {
service = mkOption { type = str; };
serviceAny = mkOption { type = bool; default = false; };
priorityMin = mkOption { type = port; default = 0; };
priorityMax = mkOption { type = port; default = 65535; };
weightMin = mkOption { type = port; default = 0; };
weightMax = mkOption { type = port; default = 65535; };
};
}));
default = null;
};
};
});
in {
node = { config, lib, pkgs, ... }:
with lib;
with types;
let
in let
cfg = config.qrystal.services.node;
mkConfigFile = cfg:
pkgs.writeText "node-config.json" (builtins.toJSON cfg.config);
in {
options.qrystal.services.node = {
enable = mkEnableOption "Enables the Qrystal Node service";
config = mkOption {
type = submodule {
options = {
trace = mkOption {
type = nullOr (submodule {
options = {
outputPath = mkOption {
type = path;
description = ''
Output path of the trace. Note that PrivateTmp=yes is set on the systemd unit, so "/tmp/trace" will actually be "/tmp/systemd-private.../tmp/trace".'';
};
waitUntilCNs = mkOption {
type = listOf str;
description =
"Wait for these CNs to reify before stopping the trace.";
};
};
});
default = null;
};
endpointOverride = mkOption {
type = nullOr path;
description = "Path to executable for endpoint override.";
default = null;
};
srvList = mkOption {
type = nullOr path;
description = "Path to list of SRV records.";
default = null;
};
hokuto = mkOption {
type = submodule {
options = {
configureDnsmasq = mkOption {
type = bool;
default = true;
description =
"Enable and configure dnsmasq to use Hokuto DNS server";
};
dnsmasqGoogleDNS = mkOption {
type = bool;
default = true;
description =
"Set 8.8.8.8 and 8.8.4.4 as DNS servers for dnsmasq. This is defaulted to true so if you forget to specify servers, your dnsmasq config isn't destroyed.";
};
addr = mkOption {
type = str;
default = "127.0.0.39";
description =
"Hokuto bind address (no port). Leave blank to disable";
};
parent = mkOption {
type = str;
default = ".qrystal.internal";
description =
"All domains inside networks will be of the format <peer>.<network>.<parent>";
};
extraParents = mkOption {
type = listOf (submodule {
options = {
network = mkOption {
type = nullOr str;
description =
"If not null, this parent domain will be for just this network.";
default = null;
};
domain = mkOption {
type = str;
description = "Parent domain.";
example = ".internal.example.org";
default = null;
};
};
});
default = [];
description = "Extra parent domains.";
};
};
};
default = { };
};
cs = mkOption {
type = submodule {
options = {
comment = mkOption {
type = str;
example = "main";
description = "Friendly name for CS";
};
endpoint = mkOption {
type = str;
example = "cs.qrystal.example.net:39252";
description = "Endpoint to CS";
};
tls = mkOption {
type = submodule {
options = {
certPath = mkOption {
type = path;
description = "Path to TLS certificate.";
};
};
};
};
networks = mkOption {
type = listOf str;
description = "Networks to pull from CS";
};
tokenPath = mkOption {
type = str;
example = "/run/secrets/qrystal-central-token-main";
description = "Path to file containing Central Token.";
};
azusa = mkOption {
type = nullOr (submodule {
options = {
networks = mkOption {
type = attrsOf
(peerOption { inherit lib; } { name = true; });
};
};
});
default = null;
description = "Push peer to net before pulling.";
};
};
};
};
};
};
};
};
config = mkIf cfg.enable {
services.dnsmasq = mkIf (cfg.config.hokuto.configureDnsmasq
&& cfg.config.hokuto.addr != "") {
enable = true;
resolveLocalQueries = true;
settings = {
server = [ "/${cfg.config.hokuto.parent}/${cfg.config.hokuto.addr}" ]
++ (map (ep: "/${ep.domain}/${cfg.config.hokuto.addr}") cfg.config.hokuto.extraParents)
++ (if cfg.config.hokuto.dnsmasqGoogleDNS then [
"8.8.8.8"
"8.8.4.4"
] else
[ ]);
conf-file = "${pkgs.dnsmasq}/share/dnsmasq/trust-anchors.conf";
dnssec = true;
listen-address = "::1,127.0.0.53";
interface = "lo";
bind-interfaces = true; # hokuto binds to cfg.config.hokuto.addr
};
};
users.groups.qrystal-node = { };
users.users.qrystal-node = {
isSystemUser = true;
description = "Qrystal Node";
group = "qrystal-node";
};
systemd.services.qrystal-node = let pkg = packages.runner;
in {
wantedBy = [ "network-online.target" ];
environment = {
"RUNNER_MIO_PATH" = "${pkg}/bin/runner-mio";
"RUNNER_HOKUTO_PATH" = "${pkg}/bin/runner-hokuto";
"RUNNER_NODE_PATH" = "${pkg}/bin/runner-node";
"RUNNER_NODE_CONFIG_PATH" = mkConfigFile cfg;
} // (if (cfg.config.trace != null) then {
"QRYSTAL_TRACE_OUTPUT_PATH" = cfg.config.trace.outputPath;
"QRYSTAL_TRACE_UNTIL_CNS" =
builtins.toJSON cfg.config.trace.waitUntilCNs;
} else
{ });
serviceConfig = {
Restart = "on-failure";
Type = "notify";
NotifyAccess = "all";
ExecStart = "${pkg}/bin/runner";
StateDirectory = "qrystal-node";
StateDirectoryMode = "0700";
WorkingDirectory = "${pkg}/lib";
PrivateTmp = "yes";
ProtectHome = "yes";
NoNewPrivileges = "yes";
PrivateDevices = "yes";
ProtectClock = "yes";
};
};
};
};
cs = { config, lib, pkgs, ... }:
with lib;
with types;
let
cfg = config.qrystal.services.cs;
mkConfigFile = cfg:
builtins.toFile "cs-config.json" (builtins.toJSON cfg.config);
in {
options.qrystal.services.cs = {
enable = mkEnableOption "Enables the Qrystal CS service";
config = mkOption {
type = submodule {
options = {
tls = mkOption {
type = submodule {
options = {
certPath = mkOption {
type = path;
description = "PEM-encoded TLS certificate";
};
keyPath = mkOption {
type = path;
description = "PEM-encoded TLS private key";
};
};
};
};
addr = mkOption {
type = str;
default = ":39252";
description = "Bind address of Node API";
};
ryoAddr = mkOption {
type = str;
default = ":39253";
description = "Bind address of Ryo (HTTP; for cs-push) API";
};
tokens = mkOption {
type = listOf (submodule {
options = {
name = mkOption {
type = str;
description = "Friendly name of token.";
};
hash = mkOption {
type = str;
description =
"Hash of the token (use qrystal-gen-keys).";
};
networks = mkOption {
type = nullOr (attrsOf str);
description =
"Nets this token can pull from and the corresponding peer names.";
};
canPull = mkOption {
type = bool;
default = false;
description =
"Allow token to pull peers (see networks)";
};
canPush = mkOption {
type = nullOr (submodule {
options = {
any = mkOption {
type = bool;
default = false;
description = "Can push to any peer in any net.";
};
networks = mkOption {
default = null;
type = nullOr (attrsOf (submodule {
options = {
name = mkOption {
type = str;
description = "Peer name.";
};
canSeeElement = mkOption {
type =
(oneOf [ (listOf str) (enum [ "any" ]) ]);
description =
"Pushed peers' canSee must be an element of canSeeElement.";
};
};
}));
};
};
});
default = null;
description = "Allow token to push peers.";
};
canAdminTokens = mkOption {
type = nullOr (submodule {
options = {
canPull = mkOption {
type = bool;
default = false;
description =
"Tokens added by this token can arbitrarily pull.";
};
canPush = mkOption {
type = bool;
default = false;
description =
"Tokens added by this token can arbitrarily push.";
};
};
});
default = null;
description =
"Allow token to add more tokens, or remove any tokens.";
};
canSRVUpdate = mkOption {
type = bool;
description = "Allow updating SRV allowanes for peers it can push to.";
default = false;
};
srvAllowances = mkOption {
type = nullOr (listOf (submodule {
options = {
service = mkOption { type = str; };
serviceAny = mkOption { type = bool; default = false; };
priorityMin = mkOption { type = port; default = 0; };
priorityMax = mkOption { type = port; default = 65535; };
weightMin = mkOption { type = port; default = 0; };
weightMax = mkOption { type = port; default = 65535; };
};
}));
default = null;
};
srvAllowancesAny = mkOption {
type = bool;
description = "Allow updating SRV allowances without restrictions by srvAllowances.";
default = false;
};
};
});
};
central = mkOption {
type = submodule {
options = {
networks = mkOption {
type = attrsOf (submodule {
options = {
keepalive = mkOption {
type = nullOr str;
default = null;
description =
"PersistentKeepalive= in wg-quick(8) config";
};
listenPort = mkOption {
type = port;
default = 39390;
description = "ListenPort= in wg-quick(8) config";
};
ips = mkOption {
type = listOf str;
default = [ "10.39.0/16" ];
description = "Endpoint= in wg-quick(8) config";
};
peers = mkOption {
type = attrsOf
(peerOption { inherit lib; } { name = false; });
description =
"All peers in the net. Note that more can be added later.";
default = { };
};
};
});
};
};
};
};
};
};
};
};
config = mkIf cfg.enable {
users.groups.qrystal-cs = { };
users.users.qrystal-cs = {
isSystemUser = true;
description = "Qrystal CS";
group = "qrystal-cs";
};
systemd.services.qrystal-cs = {
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = let pkg = packages.cs;
in {
User = "qrystal-cs";
Restart = "on-failure";
Type = "notify";
ExecStart = "${pkg}/bin/cs -config ${mkConfigFile cfg}";
RuntimeDirectory = "qrystal-cs";
RuntimeDirectoryMode = "0700";
StateDirectory = "qrystal-cs";
StateDirectoryMode = "0700";
LogsDirectory = "qrystal-cs";
LogsDirectoryMode = "0700";
PrivateTmp = "yes";
ProtectHome = "yes";
};
};
};
};
};
}