nixpkgs/nixos/modules/services/networking/zerotierone.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.0 KiB
Nix
Raw Normal View History

2015-06-07 04:10:52 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.zerotierone;
localConfFile = pkgs.writeText "zt-local.conf" (builtins.toJSON cfg.localConf);
2023-12-18 01:21:01 +00:00
localConfFilePath = "/var/lib/zerotier-one/local.conf";
2015-06-07 04:10:52 +00:00
in
{
options.services.zerotierone.enable = mkEnableOption "ZeroTierOne";
options.services.zerotierone.joinNetworks = mkOption {
default = [];
example = [ "a8a2c3c10c1a68de" ];
type = types.listOf types.str;
description = ''
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>`
'';
};
options.services.zerotierone.port = mkOption {
default = 9993;
type = types.port;
description = ''
Network port used by ZeroTier.
'';
};
options.services.zerotierone.package = mkPackageOption pkgs "zerotierone" { };
2023-12-18 00:03:29 +00:00
options.services.zerotierone.localConf = mkOption {
default = null;
description = ''
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.
2023-12-17 23:58:47 +00:00
To understand the configuration format, refer to https://docs.zerotier.com/config/#local-configuration-options.
'';
2023-12-17 23:58:47 +00:00
example = {
settings.allowTcpFallbackRelay = false;
};
type = types.nullOr types.attrs;
2023-12-17 23:58:47 +00:00
};
2023-12-18 00:03:29 +00:00
2015-06-07 04:10:52 +00:00
config = mkIf cfg.enable {
systemd.services.zerotierone = {
description = "ZeroTierOne";
2015-06-07 04:10:52 +00:00
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
wants = [ "network-online.target" ];
path = [ cfg.package ];
preStart = ''
mkdir -p /var/lib/zerotier-one/networks.d
2015-06-07 04:10:52 +00:00
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"
'') cfg.joinNetworks) + optionalString (cfg.localConf != null) ''
if [ -L "${localConfFilePath}" ]
2023-12-18 01:21:01 +00:00
then
rm ${localConfFilePath}
elif [ -f "${localConfFilePath}" ]
2023-12-18 23:07:48 +00:00
then
mv ${localConfFilePath} ${localConfFilePath}.bak
2023-12-18 01:21:01 +00:00
fi
ln -s ${localConfFile} ${localConfFilePath}
2023-12-17 23:58:47 +00:00
'';
2015-06-07 04:10:52 +00:00
serviceConfig = {
ExecStart = "${cfg.package}/bin/zerotier-one -p${toString cfg.port}";
Restart = "always";
KillMode = "process";
TimeoutStopSec = 5;
2015-06-07 04:10:52 +00:00
};
};
# ZeroTier does not issue DHCP leases, but some strangers might...
networking.dhcpcd.denyInterfaces = [ "zt*" ];
# ZeroTier receives UDP transmissions
networking.firewall.allowedUDPPorts = [ cfg.port ];
2016-08-31 10:39:55 +00:00
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";
};
};
2015-06-07 04:10:52 +00:00
};
}