summaryrefslogtreecommitdiffstats
path: root/configs/retiolum.nix
blob: e7478f948fdf77c0f3aa885f0ccef58473bda098 (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
{ config, inputs, lib, mylib, pkgs, ... }: let
  host = config.krebs.build.host;
  net = host.nets.retiolum;
  keyType = "application/x-tinc-private-key; algorithm=ed25519";
  keySlot = config.enstate.channels.secrets.slots."retiolum.key";
  keyValidator = pkgs.writers.writeDash "validate-tinc-ed25519-private-key" ''
    set -efu
    ${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 |
    while read -r address; do
      test -e "$1"/channels/"$address" || continue
      ${pkgs.jq}/bin/jq -Rsc -f ${pkgs.writeJq "check-tinc-ed25519-private-key.jq" ''
        "-----BEGIN ED25519 PRIVATE KEY-----" as $begin |
        "-----END ED25519 PRIVATE KEY-----" as $end |
        split("\n") | if .[-1] == "" then .[:-1] else . end |
        if length == 0 then "is empty"
        elif .[0] != $begin then "does not start with \($begin)"
        elif .[-1] != $end then "does not end with \($end)"
        elif length < 3 then "has no key between its first and last line"
        else empty end |
        { address: $ARGS.named.address, message: "the key \(.)" }
      ''} --arg address "$address" "$1"/channels/"$address"
    done
  '';
in {
  imports = [
    # The module only uses crane for its default package.
    (import (inputs.tincr + "/nix/module.nix") { crane = null; })
  ];
  config = lib.mkMerge [
    {
      environment.systemPackages = [
        config.services.tincr.package
      ];
      services.tincr.package = pkgs.tincr;
      services.tincr.networks.retiolum = {
        nodeName = host.name;
        listenPort = net.tinc.port;
        ed25519PrivateKeyFile =
          if config.enstate.enable then keySlot.path
          else "${config.krebs.secret.directory}/retiolum.ed25519_key.priv";
        hosts =
          lib.mapAttrs (_: host: host.nets.retiolum.tinc.config)
            (lib.filterAttrs
              (_: host: host.nets ? retiolum && host.nets.retiolum.tinc.config or null != null)
              config.krebs.hosts);
        connectTo = builtins.filter (mylib.ne host.name) [
          "ne"
          "neoprism"
          "eve"
        ];
        interfaceName = "retiolum";
        addresses = [
          "${net.ip4.addr}/${toString net.ip4.prefixLength}"
          "${net.ip6.addr}/${toString net.ip6.prefixLength}"
        ];
        extraConfig = ''
          Broadcast = no
          StrictSubnets = yes
        '';
      };
      systemd.services.tincr-retiolum.restartIfCredentialsChange = true;
      # tincd doesn't set the MTU.  This is what kartei's retiolum module uses,
      # measured across the mesh, so paths that lose large packets don't
      # stall TCP.
      systemd.network.networks."40-tincr-retiolum".linkConfig.MTUBytes = "1377";
    }
    (lib.mkIf config.enstate.enable {
      enstate.types.${keyType}.validator = keyValidator;
      enstate.channels.secrets.slots."retiolum.key" = {
        type = keyType;
        secret = true;
        source = lib.mkDefault {
          command = [
            "secrets" host.name "retiolum.ed25519_key.priv"
          ];
        };
      };
    })
    (lib.mkIf (!config.hrm.nftables.enable) {
      tv.iptables.input-internet-accept-tcp = [ "tinc" ];
      tv.iptables.input-internet-accept-udp = [ "tinc" ];
    })
    (lib.mkIf config.hrm.nftables.enable {
      hrm.nftables.input.internet.tcp = [ "tinc" ];
      hrm.nftables.input.internet.udp = [ "tinc" ];
    })
  ];
}