2023-08-28 09:09:05 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.clan.networking.zerotier;
|
2023-09-27 08:54:51 +00:00
|
|
|
facts = config.clanCore.secrets.zerotier.facts;
|
2023-09-25 17:03:54 +00:00
|
|
|
networkConfig = {
|
|
|
|
authTokens = [
|
|
|
|
null
|
|
|
|
];
|
|
|
|
authorizationEndpoint = "";
|
|
|
|
capabilities = [ ];
|
|
|
|
clientId = "";
|
|
|
|
dns = [ ];
|
|
|
|
enableBroadcast = true;
|
|
|
|
id = cfg.networkId;
|
|
|
|
ipAssignmentPools = [ ];
|
|
|
|
mtu = 2800;
|
|
|
|
multicastLimit = 32;
|
|
|
|
name = "";
|
|
|
|
uwid = cfg.networkId;
|
|
|
|
objtype = "network";
|
|
|
|
private = !cfg.controller.public;
|
|
|
|
remoteTraceLevel = 0;
|
|
|
|
remoteTraceTarget = null;
|
|
|
|
revision = 1;
|
|
|
|
routes = [ ];
|
|
|
|
rules = [
|
|
|
|
{
|
|
|
|
not = false;
|
|
|
|
or = false;
|
|
|
|
type = "ACTION_ACCEPT";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
rulesSource = "";
|
|
|
|
ssoEnabled = false;
|
|
|
|
tags = [ ];
|
|
|
|
v4AssignMode = {
|
|
|
|
zt = false;
|
|
|
|
};
|
|
|
|
v6AssignMode = {
|
|
|
|
"6plane" = false;
|
|
|
|
rfc4193 = true;
|
|
|
|
zt = false;
|
|
|
|
};
|
|
|
|
};
|
2023-08-28 09:09:05 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.clan.networking.zerotier = {
|
|
|
|
networkId = lib.mkOption {
|
2023-09-26 16:18:42 +00:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2023-09-26 15:31:45 +00:00
|
|
|
default = null;
|
2023-08-28 09:09:05 +00:00
|
|
|
description = ''
|
|
|
|
zerotier networking id
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
controller = {
|
|
|
|
enable = lib.mkEnableOption "turn this machine into the networkcontroller";
|
|
|
|
public = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
everyone can join a public network without having the administrator to accept
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-09-25 17:03:54 +00:00
|
|
|
config = lib.mkMerge [
|
2023-09-26 15:31:45 +00:00
|
|
|
({
|
|
|
|
# Override license so that we can build zerotierone without
|
|
|
|
# having to re-import nixpkgs.
|
|
|
|
services.zerotierone.package = lib.mkDefault (pkgs.zerotierone.overrideAttrs (_old: { meta = { }; }));
|
|
|
|
})
|
2023-09-25 17:03:54 +00:00
|
|
|
(lib.mkIf (cfg.networkId != null) {
|
|
|
|
systemd.network.networks.zerotier = {
|
|
|
|
matchConfig.Name = "zt*";
|
|
|
|
networkConfig = {
|
|
|
|
LLMNR = true;
|
|
|
|
LLDP = true;
|
|
|
|
MulticastDNS = true;
|
|
|
|
KeepConfiguration = "static";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
networking.firewall.allowedUDPPorts = [ 9993 ];
|
|
|
|
networking.firewall.interfaces."zt+".allowedTCPPorts = [ 5353 ];
|
|
|
|
networking.firewall.interfaces."zt+".allowedUDPPorts = [ 5353 ];
|
|
|
|
services.zerotierone = {
|
|
|
|
enable = true;
|
|
|
|
joinNetworks = [ cfg.networkId ];
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.controller.enable {
|
|
|
|
# only the controller needs to have the key in the repo, the other clients can be dynamic
|
|
|
|
# we generate the zerotier code manually for the controller, since it's part of the bootstrap command
|
|
|
|
clanCore.secrets.zerotier = {
|
2023-09-27 08:54:51 +00:00
|
|
|
facts.zerotier-network-id = { };
|
|
|
|
secrets.zerotier-identity-secret = { };
|
2023-09-25 17:03:54 +00:00
|
|
|
generator = ''
|
2023-09-26 15:31:45 +00:00
|
|
|
export PATH=${lib.makeBinPath [ config.services.zerotierone.package pkgs.fakeroot ]}
|
|
|
|
${pkgs.python3.interpreter} ${./generate-network.py} "$facts/zerotier-network-id" "$secrets/zerotier-identity-secret"
|
2023-09-25 17:03:54 +00:00
|
|
|
'';
|
2023-08-28 09:09:05 +00:00
|
|
|
};
|
2023-09-27 08:54:51 +00:00
|
|
|
})
|
|
|
|
(lib.mkIf ((config.clanCore.secrets ? zerotier) && (facts.zerotier-network-id.value != null)) {
|
|
|
|
clan.networking.zerotier.networkId = facts.zerotier-network-id.value;
|
2023-08-28 09:09:05 +00:00
|
|
|
|
2023-09-25 17:03:54 +00:00
|
|
|
systemd.services.zerotierone.serviceConfig.ExecStartPre = [
|
2023-09-26 15:31:45 +00:00
|
|
|
"+${pkgs.writeShellScript "init-zerotier" ''
|
|
|
|
cp ${config.clanCore.secrets.zerotier.secrets.zerotier-identity-secret.path} /var/lib/zerotier-one/identity.secret
|
|
|
|
ln -sfT ${pkgs.writeText "net.json" (builtins.toJSON networkConfig)} /var/lib/zerotier-one/controller.d/network/${cfg.networkId}.json
|
|
|
|
''}"
|
2023-09-25 17:03:54 +00:00
|
|
|
];
|
|
|
|
})
|
|
|
|
];
|
2023-08-28 09:09:05 +00:00
|
|
|
}
|