flakes/modules/system/wireguard.nix

84 lines
2.4 KiB
Nix
Raw Normal View History

2021-09-20 14:45:07 +00:00
{ config, lib, pkgs, ... }:
let
port = 51820;
serverName = "sugarcane";
serverInterface = "ens3";
2022-02-02 20:50:59 +07:00
serverIp = "51.79.240.130";
2021-09-20 14:45:07 +00:00
clients = {
2021-11-02 00:53:56 +07:00
blossom = {
2021-09-20 14:45:07 +00:00
publicKey = "6nVhazYdmC15A/nke9VrqIg3sOBVOmqj4GEsyBq7MVo=";
allowedIPs = [ "10.100.0.3/32" ];
2022-02-02 20:50:59 +07:00
interface = "wlp3s0";
gateway = "192.168.100.1";
2021-09-20 14:45:07 +00:00
};
2021-10-07 11:45:54 +07:00
strawberry = {
publicKey = "Fkcp/VSN4Dkhly8V4hskF4lnDviA7VZHCnWf7OliFCg=";
allowedIPs = [ "10.100.0.4/32" ];
};
2021-09-20 14:45:07 +00:00
};
clientPeers = builtins.attrValues clients;
serverPeer = {
publicKey = "3ugIk2tQZXjAH9/95s63ld2WNUHQrd4Mz5jzbln6oj0=";
allowedIPs = [ "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 = {
allowedUDPPorts = [ port ];
};
2021-09-20 14:45:07 +00:00
wireguard.interfaces.wg0 = {
ips = [ "10.100.0.1/24" ];
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
'';
postShutdown = ''
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
'';
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}";
in {
ips = client.allowedIPs;
listenPort = port;
2021-09-20 14:45:07 +00:00
2022-02-02 20:50:59 +07:00
postSetup = ''
${pkgs.iproute2}/bin/ip route add ${serverIp} via ${client.gateway} dev ${client.interface}
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
'';
postShutdown = ''
${pkgs.iproute2}/bin/ip route del ${serverIp} via ${client.gateway} dev ${client.interface}
${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 ];
};
};
in {
networking =
lib.mkMerge [
(lib.mkIf (config.networking.hostName == serverName) serverConfig)
(lib.mkIf (builtins.hasAttr config.networking.hostName clients) clientConfig)
];
}