1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-20 16:39:31 +03:00
nixpkgs/nixos/modules/services/networking/zerotierone.nix
Silvan Mosberger 4f0dadbf38 treewide: format all inactive Nix files
After final improvements to the official formatter implementation,
this commit now performs the first treewide reformat of Nix files using it.
This is part of the implementation of RFC 166.

Only "inactive" files are reformatted, meaning only files that
aren't being touched by any PR with activity in the past 2 months.
This is to avoid conflicts for PRs that might soon be merged.
Later we can do a full treewide reformat to get the rest,
which should not cause as many conflicts.

A CI check has already been running for some time to ensure that new and
already-formatted files are formatted, so the files being reformatted here
should also stay formatted.

This commit was automatically created and can be verified using

    nix-build a08b3a4d19.tar.gz \
      --argstr baseRev b32a094368
    result/bin/apply-formatting $NIXPKGS_PATH
2024-12-10 20:26:33 +01:00

113 lines
3.3 KiB
Nix

{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.zerotierone;
settingsFormat = pkgs.formats.json { };
localConfFile = settingsFormat.generate "zt-local.conf" cfg.localConf;
localConfFilePath = "/var/lib/zerotier-one/local.conf";
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" { };
options.services.zerotierone.localConf = mkOption {
default = { };
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.
To understand the configuration format, refer to https://docs.zerotier.com/config/#local-configuration-options.
'';
example = {
settings.allowTcpFallbackRelay = false;
};
type = settingsFormat.type;
};
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
# cleans up old symlinks also if we unset localConf
if [[ -L "${localConfFilePath}" && "$(readlink "${localConfFilePath}")" =~ ^${builtins.storeDir}.* ]]; then
rm ${localConfFilePath}
fi
''
+ (concatMapStrings (netId: ''
touch "/var/lib/zerotier-one/networks.d/${netId}.conf"
'') cfg.joinNetworks)
+ lib.optionalString (cfg.localConf != { }) ''
# in case the user has applied manual changes to the local.conf, we backup the file
if [ -f "${localConfFilePath}" ]; then
mv ${localConfFilePath} ${localConfFilePath}.bak
fi
ln -s ${localConfFile} ${localConfFilePath}
'';
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";
};
};
};
}