nixos/hosts/router.home.ts.hillion.co.uk/default.nix

329 lines
9.4 KiB
Nix
Raw Normal View History

2023-05-09 20:20:29 +01:00
{ config, pkgs, lib, ... }:
{
imports = [
./hardware-configuration.nix
];
config = {
system.stateVersion = "22.11";
networking.hostName = "router";
networking.domain = "home.ts.hillion.co.uk";
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
2023-05-10 20:09:58 +01:00
boot.kernel.sysctl = {
"net.ipv4.conf.all.forwarding" = true;
};
custom.defaults = true;
2024-02-07 21:54:26 +00:00
## Interactive password
custom.users.jake.password = true;
2023-07-23 17:28:58 +01:00
## Impermanence
custom.impermanence.enable = true;
2023-05-10 20:09:58 +01:00
## Networking
networking = {
firewall.enable = lib.mkForce false;
nat.enable = lib.mkForce false;
useDHCP = false;
interfaces = {
enp1s0 = {
name = "eth0";
macAddress = "b4:fb:e4:b0:90:3c";
useDHCP = true;
};
enp2s0 = {
name = "eth1";
ipv4.addresses = [
{
address = "10.64.50.1";
prefixLength = 24;
}
];
};
enp3s0 = {
name = "eth2";
ipv4.addresses = [
{
address = "10.239.19.1";
prefixLength = 24;
}
];
};
enp4s0 = { name = "eth3"; };
enp5s0 = { name = "eth4"; };
enp6s0 = { name = "eth5"; };
};
nftables = {
enable = true;
ruleset = ''
table inet filter {
chain output {
type filter hook output priority 100; policy accept;
}
chain input {
type filter hook input priority filter; policy drop;
# Allow trusted networks to access the router
iifname {
"lo",
2023-05-10 20:09:58 +01:00
"eth1",
"eth2",
"tailscale0",
} counter accept
ip protocol icmp counter accept comment "accept all ICMP types"
iifname "eth0" ct state { established, related } counter accept
iifname "eth0" drop
}
chain forward {
type filter hook forward priority filter; policy drop;
iifname {
"eth1",
"eth2",
} oifname {
"eth0",
} counter accept comment "Allow trusted LAN to WAN"
iifname {
"eth0",
} oifname {
"eth1",
"eth2",
} ct state established,related counter accept comment "Allow established back to LANs"
2023-07-22 19:57:48 +01:00
ip daddr 10.64.50.20 tcp dport 32400 counter accept comment "Plex"
ip daddr 10.64.50.20 tcp dport 8444 counter accept comment "Chia"
2024-04-22 20:49:43 +01:00
ip daddr 10.64.50.21 tcp dport 7654 counter accept comment "Tang"
2023-05-10 20:09:58 +01:00
}
}
table ip nat {
chain prerouting {
type nat hook prerouting priority filter; policy accept;
2023-07-22 19:57:48 +01:00
iifname eth0 tcp dport 32400 counter dnat to 10.64.50.20
iifname eth0 tcp dport 8444 counter dnat to 10.64.50.20
2024-04-22 20:49:43 +01:00
iifname eth0 tcp dport 7654 counter dnat to 10.64.50.21
2023-05-10 20:09:58 +01:00
}
chain postrouting {
type nat hook postrouting priority filter; policy accept;
oifname "eth0" masquerade
}
}
'';
};
};
services = {
2024-02-07 23:09:06 +00:00
kea = {
dhcp4 = {
enable = true;
settings = {
interfaces-config = {
interfaces = [ "eth1" "eth2" ];
};
lease-database = {
type = "memfile";
2024-05-20 20:41:25 +01:00
persist = true;
name = "/var/lib/kea/dhcp4.leases";
2024-02-07 23:09:06 +00:00
};
option-def = [
{
name = "cookie";
space = "vendor-encapsulated-options-space";
code = 1;
type = "string";
array = false;
}
];
client-classes = [
{
name = "APC";
test = "option[vendor-class-identifier].text == 'APC'";
option-data = [
{
always-send = true;
name = "vendor-encapsulated-options";
}
{
name = "cookie";
space = "vendor-encapsulated-options-space";
code = 1;
data = "1APC";
}
];
}
];
2024-02-07 23:09:06 +00:00
subnet4 = [
{
subnet = "10.64.50.0/24";
interface = "eth1";
pools = [{
pool = "10.64.50.64 - 10.64.50.254";
}];
option-data = [
{
name = "routers";
data = "10.64.50.1";
}
{
name = "broadcast-address";
data = "10.64.50.255";
}
{
name = "domain-name-servers";
2024-04-26 19:29:13 +01:00
data = "10.64.50.1, 1.1.1.1, 8.8.8.8";
2024-02-07 23:09:06 +00:00
}
];
2024-05-20 20:41:25 +01:00
reservations = lib.lists.imap0
(i: el: {
ip-address = "10.64.50.${toString (20 + i)}";
inherit (el) hw-address hostname;
}) [
{ hostname = "tywin"; hw-address = "c8:7f:54:6d:e1:03"; }
{ hostname = "microserver"; hw-address = "e4:5f:01:b4:58:95"; }
{ hostname = "theon"; hw-address = "00:1e:06:49:06:1e"; }
{ hostname = "server-switch"; hw-address = "84:d8:1b:9d:0d:85"; }
{ hostname = "apc-ap7921"; hw-address = "00:c0:b7:6b:f4:34"; }
2024-06-10 21:29:21 +01:00
{ hostname = "sodium"; hw-address = "d8:3a:dd:c3:d6:2b"; }
2024-02-07 23:09:06 +00:00
];
}
{
subnet = "10.239.19.0/24";
interface = "eth2";
pools = [{
pool = "10.239.19.64 - 10.239.19.254";
}];
option-data = [
{
name = "routers";
data = "10.239.19.1";
}
{
name = "broadcast-address";
data = "10.239.19.255";
}
{
name = "domain-name-servers";
2024-04-26 19:29:13 +01:00
data = "10.239.19.1, 1.1.1.1, 8.8.8.8";
2024-02-07 23:09:06 +00:00
}
];
reservations = [
{
# bedroom-everything-presence-one
hw-address = "40:22:d8:e0:1d:50";
ip-address = "10.239.19.2";
hostname = "bedroom-everything-presence-one";
}
{
# living-room-everything-presence-one
hw-address = "40:22:d8:e0:0f:78";
ip-address = "10.239.19.3";
hostname = "living-room-everything-presence-one";
}
];
}
];
};
};
2023-05-10 20:09:58 +01:00
};
2024-04-26 19:29:13 +01:00
unbound = {
enable = true;
settings = {
server = {
interface = [
"127.0.0.1"
"10.64.50.1"
"10.239.19.1"
];
access-control = [
"10.64.50.0/24 allow"
"10.239.19.0/24 allow"
];
};
forward-zone = [
{
name = ".";
forward-tls-upstream = "yes";
forward-addr = [
"1.1.1.1#cloudflare-dns.com"
"1.0.0.1#cloudflare-dns.com"
"8.8.8.8#dns.google"
"8.8.4.4#dns.google"
];
}
];
};
};
2023-05-10 20:09:58 +01:00
};
2023-05-09 20:20:29 +01:00
## Tailscale
age.secrets."tailscale/router.home.ts.hillion.co.uk".file = ../../secrets/tailscale/router.home.ts.hillion.co.uk.age;
services.tailscale = {
2023-05-09 20:20:29 +01:00
enable = true;
authKeyFile = config.age.secrets."tailscale/router.home.ts.hillion.co.uk".path;
2023-05-09 20:20:29 +01:00
};
## Enable btrfs compression
fileSystems."/data".options = [ "compress=zstd" ];
fileSystems."/nix".options = [ "compress=zstd" ];
2023-05-10 20:09:58 +01:00
## Run a persistent iperf3 server
services.iperf3.enable = true;
## Zigbee2Mqtt
custom.services.zigbee2mqtt.enable = true;
2023-08-03 21:20:55 +01:00
## Netdata
services.netdata = {
enable = true;
config = {
web = {
"bind to" = "unix:/run/netdata/netdata.sock";
};
};
};
services.caddy = {
enable = true;
virtualHosts."http://graphs.router.home.ts.hillion.co.uk" = {
listenAddresses = [ config.custom.dns.tailscale.ipv4 config.custom.dns.tailscale.ipv6 ];
2023-08-03 21:20:55 +01:00
extraConfig = "reverse_proxy unix///run/netdata/netdata.sock";
};
};
2024-02-04 10:29:07 +00:00
users.users.caddy.extraGroups = [ "netdata" ];
### HACK: Allow Caddy to restart if it fails. This happens because Tailscale
### is too late at starting. Upstream nixos caddy does restart on failure
### but it's prevented on exit code 1. Set the exit code to 0 (non-failure)
### to override this.
systemd.services.caddy = {
requires = [ "tailscaled.service" ];
after = [ "tailscaled.service" ];
serviceConfig = {
RestartPreventExitStatus = lib.mkForce 0;
};
2023-08-03 21:47:21 +01:00
};
2023-05-09 20:20:29 +01:00
};
}