at 24.11-pre 3.1 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.zerotierone; 7 localConfFile = pkgs.writeText "zt-local.conf" (builtins.toJSON cfg.localConf); 8 localConfFilePath = "/var/lib/zerotier-one/local.conf"; 9in 10{ 11 options.services.zerotierone.enable = mkEnableOption "ZeroTierOne"; 12 13 options.services.zerotierone.joinNetworks = mkOption { 14 default = []; 15 example = [ "a8a2c3c10c1a68de" ]; 16 type = types.listOf types.str; 17 description = '' 18 List of ZeroTier Network IDs to join on startup. 19 Note that networks are only ever joined, but not automatically left after removing them from the list. 20 To remove networks, use the ZeroTier CLI: `zerotier-cli leave <network-id>` 21 ''; 22 }; 23 24 options.services.zerotierone.port = mkOption { 25 default = 9993; 26 type = types.port; 27 description = '' 28 Network port used by ZeroTier. 29 ''; 30 }; 31 32 options.services.zerotierone.package = mkPackageOption pkgs "zerotierone" { }; 33 34 options.services.zerotierone.localConf = mkOption { 35 default = null; 36 description = '' 37 Optional configuration to be written to the Zerotier JSON-based local.conf. 38 If set, the configuration will be symlinked to `/var/lib/zerotier-one/local.conf` at build time. 39 To understand the configuration format, refer to https://docs.zerotier.com/config/#local-configuration-options. 40 ''; 41 example = { 42 settings.allowTcpFallbackRelay = false; 43 }; 44 type = types.nullOr types.attrs; 45 }; 46 47 config = mkIf cfg.enable { 48 systemd.services.zerotierone = { 49 description = "ZeroTierOne"; 50 51 wantedBy = [ "multi-user.target" ]; 52 after = [ "network.target" ]; 53 wants = [ "network-online.target" ]; 54 55 path = [ cfg.package ]; 56 57 preStart = '' 58 mkdir -p /var/lib/zerotier-one/networks.d 59 chmod 700 /var/lib/zerotier-one 60 chown -R root:root /var/lib/zerotier-one 61 '' + (concatMapStrings (netId: '' 62 touch "/var/lib/zerotier-one/networks.d/${netId}.conf" 63 '') cfg.joinNetworks) + optionalString (cfg.localConf != null) '' 64 if [ -L "${localConfFilePath}" ] 65 then 66 rm ${localConfFilePath} 67 elif [ -f "${localConfFilePath}" ] 68 then 69 mv ${localConfFilePath} ${localConfFilePath}.bak 70 fi 71 ln -s ${localConfFile} ${localConfFilePath} 72 ''; 73 74 serviceConfig = { 75 ExecStart = "${cfg.package}/bin/zerotier-one -p${toString cfg.port}"; 76 Restart = "always"; 77 KillMode = "process"; 78 TimeoutStopSec = 5; 79 }; 80 }; 81 82 # ZeroTier does not issue DHCP leases, but some strangers might... 83 networking.dhcpcd.denyInterfaces = [ "zt*" ]; 84 85 # ZeroTier receives UDP transmissions 86 networking.firewall.allowedUDPPorts = [ cfg.port ]; 87 88 environment.systemPackages = [ cfg.package ]; 89 90 # Prevent systemd from potentially changing the MAC address 91 systemd.network.links."50-zerotier" = { 92 matchConfig = { 93 OriginalName = "zt*"; 94 }; 95 linkConfig = { 96 AutoNegotiation = false; 97 MACAddressPolicy = "none"; 98 }; 99 }; 100 }; 101}