2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.zerotierone;
|
2024-06-24 18:47:55 +00:00
|
|
|
|
|
|
|
settingsFormat = pkgs.formats.json {};
|
|
|
|
localConfFile = settingsFormat.generate "zt-local.conf" cfg.localConf;
|
2024-01-02 11:29:13 +00:00
|
|
|
localConfFilePath = "/var/lib/zerotier-one/local.conf";
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
{
|
2024-04-21 15:54:59 +00:00
|
|
|
options.services.zerotierone.enable = mkEnableOption "ZeroTierOne";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
options.services.zerotierone.joinNetworks = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [ "a8a2c3c10c1a68de" ];
|
|
|
|
type = types.listOf types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-08-22 20:05:09 +00:00
|
|
|
List of ZeroTier Network IDs to join on startup.
|
|
|
|
Note that networks are only ever joined, but not automatically left after removing them from the list.
|
|
|
|
To remove networks, use the ZeroTier CLI: `zerotier-cli leave <network-id>`
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.services.zerotierone.port = mkOption {
|
|
|
|
default = 9993;
|
2022-11-21 17:40:18 +00:00
|
|
|
type = types.port;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Network port used by ZeroTier.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
options.services.zerotierone.package = mkPackageOption pkgs "zerotierone" { };
|
|
|
|
|
|
|
|
options.services.zerotierone.localConf = mkOption {
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
Optional configuration to be written to the Zerotier JSON-based local.conf.
|
|
|
|
If set, the configuration will be symlinked to `/var/lib/zerotier-one/local.conf` at build time.
|
|
|
|
To understand the configuration format, refer to https://docs.zerotier.com/config/#local-configuration-options.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2024-01-02 11:29:13 +00:00
|
|
|
example = {
|
|
|
|
settings.allowTcpFallbackRelay = false;
|
|
|
|
};
|
2024-06-24 18:47:55 +00:00
|
|
|
type = settingsFormat.type;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.zerotierone = {
|
|
|
|
description = "ZeroTierOne";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
|
|
|
|
path = [ cfg.package ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -p /var/lib/zerotier-one/networks.d
|
|
|
|
chmod 700 /var/lib/zerotier-one
|
|
|
|
chown -R root:root /var/lib/zerotier-one
|
|
|
|
'' + (concatMapStrings (netId: ''
|
|
|
|
touch "/var/lib/zerotier-one/networks.d/${netId}.conf"
|
2024-06-24 18:47:55 +00:00
|
|
|
'') cfg.joinNetworks) + optionalString (cfg.localConf != {}) ''
|
2024-01-02 11:29:13 +00:00
|
|
|
if [ -L "${localConfFilePath}" ]
|
|
|
|
then
|
|
|
|
rm ${localConfFilePath}
|
|
|
|
elif [ -f "${localConfFilePath}" ]
|
|
|
|
then
|
|
|
|
mv ${localConfFilePath} ${localConfFilePath}.bak
|
|
|
|
fi
|
|
|
|
ln -s ${localConfFile} ${localConfFilePath}
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/zerotier-one -p${toString cfg.port}";
|
|
|
|
Restart = "always";
|
|
|
|
KillMode = "process";
|
|
|
|
TimeoutStopSec = 5;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# ZeroTier does not issue DHCP leases, but some strangers might...
|
|
|
|
networking.dhcpcd.denyInterfaces = [ "zt*" ];
|
|
|
|
|
|
|
|
# ZeroTier receives UDP transmissions
|
|
|
|
networking.firewall.allowedUDPPorts = [ cfg.port ];
|
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
|
|
|
# Prevent systemd from potentially changing the MAC address
|
|
|
|
systemd.network.links."50-zerotier" = {
|
|
|
|
matchConfig = {
|
|
|
|
OriginalName = "zt*";
|
|
|
|
};
|
|
|
|
linkConfig = {
|
|
|
|
AutoNegotiation = false;
|
|
|
|
MACAddressPolicy = "none";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|