mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 20:04:32 +09:00
serve exits 78 (EX_CONFIG) when the configuration file it loaded is wrong: format, syntax, keys or values. No file, an unreadable one, the database and ports still exit 1, so systemd keeps retrying what can heal. The deb unit and the NixOS module set RestartPreventExitStatus=78. A parse error gives a position and fixed words, never the file's text. Updates #1926
362 lines
14 KiB
Nix
362 lines
14 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
cfg = config.services.headscale;
|
|
|
|
dataDir = "/var/lib/headscale";
|
|
runDir = "/run/headscale";
|
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
|
|
|
# headscale reads a non-empty variable of the unit over the setting.
|
|
effective =
|
|
name: setting:
|
|
let
|
|
v = config.systemd.services.headscale.environment.${name} or null;
|
|
in
|
|
if v == null || v == "" then setting else v;
|
|
policyMode = effective "HEADSCALE_POLICY_MODE" cfg.settings.policy.mode;
|
|
policyPath = effective "HEADSCALE_POLICY_PATH" cfg.settings.policy.path;
|
|
# Interpolated as the config and the unit are, so a Nix path is the same store path.
|
|
policyFile = if policyMode == "file" && policyPath != "" then "${policyPath}" else null;
|
|
policyFileCheck = cfg.check.policy && policyFile != null;
|
|
# Build-time checks run the host's headscale.
|
|
canExecute = pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform;
|
|
# The build can read a store path.
|
|
buildTimePolicy = policyFileCheck && lib.hasPrefix "${builtins.storeDir}/" policyFile && canExecute;
|
|
preSwitchPolicy = policyFileCheck && !buildTimePolicy;
|
|
|
|
# Interpolated as nixpkgs renders Environment=, so a Nix path is the same store path.
|
|
unitEnv = lib.mapAttrsToList (n: v: "${n}=${v}") (
|
|
lib.filterAttrs (_: v: v != null) config.systemd.services.headscale.environment
|
|
);
|
|
|
|
policyCheck = pkgs.runCommand "headscale-policy-check" { } ''
|
|
${lib.getExe cfg.package} check policy -f ${lib.escapeShellArg policyFile}
|
|
touch $out
|
|
'';
|
|
|
|
# check config, not check all: the files the config names are not in the sandbox.
|
|
configCheck = pkgs.runCommand "headscale-config-check" { } ''
|
|
env -i ${lib.escapeShellArgs unitEnv} ${lib.getExe cfg.package} check config -f ${cfg.configFile} || {
|
|
echo "headscale: this check sees the unit's environment, not its EnvironmentFile or %-specifiers expanded; services.headscale.check.config = false skips it" >&2
|
|
exit 1
|
|
}
|
|
touch $out
|
|
'';
|
|
|
|
# Its own script, for shellcheck and runtimeInputs.
|
|
preSwitchCheck =
|
|
let
|
|
# systemd gives the unit Group= and the account's /etc/group memberships.
|
|
# Activation rewrites the account's memberships in the groups NixOS
|
|
# declares (members holds extraGroups), so those come from the new
|
|
# configuration. With mutableUsers it keeps a hand-made group.
|
|
groups = [
|
|
"headscale"
|
|
]
|
|
++ map (g: g.name) (
|
|
lib.filter (g: lib.elem "headscale" g.members) (lib.attrValues config.users.groups)
|
|
)
|
|
++ lib.filter (g: g != "") (
|
|
lib.concatMap (g: lib.splitString " " (toString g)) (
|
|
lib.toList (config.systemd.services.headscale.serviceConfig.SupplementaryGroups or [ ])
|
|
)
|
|
);
|
|
declared = map (g: g.name) (lib.attrValues config.users.groups);
|
|
in
|
|
pkgs.writeShellApplication {
|
|
name = "headscale-pre-switch-check";
|
|
runtimeInputs = [
|
|
pkgs.coreutils
|
|
pkgs.getent
|
|
pkgs.util-linux
|
|
];
|
|
text = ''
|
|
# An unchanged unit is not restarted, so there is nothing to protect.
|
|
unit=etc/systemd/system/headscale.service
|
|
if [ "$(readlink -f "/run/current-system/$unit")" = "$(readlink -f "$1/$unit")" ]; then
|
|
echo "headscale: unit unchanged, skipping checks"
|
|
exit 0
|
|
fi
|
|
# Activation creates users and groups after this check runs.
|
|
if ! id -u headscale >/dev/null 2>&1; then
|
|
echo "headscale: user headscale does not exist yet, skipping checks"
|
|
exit 0
|
|
fi
|
|
if ! getent group headscale >/dev/null; then
|
|
echo "headscale: group headscale does not exist yet, skipping checks"
|
|
exit 0
|
|
fi
|
|
trap '[ "$?" = 0 ] || echo "headscale: to skip, set services.headscale.check.config and check.policy to false, or run once with NIXOS_NO_CHECK=1"' EXIT
|
|
|
|
# A group this switch creates does not exist yet; setpriv would fail on it.
|
|
want=(${lib.escapeShellArgs groups})
|
|
${lib.optionalString config.users.mutableUsers ''
|
|
# Not the account's primary group, which the unit does not get, nor a
|
|
# group NixOS declares now, or declared before and activation removes.
|
|
skip=" $(id -gn headscale 2>/dev/null || true) "${lib.escapeShellArg (toString declared)}" $(cat /var/lib/nixos/declarative-groups 2>/dev/null || true) "
|
|
for g in $(id -Gn headscale); do
|
|
[[ $skip == *" $g "* ]] || want+=("$g")
|
|
done
|
|
''}
|
|
groups=()
|
|
for g in "''${want[@]}"; do
|
|
if getent group "$g" >/dev/null; then groups+=("$g"); fi
|
|
done
|
|
grouplist=$(IFS=,; echo "''${groups[*]}")
|
|
|
|
# As the unit's user and groups, with the unit's environment rather than the caller's.
|
|
hs() {
|
|
setpriv --reuid=headscale --regid=headscale --groups="$grouplist" -- \
|
|
env -i ${lib.escapeShellArgs unitEnv} ${lib.getExe cfg.package} "$@"
|
|
}
|
|
|
|
${lib.optionalString cfg.check.config ''
|
|
echo "headscale: checking the new configuration"
|
|
hs check all -f ${cfg.configFile}
|
|
''}
|
|
${lib.optionalString preSwitchPolicy ''
|
|
echo "headscale: checking the new policy"
|
|
hs check policy -f ${lib.escapeShellArg policyFile}
|
|
''}
|
|
'';
|
|
};
|
|
|
|
# The option's type admits a number or http/https as the port, or an empty
|
|
# listen_addr, which grants nothing. Past four digits a port is never
|
|
# privileged, and the cap keeps toIntBase10 from throwing on a number too
|
|
# long for an integer.
|
|
listenPort = lib.last (lib.splitString ":" cfg.settings.listen_addr);
|
|
listenDigits = builtins.match "0*([0-9]{1,4})" listenPort;
|
|
privilegedPort =
|
|
lib.elem listenPort [
|
|
"http"
|
|
"https"
|
|
]
|
|
|| (listenDigits != null && lib.toIntBase10 (lib.head listenDigits) < 1024);
|
|
|
|
# Below lib.mkDefault, so a user's own mkDefault replaces these values
|
|
# instead of conflicting with them.
|
|
distroDefault = lib.mkOverride 1200;
|
|
|
|
# Go reads cli, nested or dotted, as the client-only cli.*; written to the
|
|
# config, cli.api_key would land in the world-readable store.
|
|
cliKeys = lib.filter (n: n == "cli" || lib.hasPrefix "cli." n) (lib.attrNames cfg.settings);
|
|
in
|
|
{
|
|
# Disable the upstream NixOS module to prevent conflicts
|
|
disabledModules = [ "services/networking/headscale.nix" ];
|
|
|
|
options = {
|
|
services.headscale = {
|
|
enable = lib.mkEnableOption "headscale, Open Source coordination server for Tailscale";
|
|
|
|
package = lib.mkPackageOption pkgs "headscale" { };
|
|
|
|
configFile = lib.mkOption {
|
|
type = lib.types.path;
|
|
readOnly = true;
|
|
default = settingsFormat.generate "headscale.yaml" cfg.settings;
|
|
defaultText = lib.literalMD "{option}`services.headscale.settings` as YAML";
|
|
description = ''
|
|
Path to the configuration file of headscale:
|
|
{option}`services.headscale.settings` as YAML.
|
|
'';
|
|
};
|
|
|
|
check.config = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = true;
|
|
description = ''
|
|
While building the system (native builds only), run
|
|
`headscale check config -f` on
|
|
{option}`services.headscale.configFile`, so a configuration headscale
|
|
rejects fails `nixos-rebuild build`.
|
|
|
|
Before a switch restarts headscale, run the new package's
|
|
`headscale check all` on the new configuration and abort the switch
|
|
if it fails. It checks the configuration and every file it names
|
|
(policy, DERP maps, extra records, TLS pair, existing noise and DERP
|
|
keys) as the `headscale` user. Keys are read, never created.
|
|
|
|
Both run outside the unit, with its `environment` but not its
|
|
`EnvironmentFile`; the switch check runs only when headscale's unit
|
|
changes. Neither reads a `file:` secret, so credentials referenced
|
|
with `file:` pass. Turning off only
|
|
{option}`services.headscale.check.policy` does not skip the policy
|
|
before a switch. Skip the switch check once with `NIXOS_NO_CHECK=1`.
|
|
'';
|
|
};
|
|
|
|
check.policy = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = true;
|
|
description = ''
|
|
In file policy mode, run `headscale check policy -f` on the new
|
|
policy: it checks the policy and runs its `tests` and `sshTests`
|
|
against one node for each user and tag the policy names. A policy in
|
|
the Nix store is checked while building the system (native builds
|
|
only), so a bad policy fails `nixos-rebuild build`. Any other policy
|
|
is checked before a switch restarts headscale.
|
|
{option}`services.headscale.check.config` checks the policy before a
|
|
switch too; turn off both to skip every policy check.
|
|
'';
|
|
};
|
|
|
|
# Read-only, kept only because nixpkgs' headplane module reads them.
|
|
user = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "headscale";
|
|
readOnly = true;
|
|
description = "User account under which headscale runs, created by this module.";
|
|
};
|
|
|
|
group = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "headscale";
|
|
readOnly = true;
|
|
description = "Group under which headscale runs, created by this module.";
|
|
};
|
|
|
|
settings = lib.mkOption {
|
|
description = ''
|
|
headscale's configuration, written to
|
|
{option}`services.headscale.configFile`. Each option is one key of
|
|
[Configuration options](https://headscale.net/stable/ref/configuration-options/).
|
|
A key without an option passes through to headscale, which rejects
|
|
unknown keys.
|
|
'';
|
|
type = lib.types.submodule {
|
|
freeformType = settingsFormat.type;
|
|
options = import ./options.generated.nix { inherit lib; };
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
system.checks =
|
|
lib.optional buildTimePolicy policyCheck
|
|
++ lib.optional (cfg.check.config && canExecute) configCheck;
|
|
|
|
system.preSwitchChecks.headscale = lib.mkIf (
|
|
cfg.check.config || preSwitchPolicy
|
|
) ''${lib.getExe preSwitchCheck} "$@"'';
|
|
|
|
assertions = [
|
|
{
|
|
assertion = cliKeys == [ ];
|
|
message = "services.headscale.settings: remove ${lib.concatStringsSep ", " cliKeys}: the server never reads cli.*; headscale's CLI reads ~/.config/headscale/config.yaml or HEADSCALE_CLI_*, and here it would be written to the world-readable Nix store.";
|
|
}
|
|
];
|
|
|
|
services.headscale.settings = lib.mapAttrsRecursive (_: distroDefault) {
|
|
# Where NixOS keeps headscale's state and socket. Nix pins the package,
|
|
# so an update notice cannot be acted on.
|
|
noise.private_key_path = "${dataDir}/noise_private.key";
|
|
derp.server.private_key_path = "${dataDir}/derp_server_private.key";
|
|
database.sqlite.path = "${dataDir}/db.sqlite";
|
|
unix_socket = "${runDir}/headscale.sock";
|
|
disable_check_updates = true;
|
|
};
|
|
|
|
environment = {
|
|
systemPackages = [ cfg.package ];
|
|
|
|
# The local CLI reads no file: these point it at the server's socket and
|
|
# update-check setting. Through PAM, so sudo keeps them too.
|
|
sessionVariables = {
|
|
HEADSCALE_UNIX_SOCKET = cfg.settings.unix_socket;
|
|
HEADSCALE_DISABLE_CHECK_UPDATES = lib.boolToString cfg.settings.disable_check_updates;
|
|
};
|
|
};
|
|
|
|
users.groups.headscale = { };
|
|
|
|
users.users.headscale = {
|
|
description = "headscale user";
|
|
home = dataDir;
|
|
group = "headscale";
|
|
isSystemUser = true;
|
|
};
|
|
|
|
systemd.services.headscale = {
|
|
description = "headscale coordination server for Tailscale";
|
|
wants = [ "network-online.target" ];
|
|
after = [ "network-online.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
script = ''
|
|
exec ${lib.getExe cfg.package} serve --config ${cfg.configFile}
|
|
'';
|
|
|
|
serviceConfig =
|
|
let
|
|
# "" empties the set, and comes first, so the unit gets only the
|
|
# capabilities listed after it, here or by another module.
|
|
capabilities = lib.mkBefore ([ "" ] ++ lib.optional privilegedPort "CAP_NET_BIND_SERVICE");
|
|
in
|
|
{
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
Restart = "always";
|
|
RestartSec = "5s";
|
|
# headscale serve exits 78 (EX_CONFIG) on a configuration it rejects,
|
|
# which a restart cannot fix.
|
|
RestartPreventExitStatus = 78;
|
|
Type = "simple";
|
|
# Read here, so setting either read-only option fails evaluation.
|
|
User = cfg.user;
|
|
Group = cfg.group;
|
|
|
|
# Hardening options
|
|
RuntimeDirectory = "headscale";
|
|
# Allow headscale group access so users can be added and use the CLI.
|
|
RuntimeDirectoryMode = "0750";
|
|
|
|
StateDirectory = "headscale";
|
|
StateDirectoryMode = "0750";
|
|
|
|
ProtectSystem = "strict";
|
|
ProtectHome = true;
|
|
PrivateTmp = true;
|
|
PrivateDevices = true;
|
|
ProtectKernelTunables = true;
|
|
ProtectControlGroups = true;
|
|
RestrictSUIDSGID = true;
|
|
PrivateMounts = true;
|
|
ProtectKernelModules = true;
|
|
ProtectKernelLogs = true;
|
|
ProtectHostname = true;
|
|
ProtectClock = true;
|
|
ProtectProc = "invisible";
|
|
ProcSubset = "pid";
|
|
RestrictNamespaces = true;
|
|
RemoveIPC = true;
|
|
UMask = "0077";
|
|
|
|
CapabilityBoundingSet = capabilities;
|
|
AmbientCapabilities = capabilities;
|
|
NoNewPrivileges = true;
|
|
LockPersonality = true;
|
|
RestrictRealtime = true;
|
|
SystemCallFilter = [
|
|
"@system-service"
|
|
"~@privileged"
|
|
];
|
|
SystemCallArchitectures = "native";
|
|
RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX";
|
|
};
|
|
};
|
|
};
|
|
|
|
meta.maintainers = with lib.maintainers; [
|
|
kradalby
|
|
misterio77
|
|
];
|
|
}
|