Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Last active May 22, 2024 09:09
Show Gist options
  • Save Gonzih/298c8bc6e4e3acdb6d984cf2d9d427f0 to your computer and use it in GitHub Desktop.
Save Gonzih/298c8bc6e4e3acdb6d984cf2d9d427f0 to your computer and use it in GitHub Desktop.
NixOs WIREGUARD full traffic forwarding config
{ config, pkgs, ... }:
let
secrets = import /opt/nix/secrets.nix;
externalInterface = "enp2s0";
internalInterface = "wg0";
externalPort = 51820;
externalNetMask = "10.200.200.1/24";
in
{
# enable NAT
networking.nat.enable = true;
networking.nat.externalInterface = externalInterface;
networking.nat.internalInterfaces = [ internalInterface ];
networking.firewall = {
allowedUDPPorts = [ externalPort ];
};
networking.wireguard.interfaces = {
wg0 = {
ips = [ externalNetMask ];
listenPort = externalPort;
privateKey = "${secrets.wireguard.servers.wg.privateKey}";
postSetup = ''
/run/current-system/sw/bin/iptables -A FORWARD -i ${internalInterface} -j ACCEPT
/run/current-system/sw/bin/iptables -A FORWARD -o ${internalInterface} -j ACCEPT
/run/current-system/sw/bin/iptables -t nat -A POSTROUTING -o ${externalInterface} -j MASQUERADE
'';
postShutdown = ''
/run/current-system/sw/bin/iptables -D FORWARD -i ${internalInterface} -j ACCEPT
/run/current-system/sw/bin/iptables -D FORWARD -o ${internalInterface} -j ACCEPT
/run/current-system/sw/bin/iptables -t nat -D POSTROUTING -o ${externalInterface} -j MASQUERADE
'';
peers = [
{
publicKey = "${secrets.wireguard.clients.phone.publicKey}";
allowedIPs = [ "10.200.200.2/32" ];
}
];
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment