/
default.nix
408 lines (388 loc) · 12.5 KB
/
default.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
{ config, pkgs, lib, flakeArgs, ... }:
# borrowed some stuff from https://github.com/georgewhewell/nixos-host/blob/master/profiles/router.nix
# also begyn.be https://francis.begyn.be/blog/ipv6-nixos-router
# also https://github.com/skogsbrus/os/blob/406df9a6e38a805fdae8e683fe43b5a6c320b2ec/sys/router.nix https://skogsbrus.xyz/blog/2022/06/12/router/
let
name = "router";
wanInterface = "enp1s0f1"; # f1/f0 = top two ports
lanInterface = "enp1s0f2"; # f2/f3 = bottom two ports
useLanBridge = false;
lanBridge = if useLanBridge then "br0" else lanInterface;
debugInterface = "enp2s0f0"; # onboard port (should be eno1 but platform firmware is missing info)
lanV4Subnet = "10.5.5";
lanV4Self = "${lanV4Subnet}.1";
fullHostName = "${config.networking.hostName}.${config.networking.domain}";
btrfsOpts = [ "rw" "noatime" "compress=zstd" "space_cache=v2" "noatime" "autodefrag" ];
btrfsSsdOpts = btrfsOpts ++ [ "ssd" "discard=async" ];
btrfsHddOpts = btrfsOpts;
netFqdn = "home.moonstruck.dev";
lanULA = "fd79:fc8d:af3a:ad8b::";
selfULA = "${lanULA}1";
nameservers = [
# quad9
"9.9.9.9"
"149.112.112.112"
"2620:fe::fe"
"2620:fe::9"
# cloudflare
"1.1.1.1"
"1.0.0.1"
"2606:4700:4700::1111"
"2606:4700:4700::1001"
];
# cloudVPNInterface = "wg0-cloud";
# swapsVPNInterface = "wg1-swaps";
# vpnInterfaces = [ ];
#lanBridge = "br0.lan";
swap = "/dev/disk/by-partlabel/${name}_swap";
in
{
imports = [
./samba.nix
];
config = {
sconfig.machineId = "62df49c6dd7668e60028ed7c7f8b009d";
system.stateVersion = "22.11";
boot.kernelParams = [ ];
boot.kernelPackages = lib.mkForce pkgs.linuxPackages;
boot.initrd.kernelModules = [ ];
# lib.mkForce is important here, want to make sure service modules
# don't open ports to the outside world
networking = lib.mkForce {
inherit nameservers;
hostName = "${name}-nixos";
domain = netFqdn;
useDHCP = false;
nat = {
enable = true;
externalInterface = wanInterface;
internalInterfaces = [ lanBridge ];
};
useNetworkd = true;
networkmanager.enable = false;
extraHosts = ''
${lanV4Self} ${fullHostName}
'';
firewall = {
enable = true;
logReversePathDrops = true;
logRefusedConnections = true;
logRefusedPackets = true;
logRefusedUnicastsOnly = false;
rejectPackets = true;
trustedInterfaces = [ lanBridge debugInterface "lo" ];
allowedUDPPorts = [ ];
allowedTCPPorts = [ ];
allowedUDPPortRanges = [ ];
allowedTCPPortRanges = [ ];
};
# not sure whether to use a bridge
# bridges."${lanBridge}" = {
# interfaces = [
# lanInterface
# ];
# };
};
lun.profiles = {
server = true;
emacs = false;
gaming = false;
graphical = false;
};
security.sudo.wheelNeedsPassword = false;
services.resolved.enable = false;
systemd.network = {
wait-online.anyInterface = true;
links."99-default.link.d/offload.conf" = {
linkConfig = {
ReceiveChecksumOffload = false;
TransmitChecksumOffload = false;
TCPSegmentationOffload = false;
TCP6SegmentationOffload = false;
GenericSegmentationOffload = false;
GenericReceiveOffload = false;
LargeReceiveOffload = false;
};
};
networks = {
"wan" = {
name = wanInterface;
networkConfig = {
DHCP = "yes";
Description = "ISP interface";
IPv6AcceptRA = true;
};
linkConfig = {
RequiredForOnline = "routable";
};
dhcpV6Config = {
PrefixDelegationHint = "::/48";
};
ipv6PrefixDelegationConfig = {
Managed = true;
};
};
"dbg" = {
name = debugInterface;
networkConfig = {
DHCP = "yes";
IPv6AcceptRA = true;
ConfigureWithoutCarrier = "yes";
};
};
"lanBridge" = {
name = lanBridge;
addresses = [
{ addressConfig.Address = "${selfULA}/64"; }
{ addressConfig.Address = "${lanV4Self}/24"; }
];
networkConfig = {
DHCP = "no";
Description = "LAN interface";
# the client shouldn't be allowed to send us RAs, that would be weird.
IPv6AcceptRA = false;
IPv6SendRA = true;
ConfigureWithoutCarrier = "yes";
# Just delegate prefixes from the DHCPv6 PD pool.
# If you also want to distribute a local ULA prefix you want to
# set this to `yes` as that includes both static prefixes as well
# as PD prefixes.
DHCPPrefixDelegation = "yes";
};
# finally "act as router" (according to systemd.network(5))
ipv6PrefixDelegationConfig = {
RouterLifetimeSec = 300; # required as otherwise no RA's are being emitted
# In a production environment you should consider setting these as well:
EmitDNS = true;
EmitDomains = true;
DNS = "fe80::1"; # or whatever "well known" IP your router will have on the inside.
};
# Add ULA prefix
ipv6Prefixes = [
{
ipv6PrefixConfig = {
Prefix = "${lanULA}/64";
AddressAutoconfiguration = true;
PreferredLifetimeSec = 1800;
ValidLifetimeSec = 1800;
};
}
];
};
} // (lib.optionalAttrs useLanBridge {
"lan" = {
name = lanInterface;
networkConfig = {
DHCP = "no";
Bridge = lanBridge;
};
};
});
} // (lib.optionalAttrs useLanBridge {
netdevs = {
bridge = {
netdevConfig = {
Name = lanBridge;
Kind = "bridge";
};
extraConfig = ''
[Bridge]
DefaultPVID=none
VLANFiltering=yes
'';
};
};
});
systemd.targets.network-online.wants = [ "systemd-networkd-wait-online.service" ];
systemd.services.dnsmasq.wants = [ "network-online.target" ];
systemd.services.avahi.wants = [ "network-online.target" ];
systemd.services.miniupnpd.wants = [ "network-online.target" ];
systemd.services.lldpd.wants = [ "network-online.target" ];
# TODO: consider
# unbound?
# https://old.reddit.com/r/NixOS/comments/innzkw/pihole_style_adblock_with_nix_and_unbound/g48o0qb/
# dnsmasq handles dhcp and dns
services.dnsmasq = {
enable = true;
settings = {
domain-needed = true;
bogus-priv = true;
no-resolv = true;
stop-dns-rebind = true;
server = nameservers;
cache-size = 10000;
expand-hosts = true;
domain = netFqdn;
local = "/${netFqdn}/";
# FIXME: need to listen on V6
listen-address = "::1,127.0.0.1,${lanV4Self}";
dhcp-authoritative = true;
dhcp-range = "${lanV4Subnet}.50,${lanV4Subnet}.254,24h";
dhcp-option = [
"option:router,${lanV4Self}"
"option:mtu,1500"
];
# static IPs:
# dhcp-host = [
# "MAC,ipaddr"
# ];
};
};
boot.kernel.sysctl = {
"net.ipv4.conf.all.forwarding" = true;
"net.ipv6.conf.all.forwarding" = true;
"net.ipv4.tcp_ecn" = 1;
# source: https://github.com/mdlayher/homelab/blob/master/nixos/routnerr-2/configuration.nix#L52
# By default, not automatically configure any IPv6 addresses.
"net.ipv6.conf.all.accept_ra" = 0;
"net.ipv6.conf.all.autoconf" = 0;
"net.ipv6.conf.all.use_tempaddr" = 0;
# On WAN, allow IPv6 autoconfiguration and tempory address use.
"net.ipv6.conf.${wanInterface}.accept_ra" = 2;
"net.ipv6.conf.${wanInterface}.autoconf" = 1;
"net.ipv6.conf.${debugInterface}.accept_ra" = 2;
"net.ipv6.conf.${debugInterface}.autoconf" = 1;
};
# services.corerad = {
# enable = true;
# settings = {
# debug = {
# address = "localhost:9430";
# prometheus = true; # enable prometheus metrics
# };
# interfaces = [
# {
# name = lanInterface;
# advertise = true;
# prefix = [
# { prefix = "::/64"; }
# { prefix = "${lanULA}::/64"; }
# ];
# }
# ];
# };
# };
services.miniupnpd = {
enable = true;
externalInterface = wanInterface;
internalIPs = [ lanBridge ];
natpmp = true;
upnp = true;
};
services.avahi = lib.mkForce {
enable = true;
allowInterfaces = [ lanBridge ];
ipv4 = true;
ipv6 = true;
reflector = true;
};
# https://xeiaso.net/blog/prometheus-grafana-loki-nixos-2020-11-20
services.grafana = {
enable = true;
settings = {
server = {
http_addr = "0.0.0.0"; # FIXME: one interface only?
http_port = 8888;
};
};
dataDir = "/var/lib/grafana";
};
lun.persistence.dirs = [ "/var/lib/dnsmasq" "/var/lib/grafana" "/var/tmp" "/tmp" "/persist/thoth/ftmp" ];
services.lldpd.enable = true;
lun.home-assistant.enable = true;
services.plex = {
enable = true;
openFirewall = true;
dataDir = "/persist/plex/";
};
systemd.services.nightly-reboot = {
description = "Machine reboot";
serviceConfig = {
type = "oneshot";
ExecStart = "${pkgs.systemd}/bin/systemctl --force reboot";
};
startAt = "*-*-* 06:00:00";
};
# spin down hdds
powerManagement.powerUpCommands = with pkgs;''
${bash}/bin/bash -c "${hdparm}/bin/hdparm -S 9 -B 127 $(${utillinux}/bin/lsblk -dnp -o name,rota |${gnugrep}/bin/grep \'.*\\s1\'|${coreutils}/bin/cut -d \' \' -f 1)"
'';
boot.kernelModules = [ "tcp_bbr" ];
boot.kernel.sysctl = {
"net.ipv4.tcp_congestion_control" = lib.mkForce "bbr"; # apparently this works for ipv6 too
"net.core.default_qdisc" = lib.mkForce "fq_codel"; # FIXME doesn't apply to all nics, set too late in boot?
};
hardware.cpu.amd.updateMicrocode = true;
users.mutableUsers = false;
systemd.targets.getty.wants = [ "journal@tty12.service" ];
systemd.services."thoth-lun" =
let
thothPython = pkgs.python3.withPackages (_: [
flakeArgs.thoth-reminder-bot.packages.${pkgs.system}.thoth
]);
thothWorkingDirectory = "/persist/thoth/ftmp/";
thothEnv = "${thothWorkingDirectory}secrets.env";
in
{
enable = true;
description = "Thoth reminder bot instance managed by @lun";
wantedBy = [ "multi-user.target" ];
wants = [
"network-online.target"
"dnsmasq.service"
];
unitConfig = {
ConditionPathExists = thothEnv;
StartLimitBurst = 5;
StartLimitIntervalSec = 30;
};
serviceConfig = {
EnvironmentFile = thothEnv;
ExecStart = ''
${pkgs.bash}/bin/bash -c "cd ${thothWorkingDirectory}; sleep 2; ${thothPython}/bin/python -c 'import thoth.main; thoth.main.start()'"
'';
Type = "exec";
RestartSec = 30;
TemporaryFileSystem = "/persist/";
BindPaths = thothWorkingDirectory;
PrivateTmp = true;
Restart = "always";
ProtectSystem = true;
ProtectHome = true;
};
};
lun.persistence.enable = true;
fileSystems = {
"/" = {
device = "tmpfs";
fsType = "tmpfs";
options = [ "defaults" "size=2G" "mode=755" ];
};
"/boot" = {
device = "/dev/disk/by-partlabel/${name}_esp";
fsType = "vfat";
neededForBoot = true;
options = [ "discard" "noatime" ];
};
"/persist" = {
device = "/dev/disk/by-partlabel/${name}_persist";
fsType = "btrfs";
neededForBoot = true;
options = btrfsSsdOpts ++ [ "subvol=@persist" "nodev" "nosuid" ];
};
"/nix" = {
neededForBoot = true;
};
"/mnt/_nas0" = {
fsType = "btrfs";
device = "/dev/disk/by-partlabel/_nas0";
neededForBoot = false;
options = btrfsHddOpts ++ [ "nofail" ];
};
};
swapDevices = lib.mkForce [
{ device = swap; }
];
boot.resumeDevice = swap;
};
}