panacea/modules/networking.nix

140 lines
3.1 KiB
Nix
Raw Permalink Normal View History

2024-11-19 14:18:32 +01:00
{
config,
lib,
pkgs,
...
}:
2024-11-19 14:18:32 +01:00
let
wireguard_port = "1194";
2022-09-10 09:11:26 +02:00
2024-11-19 14:18:32 +01:00
in
{
# Set hostname, hostid and enable WiFi
networking = {
hostName = "panacea";
2020-05-08 21:53:52 +02:00
hostId = "8feb0bb8";
wireless.iwd.enable = true;
};
2022-03-28 18:08:23 +02:00
# Enable systemd-networkd
networking = {
useDHCP = false;
interfaces = {
enp0s31f6.useDHCP = true;
wlan0.useDHCP = true;
};
useNetworkd = true;
dhcpcd.enable = false;
};
systemd.network.wait-online.enable = false;
2022-03-28 18:08:23 +02:00
2023-08-25 02:43:06 +02:00
# Enable mDNS
2023-01-30 02:01:30 +01:00
services.resolved = {
enable = true;
llmnr = "false";
extraConfig = ''
MulticastDNS=yes
'';
};
2022-08-16 13:12:34 +02:00
2022-04-29 18:12:46 +02:00
# Prioritize ethernet over WiFi
2023-01-30 02:01:30 +01:00
systemd.network.networks."40-enp0s31f6" = {
dhcpV4Config.RouteMetric = 10;
networkConfig.MulticastDNS = "yes";
};
systemd.network.networks."40-wlan0" = {
dhcpV4Config.RouteMetric = 20;
networkConfig.MulticastDNS = "yes";
};
2022-04-29 18:12:46 +02:00
2022-03-28 18:08:23 +02:00
# Static IP for home network
systemd.network.networks."24-home" = {
name = "wlan0";
matchConfig = {
Name = "wlan0";
2023-12-02 19:56:58 +01:00
SSID = "Zingarata";
2022-03-28 18:08:23 +02:00
};
2022-07-12 20:49:08 +02:00
address = [ "192.168.13.131/24" ];
gateway = [ "192.168.13.1" ];
dns = [ "192.168.13.2" ];
networkConfig.MulticastDNS = "yes";
};
2022-08-03 13:52:02 +02:00
2023-07-07 12:17:34 +02:00
systemd.network.networks."25-home" = {
name = "wlan0";
matchConfig = {
Name = "wlan0";
SSID = "Aminkas-5Ghz";
};
address = [ "192.168.13.3/24" ];
gateway = [ "192.168.13.1" ];
dns = [ "192.168.13.2" ];
networkConfig.MulticastDNS = "yes";
2023-07-07 12:17:34 +02:00
};
2022-08-03 13:52:02 +02:00
# VPN setup
systemd.network.netdevs."wg0" = {
netdevConfig = {
Kind = "wireguard";
Name = "wg0";
};
2022-09-10 09:11:26 +02:00
wireguardConfig = {
ListenPort = wireguard_port;
PrivateKeyFile = config.age.secrets.wireguard.path;
FirewallMark = 34952;
};
2024-11-19 14:18:32 +01:00
wireguardPeers = [
{
PublicKey = "GN8lqPBZYOulh6xD4GhkoEWI65HMMCpSxJSH5871YnU=";
AllowedIPs = [ "0.0.0.0/0" ];
Endpoint = "coolneng.duckdns.org:1194";
}
];
2022-08-03 13:52:02 +02:00
};
systemd.network.networks."wg0" = {
matchConfig.Name = "wg0";
2022-09-10 09:11:26 +02:00
linkConfig.ActivationPolicy = "manual";
2022-08-03 13:52:02 +02:00
networkConfig = {
Address = "10.8.0.2/32";
DNS = "10.8.0.1";
2022-09-10 09:11:26 +02:00
DNSDefaultRoute = true;
2022-08-03 13:52:02 +02:00
};
2024-11-19 14:18:32 +01:00
routingPolicyRules = [
{
FirewallMark = 34952;
InvertRule = true;
Table = 1000;
Priority = 10;
}
];
routes = [
{
Gateway = "10.8.0.1";
GatewayOnLink = true;
Table = 1000;
}
];
2022-08-03 13:52:02 +02:00
};
2022-09-04 18:35:36 +02:00
# Firewall configuration
networking.firewall = {
allowedTCPPorts = [
9090 # Calibre Wireless
];
allowedUDPPorts = [
54982 # Calibre Wireless
2023-01-30 02:01:30 +01:00
5353 # mDNS
2022-09-04 18:35:36 +02:00
];
2022-09-10 09:11:26 +02:00
# Allow wireguard traffic
extraCommands = ''
iptables -t mangle -I nixos-fw-rpfilter -p udp -m udp --sport ${wireguard_port} -j RETURN
iptables -t mangle -I nixos-fw-rpfilter -p udp -m udp --dport ${wireguard_port} -j RETURN
2022-09-10 09:11:26 +02:00
'';
extraStopCommands = ''
iptables -t mangle -D nixos-fw-rpfilter -p udp -m udp --sport ${wireguard_port} -j RETURN || true
iptables -t mangle -D nixos-fw-rpfilter -p udp -m udp --dport ${wireguard_port} -j RETURN || true
2022-09-10 09:11:26 +02:00
'';
2022-09-04 18:35:36 +02:00
};
}