1 { config, lib, pkgs, ... }:
6 cfg = config.services.zerotierone;
8 settingsFormat = pkgs.formats.json {};
9 localConfFile = settingsFormat.generate "zt-local.conf" cfg.localConf;
10 localConfFilePath = "/var/lib/zerotier-one/local.conf";
13 options.services.zerotierone.enable = mkEnableOption "ZeroTierOne";
15 options.services.zerotierone.joinNetworks = mkOption {
17 example = [ "a8a2c3c10c1a68de" ];
18 type = types.listOf types.str;
20 List of ZeroTier Network IDs to join on startup.
21 Note that networks are only ever joined, but not automatically left after removing them from the list.
22 To remove networks, use the ZeroTier CLI: `zerotier-cli leave <network-id>`
26 options.services.zerotierone.port = mkOption {
30 Network port used by ZeroTier.
34 options.services.zerotierone.package = mkPackageOption pkgs "zerotierone" { };
36 options.services.zerotierone.localConf = mkOption {
39 Optional configuration to be written to the Zerotier JSON-based local.conf.
40 If set, the configuration will be symlinked to `/var/lib/zerotier-one/local.conf` at build time.
41 To understand the configuration format, refer to https://docs.zerotier.com/config/#local-configuration-options.
44 settings.allowTcpFallbackRelay = false;
46 type = settingsFormat.type;
49 config = mkIf cfg.enable {
50 systemd.services.zerotierone = {
51 description = "ZeroTierOne";
53 wantedBy = [ "multi-user.target" ];
54 after = [ "network.target" ];
55 wants = [ "network-online.target" ];
57 path = [ cfg.package ];
60 mkdir -p /var/lib/zerotier-one/networks.d
61 chmod 700 /var/lib/zerotier-one
62 chown -R root:root /var/lib/zerotier-one
64 # cleans up old symlinks also if we unset localConf
65 if [[ -L "${localConfFilePath}" && "$(readlink "${localConfFilePath}")" =~ ^${builtins.storeDir}.* ]]; then
66 rm ${localConfFilePath}
68 '' + (concatMapStrings (netId: ''
69 touch "/var/lib/zerotier-one/networks.d/${netId}.conf"
70 '') cfg.joinNetworks) + lib.optionalString (cfg.localConf != {}) ''
71 # in case the user has applied manual changes to the local.conf, we backup the file
72 if [ -f "${localConfFilePath}" ]; then
73 mv ${localConfFilePath} ${localConfFilePath}.bak
75 ln -s ${localConfFile} ${localConfFilePath}
79 ExecStart = "${cfg.package}/bin/zerotier-one -p${toString cfg.port}";
86 # ZeroTier does not issue DHCP leases, but some strangers might...
87 networking.dhcpcd.denyInterfaces = [ "zt*" ];
89 # ZeroTier receives UDP transmissions
90 networking.firewall.allowedUDPPorts = [ cfg.port ];
92 environment.systemPackages = [ cfg.package ];
94 # Prevent systemd from potentially changing the MAC address
95 systemd.network.links."50-zerotier" = {
100 AutoNegotiation = false;
101 MACAddressPolicy = "none";