zaphyra's git: nixfiles

zaphyra's nixfiles

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 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
{
  nixosConfigurations,
  machines,
  machineConfig,
  config,
  lib,
  ...
}:
let
  inherit (lib) types;
  cfg = config.zpha.configure.netcupTunnel;

in
{

  options.zpha.configure.netcupTunnel = {
    enable = lib.mkEnableOption "";

    addresses = lib.mkOption {
      type = types.listOf types.str;
      default = [
        "${machineConfig.networking.ip6Address}/${toString machineConfig.networking.ip6PrefixLength}"
      ];
    };

    wgPrivateKey = lib.mkOption {
      type = types.path;
      default = config.sops.secrets."wgPrivateKey".path;
    };

    wgPublicKey = lib.mkOption {
      type = types.str;
      default = machineConfig.wgPublicKey;
    };

  };

  config = lib.mkIf cfg.enable {
    networking.firewall = {
      trustedInterfaces = [
        "wg-netcup"
        "netcup"
      ];
      allowedUDPPorts = [
        config.systemd.network.netdevs."15-wg-netcup".wireguardConfig.ListenPort
      ];
    };

    systemd.network = {
      config.networkConfig = {
        IPv6Forwarding = true;
      };

      netdevs = {
        "10-netcup" = lib.mkIf (config.networking.hostName != "sorrah") {
          netdevConfig = {
            Kind = "dummy";
            Name = "netcup";
          };
        };
        "15-wg-netcup" = {
          netdevConfig = {
            Kind = "wireguard";
            Name = "wg-netcup";
          };

          wireguardConfig = {
            PrivateKeyFile = cfg.wgPrivateKey;
            ListenPort = 51820;
            FirewallMark = 51820;
          };

          wireguardPeers =
            if (config.networking.hostName == "sorrah") then
              (lib.pipe nixosConfigurations [
                (lib.filterAttrs (name: _: name != config.networking.hostName))
                (lib.filterAttrs (_: value: value.config.zpha.configure.netcupTunnel.enable))
                (lib.mapAttrsToList (
                  name: value: {
                    PublicKey = value.config.zpha.configure.netcupTunnel.wgPublicKey;
                    AllowedIPs = value.config.zpha.configure.netcupTunnel.addresses;
                    PersistentKeepalive = 10;
                  }
                ))
              ])
            else
              [
                {
                  PublicKey = machines.sorrah.wgPublicKey;
                  Endpoint = "${machines.sorrah.networking.ip4Address}:51820";
                  AllowedIPs = [ "::/0" ];
                  PersistentKeepalive = 10;
                }
              ];
        };
      };

      networks = {
        "5-primaryInterface" = lib.mkIf (config.networking.hostName == "sorrah") {
          networkConfig = {
            IPv6ProxyNDP = true;
            IPv6ProxyNDPAddress = lib.pipe config.systemd.network.netdevs."15-wg-netcup".wireguardPeers [
              (map (element: if element ? AllowedIPs then element.AllowedIPs else [ ]))
              lib.flatten
              (map (element: builtins.elemAt (lib.strings.splitString "/" element) 0))
            ];
          };
        };
        "10-netcup" = lib.mkIf (config.networking.hostName != "sorrah") {
          enable = true;
          name = "netcup";
          address = cfg.addresses;
          routingPolicyRules = lib.singleton {
            From = "2a03:4000:4d:5e:acab::/112";
            Table = 1234;
            Priority = 2000;
          };
        };
        "15-wg-netcup" = {
          matchConfig.Name = "wg-netcup";
          linkConfig.RequiredForOnline = false;
        }
        // (
          if (config.networking.hostName == "sorrah") then
            {
              address = cfg.addresses;
            }
          else
            {
              routes = lib.singleton {
                Destination = "::/0";
                Table = "1234";
              };
            }
        );
      };
    };
  };

}