# WireGuard, the admin network (10.20.0.0/24). asxpio-ops is the hub # every peer connects to; everything else is a spoke with ops as its # only peer. Keys come from sops: wireguard/private_key and, per peer, # wireguard/psk/. { config, lib, ... }: let cfg = config.asxpio.wireguard; in { options.asxpio.wireguard = { interface = lib.mkOption { type = lib.types.str; }; address = lib.mkOption { type = lib.types.listOf lib.types.str; }; listenPort = lib.mkOption { type = lib.types.port; }; hub = lib.mkOption { type = lib.types.bool; default = false; description = "Forward between peers, and masquerade what leaves by the public interface."; }; peers = lib.mkOption { type = lib.types.listOf ( lib.types.submodule { options = { name = lib.mkOption { type = lib.types.str; }; publicKey = lib.mkOption { type = lib.types.str; }; allowedIPs = lib.mkOption { type = lib.types.listOf lib.types.str; }; endpoint = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; }; }; } ); }; }; config = { sops.secrets = { "wireguard/private_key" = { }; } // lib.listToAttrs (map (p: lib.nameValuePair "wireguard/psk/${p.name}" { }) cfg.peers); networking.wireguard.interfaces.${cfg.interface} = { ips = cfg.address; listenPort = cfg.listenPort; privateKeyFile = config.sops.secrets."wireguard/private_key".path; peers = map (p: { inherit (p) name publicKey allowedIPs endpoint; presharedKeyFile = config.sops.secrets."wireguard/psk/${p.name}".path; persistentKeepalive = 25; }) cfg.peers; }; networking.firewall.allowedUDPPorts = [ cfg.listenPort ]; boot.kernel.sysctl = lib.mkIf cfg.hub { "net.ipv4.ip_forward" = 1; "net.ipv6.conf.all.forwarding" = 1; }; networking.nat = lib.mkIf cfg.hub { enable = true; enableIPv6 = true; internalInterfaces = [ cfg.interface ]; externalInterface = "enp1s0"; }; }; }