1 { pkgs, lib, config, hostName, private, ... }:
4 peers = import wg-intra/peers.nix;
5 wg = config.networking.wireguard.interfaces.${iface};
8 options.networking.wireguard.${iface}.peers =
9 lib.genAttrs (lib.attrNames peers) (peerName: {
10 enable = lib.mkEnableOption "this peer";
13 networking.wireguard.interfaces.${iface} = lib.recursiveUpdate
14 (removeAttrs peers.${hostName} ["ipv4" "persistentKeepalive" "peer"])
17 lib.mapAttrsToList (peerName: peer:
21 peer.persistentKeepalive # Useful if this peer is behind a NAT
22 or peers.${hostName}.persistentKeepalive # Useful if this host is behind a NAT
27 (lib.filterAttrs (peerName: _: config.networking.wireguard.${iface}.peers.${peerName}.enable) peers)
29 privateKeyFile = lib.mkDefault "${private}/${hostName}/wireguard/${iface}/privateKey";
31 # Set the MTU to a minimum
32 # (IPv4 requires at least 68 but it's 1280 for IPv6).
33 # This prevents connections to stall on huge packets,
34 # or delaying their initializing due to TCP PMTU probing.
36 ip link set dev ${iface} mtu 1280
39 networking.hosts = lib.mkMerge [
40 (lib.mapAttrs' (hostName: host:
41 lib.nameValuePair host.ipv4 [ "${hostName}.wg" ]) peers)
42 { "${peers.losurdo.ipv4}" = [
43 "nix-extracache.losurdo.wg"
44 "nix-localcache.losurdo.wg"
47 networking.firewall.extraCommands = ''
48 ip46tables -A nixos-fw -i ${iface} -p tcp -m tcp --dport 22 -j ACCEPT
49 '' + lib.optionalString (wg.listenPort != null) ''
50 ip46tables -A nixos-fw -i any -p udp -m udp --dport ${toString wg.listenPort} -j ACCEPT
52 services.fail2ban.ignoreIP = lib.concatMap
53 (host: host.peer.allowedIPs)
54 (lib.attrValues peers);