clan-core/nixosModules/clanCore/zerotier/default.nix

284 lines
9.9 KiB
Nix
Raw Permalink Normal View History

2024-03-17 18:48:49 +00:00
{
config,
lib,
pkgs,
...
}:
let
Revert "clan.core: rename clan.{deployment,networking} -> clan.core.{deployment,networking}" This reverts commit afbd4a984de393d54a053e9d721929fb644b001b. The old configuration cannot be updated like this: eve] error: [eve] … while calling the 'head' builtin [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/attrsets.nix:1575:11: [eve] 1574| || pred here (elemAt values 1) (head values) then [eve] 1575| head values [eve] | ^ [eve] 1576| else [eve] [eve] … while evaluating the attribute 'value' [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/modules.nix:809:9: [eve] 808| in warnDeprecation opt // [eve] 809| { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value; [eve] | ^ [eve] 810| inherit (res.defsFinal') highestPrio; [eve] [eve] … while evaluating the option `system.build.toplevel': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/activation/top-level.nix': [eve] [eve] … while evaluating the option `assertions': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/boot/systemd.nix': [eve] [eve] … while evaluating the option `systemd.services': [eve] [eve] … while evaluating definitions from `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/zerotier': [eve] [eve] … while evaluating the option `clan.core.networking.zerotier.networkId': [eve] [eve] (stack trace truncated; use '--show-trace' to show the full, detailed trace) [eve] [eve] error: A definition for option `clan.core.networking.zerotier.networkId' is not of type `null or string'. Definition values: [eve] - In `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/networking.nix': [eve] { [eve] _type = "override"; [eve] content = "267efd4a15b69623"; [eve] priorit
2024-07-05 09:16:05 +00:00
cfg = config.clan.networking.zerotier;
2024-06-17 10:42:28 +00:00
facts = config.clan.core.facts.services.zerotier.public or { };
2024-01-12 16:33:27 +00:00
genMoonScript = pkgs.runCommand "genmoon" { nativeBuildInputs = [ pkgs.python3 ]; } ''
install -Dm755 ${./genmoon.py} $out/bin/genmoon
patchShebangs $out/bin/genmoon
'';
in
{
Revert "clan.core: rename clan.{deployment,networking} -> clan.core.{deployment,networking}" This reverts commit afbd4a984de393d54a053e9d721929fb644b001b. The old configuration cannot be updated like this: eve] error: [eve] … while calling the 'head' builtin [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/attrsets.nix:1575:11: [eve] 1574| || pred here (elemAt values 1) (head values) then [eve] 1575| head values [eve] | ^ [eve] 1576| else [eve] [eve] … while evaluating the attribute 'value' [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/modules.nix:809:9: [eve] 808| in warnDeprecation opt // [eve] 809| { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value; [eve] | ^ [eve] 810| inherit (res.defsFinal') highestPrio; [eve] [eve] … while evaluating the option `system.build.toplevel': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/activation/top-level.nix': [eve] [eve] … while evaluating the option `assertions': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/boot/systemd.nix': [eve] [eve] … while evaluating the option `systemd.services': [eve] [eve] … while evaluating definitions from `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/zerotier': [eve] [eve] … while evaluating the option `clan.core.networking.zerotier.networkId': [eve] [eve] (stack trace truncated; use '--show-trace' to show the full, detailed trace) [eve] [eve] error: A definition for option `clan.core.networking.zerotier.networkId' is not of type `null or string'. Definition values: [eve] - In `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/networking.nix': [eve] { [eve] _type = "override"; [eve] content = "267efd4a15b69623"; [eve] priorit
2024-07-05 09:16:05 +00:00
options.clan.networking.zerotier = {
networkId = lib.mkOption {
2023-09-26 16:18:42 +00:00
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
zerotier networking id
'';
};
name = lib.mkOption {
type = lib.types.str;
2024-06-17 10:42:28 +00:00
default = config.clan.core.clanName;
defaultText = "config.clan.core.clanName";
description = ''
zerotier network name
'';
};
2024-01-12 16:33:27 +00:00
moon = {
stableEndpoints = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Make this machine a moon.
Other machines can join this moon by adding this moon in their config.
It will be reachable under the given stable endpoints.
'';
};
2024-01-12 17:31:18 +00:00
orbitMoons = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Join these moons.
This machine will be able to reach all machines in these moons.
'';
};
2024-01-12 16:33:27 +00:00
};
2023-11-14 13:03:47 +00:00
subnet = lib.mkOption {
type = lib.types.nullOr lib.types.str;
readOnly = true;
default =
if cfg.networkId == null then
null
else
2023-11-14 13:03:47 +00:00
let
part0 = builtins.substring 0 2 cfg.networkId;
part1 = builtins.substring 2 2 cfg.networkId;
part2 = builtins.substring 4 2 cfg.networkId;
part3 = builtins.substring 6 2 cfg.networkId;
part4 = builtins.substring 8 2 cfg.networkId;
part5 = builtins.substring 10 2 cfg.networkId;
part6 = builtins.substring 12 2 cfg.networkId;
part7 = builtins.substring 14 2 cfg.networkId;
in
"fd${part0}:${part1}${part2}:${part3}${part4}:${part5}${part6}:${part7}99:9300::/88";
2023-11-14 13:03:47 +00:00
description = ''
zerotier subnet
'';
};
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
'';
};
};
2024-02-03 03:27:04 +00:00
settings = lib.mkOption {
2024-05-28 11:35:11 +00:00
description = "override the network config in /var/lib/zerotier/bla/$network.json";
2024-03-17 18:48:49 +00:00
type = lib.types.submodule { freeformType = (pkgs.formats.json { }).type; };
2024-02-03 03:27:04 +00:00
};
};
config = lib.mkMerge [
({
# Override license so that we can build zerotierone without
# having to re-import nixpkgs.
2024-03-17 18:48:49 +00:00
services.zerotierone.package = lib.mkDefault (
pkgs.zerotierone.overrideAttrs (_old: {
meta = { };
})
);
})
(lib.mkIf ((facts.zerotier-ip.value or null) != null) {
environment.etc."zerotier/ip".text = facts.zerotier-ip.value;
})
(lib.mkIf (cfg.networkId != null) {
systemd.network.networks."09-zerotier" = {
matchConfig.Name = "zt*";
networkConfig = {
LLDP = true;
MulticastDNS = true;
KeepConfiguration = "static";
};
};
systemd.services.zerotierone.serviceConfig.ExecStartPre = [
"+${pkgs.writeShellScript "init-zerotier" ''
2024-06-17 10:42:28 +00:00
cp ${config.clan.core.facts.services.zerotier.secret.zerotier-identity-secret.path} /var/lib/zerotier-one/identity.secret
2024-03-17 18:48:49 +00:00
zerotier-idtool getpublic /var/lib/zerotier-one/identity.secret > /var/lib/zerotier-one/identity.public
2024-03-17 18:48:49 +00:00
${lib.optionalString (cfg.controller.enable) ''
mkdir -p /var/lib/zerotier-one/controller.d/network
ln -sfT ${pkgs.writeText "net.json" (builtins.toJSON cfg.settings)} /var/lib/zerotier-one/controller.d/network/${cfg.networkId}.json
''}
${lib.optionalString (cfg.moon.stableEndpoints != [ ]) ''
if [[ ! -f /var/lib/zerotier-one/moon.json ]]; then
zerotier-idtool initmoon /var/lib/zerotier-one/identity.public > /var/lib/zerotier-one/moon.json
fi
${genMoonScript}/bin/genmoon /var/lib/zerotier-one/moon.json ${builtins.toFile "moon.json" (builtins.toJSON cfg.moon.stableEndpoints)} /var/lib/zerotier-one/moons.d
''}
2024-01-11 11:56:05 +00:00
2024-03-17 18:48:49 +00:00
# cleanup old networks
if [[ -d /var/lib/zerotier-one/networks.d ]]; then
find /var/lib/zerotier-one/networks.d \
-type f \
-name "*.conf" \
-not \( ${
lib.concatMapStringsSep " -o " (
netId: ''-name "${netId}.conf"''
) config.services.zerotierone.joinNetworks
} \) \
-delete
fi
''}"
];
2024-01-11 14:34:46 +00:00
systemd.services.zerotierone.serviceConfig.ExecStartPost = [
"+${pkgs.writeShellScript "configure-interface" ''
while ! ${pkgs.netcat}/bin/nc -z localhost 9993; do
sleep 0.1
done
zerotier-cli listnetworks -j | ${pkgs.jq}/bin/jq -r '.[] | [.portDeviceName, .name] | @tsv' \
| while IFS=$'\t' read -r portDeviceName name; do
if [[ -z "$name" ]] || [[ -z "$portDeviceName" ]]; then
continue
fi
# Execute the command for each element
${pkgs.iproute2}/bin/ip link property add dev "$portDeviceName" altname "$name"
done
2024-01-12 17:31:18 +00:00
${lib.concatMapStringsSep "\n" (moon: ''
zerotier-cli orbit ${moon} ${moon}
'') cfg.moon.orbitMoons}
2024-03-17 18:48:49 +00:00
''}"
2024-01-11 14:34:46 +00:00
];
2024-01-10 17:21:37 +00:00
networking.firewall.allowedTCPPorts = [ 9993 ]; # zerotier
networking.firewall.allowedUDPPorts = [ 9993 ]; # zerotier
networking.networkmanager.unmanaged = [ "interface-name:zt*" ];
services.zerotierone = {
enable = true;
joinNetworks = [ cfg.networkId ];
};
# The official zerotier tcp relay no longer works: https://github.com/zerotier/ZeroTierOne/issues/2202
# So we host our own relay in https://git.clan.lol/clan/clan-infra
services.zerotierone.localConf.settings.tcpFallbackRelay = "65.21.12.51/4443";
})
(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
2024-06-17 10:42:28 +00:00
clan.core.facts.services.zerotier = {
2024-03-28 09:30:37 +00:00
public.zerotier-ip = { };
public.zerotier-network-id = { };
secret.zerotier-identity-secret = { };
2024-03-17 18:48:49 +00:00
generator.path = [
config.services.zerotierone.package
pkgs.python3
];
generator.script =
let
library = "libfakeroot${pkgs.stdenv.hostPlatform.extensions.sharedLibrary}";
minifakeroot = pkgs.stdenv.mkDerivation {
name = "minifakeroot";
dontUnpack = true;
installPhase = ''
mkdir -p $out/lib
${
if pkgs.stdenv.isDarwin then
"$CC -dynamiclib -o $out/lib/libfakeroot.dylib ${./fake_root.c}"
else
"$CC -shared -o $out/lib/libfakeroot.so ${./fake_root.c}"
}
'';
};
varName = if pkgs.stdenv.isDarwin then "DYLD_INSERT_LIBRARIES" else "LD_PRELOAD";
in
''
export ${varName}=${minifakeroot}/lib/${library}
python3 ${./generate.py} --mode network \
--ip "$facts/zerotier-ip" \
--identity-secret "$secrets/zerotier-identity-secret" \
--network-id "$facts/zerotier-network-id"
'';
};
2024-06-17 10:42:28 +00:00
clan.core.state.zerotier.folders = [ "/var/lib/zerotier-one" ];
2023-12-04 16:05:37 +00:00
2024-06-17 10:42:28 +00:00
environment.systemPackages = [ config.clan.core.clanPkgs.zerotier-members ];
})
2024-02-14 06:15:59 +00:00
(lib.mkIf (!cfg.controller.enable && cfg.networkId != null) {
2024-06-17 10:42:28 +00:00
clan.core.facts.services.zerotier = {
2024-03-28 09:30:37 +00:00
public.zerotier-ip = { };
secret.zerotier-identity-secret = { };
2024-03-17 18:48:49 +00:00
generator.path = [
config.services.zerotierone.package
pkgs.python3
];
generator.script = ''
python3 ${./generate.py} --mode identity \
--ip "$facts/zerotier-ip" \
--identity-secret "$secrets/zerotier-identity-secret" \
--network-id ${cfg.networkId}
'';
};
})
(lib.mkIf (cfg.controller.enable && (facts.zerotier-network-id.value or null) != null) {
Revert "clan.core: rename clan.{deployment,networking} -> clan.core.{deployment,networking}" This reverts commit afbd4a984de393d54a053e9d721929fb644b001b. The old configuration cannot be updated like this: eve] error: [eve] … while calling the 'head' builtin [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/attrsets.nix:1575:11: [eve] 1574| || pred here (elemAt values 1) (head values) then [eve] 1575| head values [eve] | ^ [eve] 1576| else [eve] [eve] … while evaluating the attribute 'value' [eve] at /nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/lib/modules.nix:809:9: [eve] 808| in warnDeprecation opt // [eve] 809| { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value; [eve] | ^ [eve] 810| inherit (res.defsFinal') highestPrio; [eve] [eve] … while evaluating the option `system.build.toplevel': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/activation/top-level.nix': [eve] [eve] … while evaluating the option `assertions': [eve] [eve] … while evaluating definitions from `/nix/store/5b0hl2dnvr1sawqlkwmsnaiyqz00d34h-source/nixos/modules/system/boot/systemd.nix': [eve] [eve] … while evaluating the option `systemd.services': [eve] [eve] … while evaluating definitions from `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/zerotier': [eve] [eve] … while evaluating the option `clan.core.networking.zerotier.networkId': [eve] [eve] (stack trace truncated; use '--show-trace' to show the full, detailed trace) [eve] [eve] error: A definition for option `clan.core.networking.zerotier.networkId' is not of type `null or string'. Definition values: [eve] - In `/nix/store/kpzcdgndym0qm1w490mjvk9c2qmz03h5-source/nixosModules/clanCore/networking.nix': [eve] { [eve] _type = "override"; [eve] content = "267efd4a15b69623"; [eve] priorit
2024-07-05 09:16:05 +00:00
clan.networking.zerotier.networkId = facts.zerotier-network-id.value;
clan.networking.zerotier.settings = {
2024-03-17 18:48:49 +00:00
authTokens = [ null ];
authorizationEndpoint = "";
capabilities = [ ];
clientId = "";
2024-02-05 01:31:10 +00:00
dns = { };
enableBroadcast = true;
id = cfg.networkId;
ipAssignmentPools = [ ];
mtu = 2800;
multicastLimit = 32;
name = cfg.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;
};
};
environment.etc."zerotier/network-id".text = facts.zerotier-network-id.value;
2023-09-29 08:51:38 +00:00
systemd.services.zerotierone.serviceConfig.ExecStartPost = [
"+${pkgs.writeShellScript "whitelist-controller" ''
2024-06-17 10:42:28 +00:00
${config.clan.core.clanPkgs.zerotier-members}/bin/zerotier-members allow ${
2024-03-17 18:48:49 +00:00
builtins.substring 0 10 cfg.networkId
}
2023-09-29 08:51:38 +00:00
''}"
];
})
];
}