Last active
October 1, 2025 01:52
-
-
Save WhiteHusky/680c1f80c370c87703888a79e64cfa29 to your computer and use it in GitHub Desktop.
Private WireGuard VPN for home access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { config, lib, pkgs, ... }: | |
| { | |
| # Opening ports to the public, permitting the tunnel to connect regardless. | |
| networking.firewall = { | |
| enable = true; | |
| trustedInterfaces = [ | |
| "wg-home" | |
| ]; | |
| allowedUDPPorts = [ | |
| # 51820 | |
| 51821 | |
| ]; | |
| }; | |
| # NAT is needed so VPN clients can connect to the internet "as" the server | |
| networking.nat = { | |
| enable = true; | |
| internalInterfaces = [ "wg-home" ]; | |
| externalInterface = "enp0s6"; | |
| /* You can use this to expose services at home to the public | |
| forwardPorts = [ | |
| { | |
| sourcePort = 7245; | |
| proto = "tcp"; | |
| destination = "10.10.10.1:7245"; | |
| } | |
| ]; | |
| */ | |
| }; | |
| # DNS Caching, optional but helps optimize DNS requests while wanting to be able to resolve LAN hostnames | |
| services.bind = { | |
| enable = true; | |
| # Allow these networks (localhost, our VPN) to query for cached requests | |
| cacheNetworks = [ | |
| "127.0.0.0/24" | |
| "::1/128" | |
| "10.10.10.0/24" | |
| ]; | |
| forward = "first"; | |
| # Who resolves requests for us and how it is done | |
| forwarders = [ | |
| "9.9.9.9 tls quad9-tls" | |
| "149.112.112.112 tls quad9-tls" | |
| "2620:fe::fe tls quad9-tls" | |
| "2620:fe::9 tls quad9-tls" | |
| ]; | |
| # DNSSEC, except for our home DNS | |
| extraOptions = '' | |
| validate-except { my.home.lan; }; | |
| ''; | |
| # Set up our forwarder's DNS-over-HTTPS and our home DNS | |
| extraConfig = '' | |
| tls quad9-tls { remote-hostname "dns.quad9.net"; }; | |
| zone "my.home.lan" { | |
| type forward; | |
| forward only; | |
| forwarders { 10.10.10.101; }; | |
| }; | |
| ''; | |
| }; | |
| # The actual Wireguard VPN configuration | |
| networking.wireguard.interfaces."wg-home" = { | |
| # Required so the VPS can route traffic coming in from the VPN | |
| postSetup = '' | |
| ${lib.getExe pkgs.sysctl} -w net.ipv4.conf.wg-home.forwarding=1 | |
| ''; | |
| preShutdown = '' | |
| ${lib.getExe pkgs.sysctl} -w net.ipv4.conf.wg-home.forwarding=0 | |
| ''; | |
| ips = [ "10.10.10.100/24" ]; | |
| listenPort = 51821; | |
| privateKeyFile = "/etc/nixos/wg-home.key"; | |
| peers = [ | |
| { | |
| # Home firewall | |
| publicKey = "AAAA"; | |
| allowedIPs = [ | |
| "10.10.10.101/32" | |
| # Home subnet | |
| "10.20.10.0/24" | |
| ]; | |
| } | |
| { | |
| # Other devices... | |
| publicKey = "BBBB"; | |
| allowedIPs = ["10.10.10.1/32"]; | |
| } | |
| ]; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment