{ config, lib, pkgs, ... }: let cfg = { enable = cfg.net != null; net = config.krebs.build.host.nets.wiregrill or null; }; # The hub is the host with a public address; the others reach each other # through it. isHub = cfg.net.via != null; toCidrNotation = ip: "${ip.addr}/${toString ip.prefixLength}"; keyType = "application/x-wireguard-private-key"; keySlot = config.enstate.channels.secrets.slots."wiregrill.key"; keyValidator = pkgs.writers.writeDash "validate-wireguard-private-key" '' set -efu # Not in a pipe, where set -e would miss a failure of jq. addresses=$(${pkgs.jq}/bin/jq -r -f ${pkgs.writeJq "slots-of-type.jq" '' .slots | to_entries[] | select(.value.type == $ARGS.named.type) | .key ''} --arg type ${lib.escapeShellArg keyType} "$1"/invocation.json) for address in $addresses; do ${pkgs.jq}/bin/jq -Rsc -f ${pkgs.writeJq "check-wireguard-private-key.jq" '' if rtrimstr("\n") | test("^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw048]=$") then empty else { address: $ARGS.named.address, message: "the key is not 32 bytes in base64" } end ''} --arg address "$address" "$1"/channels/"$address" done ''; in lib.mkIf cfg.enable (lib.mkMerge [ { # The hub passes packets between the other hosts. boot.kernel.sysctl = lib.mkIf isHub { "net.ipv4.ip_forward" = lib.mkDefault true; "net.ipv6.conf.all.forwarding" = lib.mkDefault true; }; networking.wireguard.interfaces.wiregrill = { ips = lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++ lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6); listenPort = 51820; privateKeyFile = if config.enstate.enable then keySlot.path else "${config.krebs.secret.directory}/wiregrill.key"; allowedIPsAsRoutes = true; peers = lib.mapAttrsToList (_: host: { allowedIPs = lib.optionals isHub ( lib.optional (host.nets.wiregrill.ip4 != null) host.nets.wiregrill.ip4.addr ++ lib.optional (host.nets.wiregrill.ip6 != null) host.nets.wiregrill.ip6.addr ) ++ host.nets.wiregrill.wireguard.subnets; endpoint = lib.mkIf (host.nets.wiregrill.via != null) (host.nets.wiregrill.via.ip4.addr + ":${toString host.nets.wiregrill.wireguard.port}"); persistentKeepalive = lib.mkIf (host.nets.wiregrill.via != null) 61; publicKey = lib.replaceStrings ["\n"] [""] host.nets.wiregrill.wireguard.pubkey; }) (lib.filterAttrs (name: host: name != config.krebs.build.host.name && lib.hasAttr "wiregrill" host.nets && (isHub || host.nets.wiregrill.via != null)) config.krebs.hosts); }; systemd.network.networks.wiregrill = { matchConfig.Name = "wiregrill"; address = lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++ lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6); }; hrm.nftables.input.internet.udp = [ cfg.net.wireguard.port ]; hrm.nftables.tables.filter.chains.forward.rules.wiregrill = lib.mkIf isHub { iifname = "wiregrill"; oifname = "wiregrill"; verdict = "accept"; }; } # With networkd, the key is systemd-networkd's credential, which orders # it after enstate.service by itself; the scripted unit reads it itself. (lib.mkIf config.enstate.enable (lib.mkMerge [ { enstate.types.${keyType}.validator = keyValidator; enstate.channels.secrets.slots."wiregrill.key" = { type = keyType; secret = true; source = lib.mkDefault { secrets.key = "wiregrill.key"; }; reactions.restart = if config.networking.wireguard.useNetworkd then [ "systemd-networkd.service" ] else [ "wireguard-wiregrill.service" ]; }; } # Without the key, systemd wouldn't start networkd at all, and every # interface would stay down. With a key that isn't one, only # wiregrill does. (lib.mkIf config.networking.wireguard.useNetworkd { systemd.services.systemd-networkd.serviceConfig.SetCredential = [ "wireguard-wiregrill-private-key:none" ]; }) (lib.mkIf (!config.networking.wireguard.useNetworkd) { systemd.services.wireguard-wiregrill = { after = [ "enstate.service" ]; wants = [ "enstate.service" ]; }; }) ])) ])