summaryrefslogtreecommitdiffstats
path: root/configs/wiregrill.nix
blob: 55bb6f52601ed3d88d71f268b0b92aaa7d859300 (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
{ config, lib, pkgs, ... }: let
  cfg = {
    enable = cfg.net != null;
    net = config.krebs.build.host.nets.wiregrill or null;
  };
  toCidrNotation = ip: "${ip.addr}/${toString ip.prefixLength}";
in
  lib.mkIf cfg.enable {
    networking.wireguard.interfaces.wiregrill = {
      ips =
        lib.optional (cfg.net.ip4 != null) cfg.net.ip4.addr ++
        lib.optional (cfg.net.ip6 != null) cfg.net.ip6.addr;
      listenPort = 51820;
      privateKeyFile = "${config.krebs.secret.directory}/wiregrill.key";
      allowedIPsAsRoutes = true;
      peers = lib.mapAttrsToList
        (_: host: {
          allowedIPs = 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 (_: h: lib.hasAttr "wiregrill" h.nets) 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);
    };
    tv.iptables.extra.filter.INPUT = [
      "-p udp --dport ${toString cfg.net.wireguard.port} -j ACCEPT"
    ];
  }