-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainerModule.nix
More file actions
809 lines (769 loc) · 29.9 KB
/
containerModule.nix
File metadata and controls
809 lines (769 loc) · 29.9 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
{
lib,
config,
pkgs,
inputs,
hostName,
...
}:
let
cfg = config.myModules.container;
enabledContainers = lib.filterAttrs (n: v: v.enable) cfg.containers;
getPrefix = (
v:
if v.mullvadRouting then
"10.42.44"
else
(if (v.nebulaGateway != null) then "10.42.43" else "10.42.42")
);
getIP = (v: "${getPrefix v}.${builtins.toString v.hostID}");
dnsRecords = lib.mergeAttrsList (
lib.mapAttrsToList (n: v: {
"${getIP v}" = v.associatedDomains;
}) enabledContainers
);
containersEnabled = enabledContainers != { };
nebulaContainersEnabled = (lib.filterAttrs (_: v: !v.mullvadRouting) enabledContainers) != { };
nebulaGatewayContainersEnabled =
(lib.filterAttrs (_: v: v.nebulaGateway != null) enabledContainers) != { };
mullvadContainersEnabled = (lib.filterAttrs (_: v: v.mullvadRouting) enabledContainers) != { };
nebulaContainerIPNetId = "10.28.129";
in
{
imports = [
./nebulaModule.nix
];
options.myModules.container = {
externalNetworkInterface = lib.mkOption {
type = lib.types.str;
description = "The external network interface that is used for the default route of this machine (where it gets internet connection over)";
};
mullvadPrivateKeyFile = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Path to the file containing the wireguard private key for the mullvad config";
};
associatedHostDomains = lib.mkOption {
type = lib.types.listOf lib.types.singleLineStr;
default = [ ];
description = "A list of domains associated with the host machine. All containers will have DNS records changed such that they will directly point to the internal ip address of the host machine";
};
containers = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether this container should be enabled";
};
mullvadRouting = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether the containers internet traffic should be routed (and forced) through mullvad";
};
after = lib.mkOption {
type = lib.types.listOf lib.types.singleLineStr;
default = [ ];
description = "List of container names that are required for running this container";
};
enableSops = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether sops-nix should be enabled inside the container";
};
hostID = lib.mkOption {
type = lib.types.ints.u8;
description = "Host ID of local and nebula IP address (equivalent to what vmID was for Proxmox VMs before)";
};
nebulaGateway = lib.mkOption {
type = lib.types.nullOr lib.types.singleLineStr;
default = null;
description = "If this container should only have access to its nebula interface (aside from loopback) and route everything through the nebula network then set this to the nebula IP of the nebula device that should be the gateway (i.e. exit node)";
};
openTCPPorts = lib.mkOption {
type = lib.types.listOf lib.types.port;
default = [ ];
description = "List of TCP ports that should be opened at container (for both NixOS firewall and nebula firewall for edge group)";
};
openUDPPorts = lib.mkOption {
type = lib.types.listOf lib.types.port;
default = [ ];
description = "List of UDP ports that should be opened at container (for both NixOS firewall and nebula firewall for edge group)";
};
forwardPorts = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to forward opened udp and tcp ports to the local external interface using dnat";
};
associatedDomains = lib.mkOption {
type = lib.types.listOf lib.types.singleLineStr;
default = [ ];
description = "A list of domains associated with this container. Other containers will have DNS records changed such that they will directly point to this containers internal ip address";
};
permittedUnfreePackages = lib.mkOption {
type = lib.types.listOf lib.types.singleLineStr;
default = [ ];
description = "List of unfree package names that should be allowed inside this container";
};
additionalBindMounts = lib.mkOption {
type = lib.types.attrs;
default = { };
description = "Bind mounts in addition to the one to /persist/backMeUp and /persist/sops-nix";
};
additionalContainerConfig = lib.mkOption {
type = lib.types.attrs;
default = { };
description = "Additional configuration that will be put into the NixOS module under containers.<name>, e.g. to configure extra capabilities";
};
additionalSpecialArgs = lib.mkOption {
type = lib.types.attrs;
default = { };
description = "A set of special arguments to be passed to the containers NixOS modules";
};
config = lib.mkOption {
type = lib.types.path;
description = "Path to config file. Note that some things like stateVersion and DNS fixes are already being configured by this module for all containers.";
};
};
}
);
};
};
config = lib.mkIf containersEnabled {
assertions = [
{
assertion = mullvadContainersEnabled && cfg.mullvadPrivateKeyFile != null;
message = "If you want to use mullvad containers, then you have to set the 'mullvadPrivateKeyFile' option as well";
}
{
assertion =
!(lib.any (v: v.mullvadRouting && v.nebulaGateway != null) (builtins.attrValues enabledContainers));
message = "You cannot set 'nebulaGateway' on a container with mullvad routing";
}
{
assertion =
!(lib.any (v: v.forwardPorts && v.mullvadRouting) (builtins.attrValues enabledContainers));
message = "You cannot enable 'forwardPorts' on a container with mullvad routing";
}
{
assertion =
!(lib.any (v: v.forwardPorts && v.nebulaGateway != null) (builtins.attrValues enabledContainers));
message = "You cannot enable 'forwardPorts' on a container with 'nebulaGateway' defined";
}
];
#setup nebula connection for every container
myModules.nebula = builtins.mapAttrs (n: v: {
secretHostName = hostName;
installHostName = n;
ipMap = config.myModules.nebula."serverNetwork".ipMap;
lighthouseMap = config.myModules.nebula."serverNetwork".lighthouseMap;
}) enabledContainers;
services.nebula.networks = (
builtins.mapAttrs (n: v: {
settings.tun.unsafe_routes = lib.mkIf (v.nebulaGateway != null) [
{
route = "0.0.0.0/0";
via = v.nebulaGateway;
install = false;
}
];
firewall.inbound =
(builtins.concatLists (
builtins.map (port: [
{
port = port;
proto = "tcp";
group = "edge";
}
{
#allow admins to bypass reverse proxy for debugging/maintenance purposes
port = port;
proto = "tcp";
group = "admin";
}
]) v.openTCPPorts
))
++ (builtins.concatLists (
builtins.map (port: [
{
port = port;
proto = "udp";
group = "edge";
}
{
port = port;
proto = "udp";
group = "admin";
}
]) v.openUDPPorts
));
}) enabledContainers
);
#networking stuff that all containers share. systemd service templates
#every container gets their own network namespace, nebula interface and optionally veth pair
#taken from https://uint.one/posts/all-internet-over-wireguard-using-systemd-networkd-on-nixos/ and adapted for my use case with nebula
boot.kernel.sysctl."net.ipv4.ip_forward" = lib.mkDefault 1;
networking = {
hosts = dnsRecords // {
"127.0.0.1" = cfg.associatedHostDomains;
};
nftables = {
enable = true;
tables."custom_container_rules" = lib.mkIf mullvadContainersEnabled {
family = "ip";
content = ''
chain forward {
type filter hook forward priority filter; policy accept;
# allow established traffic
ct state established,related accept
# allow br0 to go to external interface (no VPN)
iifname "br0" oifname "${cfg.externalNetworkInterface}" accept
# allow mullvad containers to the mullvad interface (VPN)
iifname "br2" oifname "wg0-mullvad" accept
''
+ lib.optionalString nebulaContainersEnabled ''
# nebula containers -> nebula gateway containers
ip saddr 10.42.42.0/24 ip daddr 10.42.43.0/24 accept
# nebula containers <- nebula gateway containers
ip saddr 10.42.43.0/24 ip daddr 10.42.42.0/24 accept
# nebula containers -> mullvad containers
ip saddr 10.42.42.0/24 ip daddr 10.42.44.0/24 accept
# nebula containers <- mullvad containers
ip saddr 10.42.44.0/24 ip daddr 10.42.42.0/24 accept
# nebula gateway containers -> mullvad containers
ip saddr 10.42.43.0/24 ip daddr 10.42.44.0/24 accept
# nebula gateway containers <- mullvad containers
ip saddr 10.42.44.0/24 ip daddr 10.42.43.0/24 accept
''
+ ''
# drop anything nebula gateway containers (kill switch)
iifname "br1" drop
# drop anything else from mullvad containers (kill switch)
iifname "br2" drop
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
# NAT mullvad container subnet when leaving via mullvad
ip saddr 10.42.44.0/24 oifname "wg0-mullvad" masquerade
}
'';
};
};
firewall = {
checkReversePath = "loose";
allowedTCPPorts = (
builtins.concatLists (
lib.mapAttrsToList (n: v: if v.forwardPorts then v.openTCPPorts else [ ]) enabledContainers
)
);
allowedUDPPorts = (
builtins.concatLists (
lib.mapAttrsToList (n: v: if v.forwardPorts then v.openUDPPorts else [ ]) enabledContainers
)
);
};
nat = {
enable = true;
internalInterfaces = [ "br0" ];
externalInterface = cfg.externalNetworkInterface;
enableIPv6 = false;
forwardPorts = (
builtins.concatLists (
lib.mapAttrsToList (
n: v:
if v.forwardPorts then
(
(builtins.map (tcp_port: {
destination = "10.42.42.${builtins.toString v.hostID}:${builtins.toString tcp_port}";
proto = "tcp";
sourcePort = tcp_port;
}) v.openTCPPorts)
++ (builtins.map (udp_port: {
destination = "10.42.42.${builtins.toString v.hostID}:${builtins.toString udp_port}";
proto = "udp";
sourcePort = udp_port;
}) v.openUDPPorts)
)
else
[ ]
) enabledContainers
)
);
};
};
systemd = lib.mkMerge (
#configure bridge for nebula containers
(lib.optional nebulaContainersEnabled {
network = {
netdevs."20-br0".netdevConfig = {
Kind = "bridge";
Name = "br0";
};
networks."20-br0" = {
name = "br0";
DHCP = "no";
addresses = [
{
Address = "10.42.42.1/24";
}
];
};
};
})
++ (lib.optional nebulaGatewayContainersEnabled {
network = {
netdevs."20-br1".netdevConfig = {
Kind = "bridge";
Name = "br1";
};
networks."20-br1" = {
name = "br1";
DHCP = "no";
addresses = [
{
Address = "10.42.43.1/24";
}
];
};
};
})
#configure bridge for mullvad containers
++ (lib.optional mullvadContainersEnabled {
network = {
netdevs = {
"20-br2".netdevConfig = {
Kind = "bridge";
Name = "br2";
};
"30-wg0_mullvad" = {
netdevConfig = {
Kind = "wireguard";
Name = "wg0-mullvad";
};
wireguardConfig = {
ListenPort = 51820;
PrivateKeyFile = cfg.mullvadPrivateKeyFile;
};
wireguardPeers = [
{
PublicKey = "vVQKs2TeTbdAvl3sH16UWLSESncXAj0oBaNuFIUkLVk=";
AllowedIPs = [
"::/0"
"0.0.0.0/0"
];
RouteTable = 1000;
Endpoint = "185.209.196.73:51820";
}
];
};
};
networks = {
"20-br2" = {
name = "br2";
DHCP = "no";
addresses = [
{
Address = "10.42.44.1/24";
}
];
};
"30-wg0_mullvad" = {
matchConfig.Name = "wg0-mullvad";
address = [
"10.72.66.107/32"
"fc00:bbbb:bbbb:bb01::9:426a/128"
];
dns = [
"10.64.0.1"
];
routingPolicyRules = [
{
Family = "both";
IncomingInterface = "br2";
Table = 1000;
Priority = 10;
}
{
To = "10.42.42.0/24";
Priority = 5;
}
{
To = "10.42.43.0/24";
Priority = 5;
}
];
};
};
};
})
#configure some overwrites for all containers
++ (lib.mapAttrsToList (n: v: {
tmpfiles.settings."10-containerMountDirs" =
lib.optionalAttrs v.enableSops {
"/persist/sops-nix/${n}"."d" = {
user = "root";
group = "root";
mode = "0755";
};
}
// lib.mkMerge (
lib.mapAttrsToList (n: v: {
"${v.hostPath}"."d" = {
user = "root";
group = "root";
mode = "0755";
};
}) v.additionalBindMounts
);
#setting container startup order
services."container@${n}".after = builtins.map (v: "container@${v}.service") v.after;
}) enabledContainers)
#configure custom systemd services for nebula containers
++ (lib.mapAttrsToList (
n: v:
let
shortenedN = builtins.substring 0 11 n;
nebulaMoveRequired = (v.nebulaGateway != null) || v.mullvadRouting;
in
{
services = {
"netns@${n}" = {
description = "${n} containers veth network namespace";
serviceConfig = with pkgs; {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${iproute2}/bin/ip netns add ${shortenedN}";
ExecStop = "${iproute2}/bin/ip netns del ${shortenedN}";
};
};
"lo@${n}" = {
description = "loopback in ${n} containers network namespace";
bindsTo = [ "netns@${n}.service" ];
after = [ "netns@${n}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let
start =
with pkgs;
writeShellScript "lo-up" ''
set -e
${iproute2}/bin/ip -n $1 addr add 127.0.0.1/8 dev lo
${iproute2}/bin/ip -n $1 link set lo up
'';
in
"${start} ${shortenedN}";
ExecStopPost = with pkgs; "${iproute2}/bin/ip -n ${shortenedN} link del lo";
};
};
"veth@${n}" = {
description = "virtual ethernet network interface between the main and ${n} containers network namespaces";
bindsTo = [
"netns@${n}.service"
"network-online.target"
];
after = [
"netns@${n}.service"
"network-online.target"
];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let
start =
with pkgs;
writeShellScript "veth-${n}-up" ''
set -e
${iproute2}/bin/ip link add ve-${shortenedN} type veth peer name eth-${shortenedN}
${iproute2}/bin/ip link set eth-${shortenedN} netns ${shortenedN}
${iproute2}/bin/ip -n ${shortenedN} link set dev eth-${shortenedN} name eth-lan
${iproute2}/bin/ip -n ${shortenedN} addr add 10.42.42.${builtins.toString v.hostID}/24 dev eth-lan
${iproute2}/bin/ip -n ${shortenedN} link set eth-lan up
${iproute2}/bin/ip link set dev ve-${shortenedN} master br0
${iproute2}/bin/ip link set ve-${shortenedN} up
${iproute2}/bin/ip -n ${shortenedN} route add default via 10.42.42.1
'';
in
"${start}";
ExecStopPost =
let
stop =
with pkgs;
writeShellScript "veth-down" ''
${iproute2}/bin/ip -n $1 link del eth-lan
${iproute2}/bin/ip link del ve-$1
'';
in
"${stop} ${shortenedN}";
};
};
"nebulaVeth@${n}" =
let
nebulaSystemdService = config.systemd.services."nebula@${n}";
in
{
description = "nebula network interface in ${n} containers network namespace (same one where veth is in)";
bindsTo = [
"netns@${n}.service"
"veth@${n}.service"
];
wants = [
"network-online.target"
"nss-lookup.target"
];
after = [
"netns@${n}.service"
"veth@${n}.service"
"network-online.target"
"nss-lookup.target"
];
unitConfig.StartLimitIntervalSec = 0;
serviceConfig =
with pkgs;
lib.mkMerge [
nebulaSystemdService.serviceConfig
{
ExecStart = lib.mkForce "${iproute2}/bin/ip netns exec ${shortenedN} ${nebulaSystemdService.serviceConfig.ExecStart}";
CapabilityBoundingSet = lib.mkForce [
"CAP_SYS_ADMIN"
"CAP_NET_ADMIN"
];
AmbientCapabilities = lib.mkForce [
"CAP_SYS_ADMIN"
"CAP_NET_ADMIN"
];
RestrictNamespaces = lib.mkForce false;
RestrictSUIDSGID = lib.mkForce false;
}
];
};
#overwriting default systemd services
"container@${n}" =
let
serviceList =
if nebulaMoveRequired then
[
"lo@neb-${n}.service"
"moveNebNS@${n}.service"
"veth@neb-${n}.service"
]
else
[
"lo@${n}.service"
"nebulaVeth@${n}.service"
];
in
{
requires = serviceList;
after = serviceList;
};
"nebula@${n}".enable = lib.mkForce false; # we have our own systemd service
}
// lib.optionalAttrs nebulaMoveRequired {
"netns@neb-${n}" = {
description = "${n} containers nebula-only network namespace";
serviceConfig = with pkgs; {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${iproute2}/bin/ip netns add neb-${shortenedN}";
ExecStop = "${iproute2}/bin/ip netns del neb-${shortenedN}";
};
};
"lo@neb-${n}" = {
description = "loopback in ${n} containers nebula-only network namespace";
bindsTo = [ "netns@neb-${n}.service" ];
after = [ "netns@neb-${n}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let
start =
with pkgs;
writeShellScript "lo-up" ''
set -e
${iproute2}/bin/ip -n $1 addr add 127.0.0.1/8 dev lo
${iproute2}/bin/ip -n $1 link set lo up
'';
in
"${start} neb-${shortenedN}";
ExecStopPost = with pkgs; "${iproute2}/bin/ip -n neb-${shortenedN} link del lo";
};
};
"moveNebNS@${n}" = {
description = "Move nebula interface of ${n} container into its own nebula-only network namespace";
bindsTo = [
"netns@neb-${n}.service"
"nebulaVeth@${n}.service"
];
after = [
"netns@neb-${n}.service"
"nebulaVeth@${n}.service"
];
unitConfig = {
StartLimitBurst = 10;
StartLimitInterval = 11;
};
serviceConfig =
with pkgs;
let
nebulaInterfaceName = builtins.substring 0 15 "neb-${n}";
in
{
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let
start =
with pkgs;
writeShellScript "moveNebNS-${n}" (
''
set -e
${iproute2}/bin/ip -n ${shortenedN} link set ${nebulaInterfaceName} netns neb-${shortenedN}
${iproute2}/bin/ip -n neb-${shortenedN} addr add ${nebulaContainerIPNetId}.${builtins.toString v.hostID} dev ${nebulaInterfaceName}
${iproute2}/bin/ip -n neb-${shortenedN} link set ${nebulaInterfaceName} up
${iproute2}/bin/ip -n neb-${shortenedN} route add ${
config.myModules.nebula."serverNetwork".subnet
} dev ${nebulaInterfaceName}
''
+ lib.optionalString (v.nebulaGateway != null) ''
${iproute2}/bin/ip -n neb-${shortenedN} route add default via ${v.nebulaGateway}
''
);
in
"${start}";
ExecStop = "${iproute2}/bin/ip link set ${nebulaInterfaceName} ${shortenedN}";
Restart = "on-failure";
RestartSec = 1;
};
};
"veth@neb-${n}" = {
description = "virtual ethernet network interface between the main and ${n} containers nebula-only network namespaces";
bindsTo = [
"netns@neb-${n}.service"
"network-online.target"
];
after = [
"netns@neb-${n}.service"
"moveNebNS@${n}.service"
"network-online.target"
];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart =
let
start =
with pkgs;
writeShellScript "veth-neb-${n}-up" (
''
set -e
${iproute2}/bin/ip link add ven-${shortenedN} type veth peer name etn-${shortenedN}
${iproute2}/bin/ip link set etn-${shortenedN} netns neb-${shortenedN}
${iproute2}/bin/ip -n neb-${shortenedN} link set dev etn-${shortenedN} name eth-lan
${iproute2}/bin/ip -n neb-${shortenedN} addr add ${getIP v}/24 dev eth-lan
${iproute2}/bin/ip -n neb-${shortenedN} link set eth-lan up
${iproute2}/bin/ip link set dev ven-${shortenedN} master ${
if v.mullvadRouting then "br2" else "br1"
}
${iproute2}/bin/ip link set ven-${shortenedN} up
''
+ lib.optionalString (v.nebulaGateway != null) ''
${iproute2}/bin/ip -n neb-${shortenedN} route add 10.42.42.0/24 dev eth-lan via ${getPrefix v}.1
${iproute2}/bin/ip -n neb-${shortenedN} route add 10.42.44.0/24 dev eth-lan via ${getPrefix v}.1
''
+ lib.optionalString v.mullvadRouting ''
${iproute2}/bin/ip -n neb-${shortenedN} route add default via 10.42.44.1
''
);
in
"${start}";
ExecStopPost =
let
stop =
with pkgs;
writeShellScript "veth-down" ''
${iproute2}/bin/ip -n $1 link del eth-lan
${iproute2}/bin/ip link del ven-$1
'';
in
"${stop} ${shortenedN}";
};
};
};
#if the system uses systemd networkd, then mark our manually created veth interface as unmanaged so that networkd doesn't touch it
network.networks."20-${n}-unmanaged" = lib.mkIf config.systemd.network.enable {
matchConfig.Name = "ve-${shortenedN}";
linkConfig.Unmanaged = true;
};
}
) enabledContainers)
);
containers = builtins.mapAttrs (
n: v:
let
shortenedN = builtins.substring 0 11 n;
nebulaMoveRequired = (v.nebulaGateway != null) || v.mullvadRouting;
in
{
autoStart = true;
ephemeral = true;
#even changing age key for one machine will trigger a restart of containers.
#we want to restart them explicitly only when needed (or on reboot)!
restartIfChanged = false;
networkNamespace =
if nebulaMoveRequired then "/run/netns/neb-${shortenedN}" else "/run/netns/${shortenedN}";
bindMounts =
lib.optionalAttrs v.enableSops {
"/persist/sops-nix" = {
hostPath = "/persist/sops-nix/${n}";
isReadOnly = false;
};
}
// v.additionalBindMounts;
specialArgs = {
hostName = n;
inputs = inputs;
}
// v.additionalSpecialArgs;
config =
{ hostName, ... }:
{
imports = [
v.config
./promtail.nix # to get systemd-journal out of container into loki
]
++ lib.lists.optional v.enableSops ./sops.nix;
myModules.promtail.host = "${getPrefix v}.1";
nixpkgs.config.allowUnfreePredicate =
pkg: builtins.elem (lib.getName pkg) v.permittedUnfreePackages;
networking = {
hostName = hostName;
useHostResolvConf = lib.mkForce false;
firewall = {
allowedTCPPorts = v.openTCPPorts;
allowedUDPPorts = v.openUDPPorts;
};
hosts =
(lib.filterAttrs (ip: _: ip != (getIP v)) dnsRecords)
// (
let
hostIP = "${getPrefix v}.1";
in
{
"${hostIP}" = cfg.associatedHostDomains;
"127.0.0.1" = v.associatedDomains;
}
);
};
services.resolved.enable = true;
system.stateVersion = config.system.stateVersion; # stateVersion of container should be the same as the one of the host
};
}
// v.additionalContainerConfig
) enabledContainers;
};
}