flakes/modules/system/wireguard.nix

141 lines
4.2 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, gcSecrets, ... }:
2021-09-20 14:45:07 +00:00
let
port = 51820;
serverName = "dandelion";
serverInterface = "enp0s6";
serverIp = gcSecrets.wireguard.gateway;
2021-09-20 14:45:07 +00:00
forwarding = {
# "22727" = [ "10.100.0.3" "7777" ];
};
mapForwards = type:
builtins.concatStringsSep "\n" (
lib.mapAttrsToList (sport: tuple:
let
dest = builtins.head tuple;
dport = lib.last tuple;
in ''
${pkgs.iptables}/bin/iptables -${type} PREROUTING -t nat -i ${serverInterface} -p tcp --dport ${sport} -j DNAT --to ${dest}:${dport}
${pkgs.iptables}/bin/iptables -${type} FORWARD -p tcp -d ${dest} --dport ${dport} -j ACCEPT
'') forwarding
);
routeBypass = {
anemone = {
interface = "wlp1s0";
routes = [ serverIp ];
};
2023-01-28 00:12:28 +07:00
hyacinth = {
interface = "enp5s0";
routes = [ serverIp ];
};
};
2021-09-20 14:45:07 +00:00
clients = {
# caramel = {
# publicKey = "VDqcpS0lJzFgwikj61MJ1xc9P8Cuq0NXa+Hc+etn2iA=";
# allowedIPs = [ "10.100.0.2/32" ];
# };
2023-01-28 00:12:28 +07:00
hyacinth = {
2021-09-20 14:45:07 +00:00
publicKey = "6nVhazYdmC15A/nke9VrqIg3sOBVOmqj4GEsyBq7MVo=";
allowedIPs = [ "10.100.0.3/32" "${gcSecrets.wireguard.ipv6Subnet}:3"];
2021-09-20 14:45:07 +00:00
};
anemone = {
2025-05-20 02:50:41 +10:00
publicKey = "px5+JNdAmqBvUC++DhiJrUBRAr+BYP6iYVt4sbhPTWY=";
allowedIPs = [ "10.100.0.4/32" "${gcSecrets.wireguard.ipv6Subnet}:4" ];
2021-10-07 11:45:54 +07:00
};
hibiscus = {
publicKey = "vQ5a2KMrwi7RCRsD0yvog+n35vQYFuvwiPn+W4lbRBw=";
allowedIPs = [ "10.100.0.5/32" "${gcSecrets.wireguard.ipv6Subnet}:5" ];
2022-12-02 23:19:53 +07:00
};
2021-09-20 14:45:07 +00:00
};
clientPeers = builtins.attrValues clients;
serverPeer = {
publicKey = "3ugIk2tQZXjAH9/95s63ld2WNUHQrd4Mz5jzbln6oj0=";
allowedIPs = [ "0.0.0.0/0" "::/0" ];
2022-02-02 20:50:59 +07:00
endpoint = "${serverIp}:${toString port}";
2021-09-20 14:45:07 +00:00
persistentKeepalive = 25;
};
serverConfig = {
nat = {
enable = true;
externalInterface = serverInterface;
internalInterfaces = [ "wg0" ];
};
firewall = {
allowedTCPPorts = (builtins.map (s: lib.strings.toInt s) (builtins.attrNames forwarding));
allowedUDPPorts = [ port ];
};
2021-09-20 14:45:07 +00:00
wireguard.interfaces.wg0 = {
ips = [ "10.100.0.1/24" "${gcSecrets.wireguard.ipv6Subnet}:1" ];
listenPort = port;
2021-09-20 14:45:07 +00:00
postSetup = ''
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
${mapForwards "A"}
2021-09-20 14:45:07 +00:00
'';
postShutdown = ''
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
${mapForwards "D"}
2021-09-20 14:45:07 +00:00
'';
privateKeyFile = config.age.secrets."wg_${serverName}".path;
peers = clientPeers;
};
};
clientConfig = {
2022-02-02 20:50:59 +07:00
wireguard.interfaces.wg0 =
let
client = clients."${config.networking.hostName}";
routes = routeBypass."${config.networking.hostName}";
mapRoutes = type: lib.concatMapStringsSep "\n" (r: "${pkgs.iproute2}/bin/ip route ${type} ${r} dev ${routes.interface}") routes.routes;
2022-02-02 20:50:59 +07:00
in {
ips = client.allowedIPs;
listenPort = port;
2021-09-20 14:45:07 +00:00
postSetup = ''
${mapRoutes "add"}
2022-02-02 20:50:59 +07:00
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
'';
postShutdown = ''
${mapRoutes "del"}
2022-02-02 20:50:59 +07:00
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
'';
2021-09-20 14:45:07 +00:00
privateKeyFile = config.age.secrets."wg_${config.networking.hostName}".path;
peers = [ serverPeer ];
};
};
clientQuickConfig = {
wg-quick.interfaces =
let
client = clients."${config.networking.hostName}";
in {
wg0 = {
address = client.allowedIPs;
privateKeyFile = config.age.secrets."wg_${config.networking.hostName}".path;
peers = [ serverPeer ];
};
};
};
2021-09-20 14:45:07 +00:00
in {
boot.kernel.sysctl = lib.mkIf (config.networking.hostName == serverName) ({
"net.ipv6.conf.all.forwarding" = true;
"net.ipv6.conf.default.forwarding" = true;
});
2021-09-20 14:45:07 +00:00
networking =
lib.mkMerge [
(lib.mkIf (config.networking.hostName == serverName) serverConfig)
#(lib.mkIf (builtins.hasAttr config.networking.hostName clients) clientConfig)
(lib.mkIf (builtins.hasAttr config.networking.hostName clients) clientQuickConfig)
2021-09-20 14:45:07 +00:00
];
}