dotfiles/systems/ceres/config/wireguard.nix

95 lines
2.4 KiB
Nix
Raw Normal View History

2025-07-01 15:39:57 -05:00
{
config,
flake,
pkgs,
...
}:
2025-07-01 04:11:32 -05:00
let
2025-07-01 13:54:00 -05:00
inherit (flake.config.services.instances) wireGuard;
2025-07-01 04:32:00 -05:00
inherit (flake.config.machines.devices) mars ceres;
2025-07-01 04:11:32 -05:00
service = wireGuard;
2025-07-01 15:39:57 -05:00
hostIP = "${ceres.wireguard.ip0}/24";
2025-07-01 04:11:32 -05:00
in
{
networking = {
firewall = {
2025-07-01 15:39:57 -05:00
allowedTCPPorts = [
service.ports.port0
];
2025-07-01 04:11:32 -05:00
allowedUDPPorts = [
service.ports.port0
2025-07-01 13:54:00 -05:00
service.ports.port1
2025-07-01 04:11:32 -05:00
];
};
nat = {
enable = true;
enableIPv6 = true;
externalInterface = "eth0";
internalInterfaces = [ "wg0" ];
};
2025-07-01 15:39:57 -05:00
wg-quick.interfaces = {
2025-07-01 04:11:32 -05:00
wg0 = {
2025-07-01 15:39:57 -05:00
address = [
hostIP
"fdc9:281f:04d7:9ee9::1/64"
];
2025-07-01 13:54:00 -05:00
listenPort = service.ports.port1;
2025-07-01 04:11:32 -05:00
privateKeyFile = config.sops.secrets."${service.name}-private".path;
2025-07-01 15:39:57 -05:00
postUp = ''
${pkgs.iptables}/bin/iptables -A FORWARD -i wg0 -j ACCEPT
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s ${hostIP} -o eth0 -j MASQUERADE
${pkgs.iptables}/bin/ip6tables -A FORWARD -i wg0 -j ACCEPT
${pkgs.iptables}/bin/ip6tables -t nat -A POSTROUTING -s fdc9:281f:04d7:9ee9::1/64 -o eth0 -j MASQUERADE
'';
# Undo the above
preDown = ''
${pkgs.iptables}/bin/iptables -D FORWARD -i wg0 -j ACCEPT
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -s ${hostIP} -o eth0 -j MASQUERADE
${pkgs.iptables}/bin/ip6tables -D FORWARD -i wg0 -j ACCEPT
${pkgs.iptables}/bin/ip6tables -t nat -D POSTROUTING -s fdc9:281f:04d7:9ee9::1/64 -o eth0 -j MASQUERADE
'';
2025-07-01 04:11:32 -05:00
peers = [
{
publicKey = "9zfRPxkxTLHM9tABC8lIaDMrzdjcF2l1mtG82uqGKUQ=";
2025-07-01 15:39:57 -05:00
presharedKeyFile = config.sops.secrets."${service.name}-mars-public".path;
allowedIPs = [
"${mars.wireguard.ip0}/32"
"fdc9:281f:04d7:9ee9::2/128"
];
2025-07-01 04:11:32 -05:00
}
];
};
};
};
2025-07-01 04:16:52 -05:00
sops =
2025-07-01 04:11:32 -05:00
let
sopsPath = secret: {
2025-07-01 04:16:52 -05:00
path = "${service.sops.path0}/${service.name}-${secret}-pass";
2025-07-01 04:11:32 -05:00
owner = "root";
mode = "600";
};
in
2025-07-01 04:16:52 -05:00
{
secrets = builtins.listToAttrs (
map
(secret: {
name = "${service.name}-${secret}";
value = sopsPath secret;
})
[
"private"
"public"
2025-07-01 15:39:57 -05:00
"mars-public"
2025-07-01 04:16:52 -05:00
]
);
};
2025-07-01 04:11:32 -05:00
boot.kernel.sysctl = {
"net.ipv4.ip_forward" = 1;
};
}