WireGuard VPN

WireGuard provides a secure, low-latency VPN between all hosts. Server acts as the hub with a public endpoint; desktop and laptop connect to it.

Network Topology

Host WireGuard IP Role
Server 10.100.0.1 Hub (public endpoint)
Desktop 10.100.0.2 Client
Laptop 10.100.0.3 Client

Prerequisites

DNS Record

wg.yhkze.net must resolve directly to the server’s public IP — WireGuard uses UDP and cannot go through the Cloudflare proxy. The record is already configured as DNS-only in Cloudflare DNS.

Port Forwarding

Forward UDP port 30352 to the server (192.168.100.2) on the router. WireGuard clients connect to this port from the internet.

Initial Setup

Generate Keys

Generate a keypair on each host:

nix-shell -p wireguard-tools
cd /tmp
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
cat privatekey
cat publickey

Save the private key securely and note the public key for the next steps.

Add Public Keys to Configuration

Edit values/wireguard-keys.nix with the public keys from each host:

{
  server = "SERVER_PUBLIC_KEY_HERE";
  desktop = "DESKTOP_PUBLIC_KEY_HERE";
  laptop = "LAPTOP_PUBLIC_KEY_HERE";
}

Add Private Keys to SOPS

On each host, add the private key to SOPS:

sops hosts/<hostname>/secrets/secrets.yaml

Add under system/wireguard/private-key:

system:
    wireguard:
        private-key: YOUR_PRIVATE_KEY_HERE

Deploy

Rebuild NixOS on all hosts:

sudo nixos-rebuild switch --flake "path:.#<hostname>"

Verification

Check Interface Status

ip addr show wg0

Expected output shows the WireGuard interface with assigned IP.

Check Peer Connections

sudo wg show

Shows peer public keys, endpoints, allowed IPs, and last handshake time.

Test Connectivity

From desktop or laptop:

ping 10.100.0.1

From server:

ping 10.100.0.2  # desktop
ping 10.100.0.3  # laptop

Connect Imperatively

  1. Enter Nix shell with required dependencies.

    nix-shell -p wireguard-tools
  2. Create Wireguard configuration file.

    [Interface]
    PrivateKey = <your-private-key>
    Address = 10.100.0.<your-id>/24
    
    [Peer]
    PublicKey = O7sztnEM87WxYjdzbSht+jfIQHd025t21kYEsXUqIlM=
    Endpoint = wg.yhkze.net:30352
    AllowedIPs = 10.100.0.0/24
    PersistentKeepalive = 25
  3. Activate Wireguard interface

    sudo wg-quick up ./wg0.conf

Troubleshooting

No Handshake

If wg show shows no recent handshake:

  1. Verify firewall allows UDP 30352 on server
  2. Check that wg.yhkze.net resolves to server’s public IP (not a proxy)
  3. Verify UDP 30352 is port forwarded on the router
  4. Ensure private keys match their corresponding public keys

Check systemd-networkd logs:

journalctl -u systemd-networkd.service -f

Interface Not Created

Verify SOPS secret is accessible:

sudo cat /run/secrets/system/wireguard/private-key

Check netdev status:

networkctl status wg0

Routes Not Working

Verify routes exist:

ip route | grep 10.100.0

Expected output includes route to 10.100.0.0/24 via wg0.

Key Rotation

To rotate keys:

  1. Generate new keypair on the target host
  2. Update public key in values/wireguard-keys.nix
  3. Update private key in SOPS
  4. Rebuild all hosts (they need the new public key)

References