nix-config/modules/TLMS/wg.nix

126 lines
3.7 KiB
Nix
Raw Normal View History

2022-10-02 21:39:37 +02:00
{ lib, config, self, ... }:
let
2022-12-30 18:29:13 +01:00
cfg = config.deployment-TLMS.net.wg;
2022-10-02 21:39:37 +02:00
in {
2022-12-30 18:29:13 +01:00
options.deployment-TLMS.net.wg = with lib; {
2022-10-02 21:39:37 +02:00
ownEndpoint.host = mkOption {
2022-10-02 21:39:37 +02:00
type = types.nullOr types.str;
default = null;
};
ownEndpoint.port = mkOption {
type = types.port;
2022-10-02 21:39:37 +02:00
default = 51820;
};
2022-10-02 21:39:37 +02:00
publicKey = mkOption {
type = types.str;
default = "";
2022-10-02 21:39:37 +02:00
description = "own public key";
};
privateKeyFile = mkOption {
type = types.either types.str types.path;
};
addr4 = mkOption {
2022-10-02 21:39:37 +02:00
type = types.nullOr types.str;
default = null;
};
prefix4 = mkOption {
type = types.int;
default = 24;
description = "network prefix";
};
extraPeers = mkOption {
description = "extra peers that are not part of the deployment";
type = types.listOf (types.submodule {
options.addr4 = mkOption {
type = types.str;
description = "ip _without_ a network prefix";
};
options.publicKey = mkOption {
type = types.str;
description = "public key";
};
});
};
};
config = let
2022-10-02 21:39:37 +02:00
# move out as options?
dvbwg-name = "wg-ddvb";
keepalive = 25;
2022-10-02 21:39:37 +02:00
# helpers
2022-12-30 18:29:13 +01:00
peer-systems = (lib.filter (x: (x.config.deployment-TLMS.net.wg.addr4 != cfg.addr4) && (!isNull x.config.deployment-TLMS.net.wg.addr4))
2022-10-02 21:39:37 +02:00
(lib.attrValues self.nixosConfigurations));
endpoint =
let
ep = (lib.filter (x:
2022-12-30 18:29:13 +01:00
x.config.deployment-TLMS.net.wg.addr4 != cfg.addr4
&& (!isNull x.config.deployment-TLMS.net.wg.ownEndpoint.host))
2022-10-02 21:39:37 +02:00
(lib.attrValues self.nixosConfigurations));
in
assert lib.assertMsg (lib.length ep == 1) "there should be exactly one endpoint"; ep;
peers = map (x: {
wireguardPeerConfig = {
2022-12-30 18:29:13 +01:00
PublicKey = x.config.deployment-TLMS.net.wg.publicKey;
AllowedIPs = [ "${x.config.deployment-TLMS.net.wg.addr4}/32" ];
2022-10-02 21:39:37 +02:00
PersistentKeepalive = keepalive;
};
}) peer-systems;
ep = [ {
wireguardPeerConfig =
let x = lib.elemAt endpoint 0; in {
2022-12-30 18:29:13 +01:00
PublicKey = x.config.deployment-TLMS.net.wg.publicKey;
AllowedIPs = [ "${x.config.deployment-TLMS.net.wg.addr4}/${toString cfg.prefix4}" ];
Endpoint = with x.config.deployment-TLMS.net.wg.ownEndpoint; "${host}:${toString port}";
2022-10-02 21:39:37 +02:00
PersistentKeepalive = keepalive;
};
} ];
# stuff proper
dvbwg-netdev = {
Kind = "wireguard";
Name = dvbwg-name;
2022-12-30 18:29:13 +01:00
Description = "TLMS enterprise, highly available, biocomputing-neural-network maintained, converged network";
2022-10-02 21:39:37 +02:00
};
dvbwg-wireguard = {
PrivateKeyFile = cfg.privateKeyFile;
2022-10-05 20:47:05 +02:00
} //
(if !isNull cfg.ownEndpoint.host then { ListenPort = cfg.ownEndpoint.port; } else {});
2022-10-02 21:39:37 +02:00
expeers = map (x: {
wireguardPeerConfig = {
PublicKey = x.publicKey;
AllowedIPs = [ "${x.addr4}/32" ];
PersistentKeepalive = keepalive;
};
}) cfg.extraPeers;
peerconf = if isNull cfg.ownEndpoint.host then ep else (peers ++ expeers);
in
lib.mkIf (!isNull cfg.addr4) {
networking.wireguard.enable = true;
2022-10-04 20:07:53 +02:00
networking.firewall.trustedInterfaces = [ dvbwg-name ];
2022-10-02 21:39:37 +02:00
systemd.network.netdevs."30-${dvbwg-name}" = {
netdevConfig = dvbwg-netdev;
wireguardConfig = dvbwg-wireguard;
wireguardPeers = peerconf;
};
systemd.network.networks."30-${dvbwg-name}" = {
matchConfig.Name = dvbwg-name;
networkConfig = {
Address = "${cfg.addr4}/${toString cfg.prefix4}";
};
};
2022-10-02 21:39:37 +02:00
};
}