summaryrefslogtreecommitdiffstats
path: root/configs/wiregrill.nix
blob: 98ea7425ff44c4a93aebd45ea0c3f503f70131f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{ 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" ];
        };
      })
    ]))
  ])
[cgit] Unable to lock slot /tmp/cgit/15300000.lock: Permission denied (13)