From 8085f764b6e344d5eae5f2aaf710ae4f3e4098ea Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 00:46:56 +0000 Subject: systemd-credentials: make the options part of systemd.services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit systemd.services..restartIfCredentialsChange and reloadIfCredentialsChange replace hrm.systemd.services..…; the alias for stockholm's krebs.systemd.services now points there. Units can't be made from config.systemd.services itself, so the trigger services become two templates, credentials-restart@ and credentials-reload@, which call systemctl themselves; only the path units remain per service, by their old names. Without comparing checksums any more: the path units fire when a credential is written, and nothing rewrites one unchanged. --- modules/systemd-credentials.nix | 108 +++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 56 deletions(-) (limited to 'modules/systemd-credentials.nix') diff --git a/modules/systemd-credentials.nix b/modules/systemd-credentials.nix index 9342d55b..f25ebd5a 100644 --- a/modules/systemd-credentials.nix +++ b/modules/systemd-credentials.nix @@ -1,16 +1,31 @@ -{ config, pkgs, lib, mylib, ... }: { +{ config, pkgs, lib, mylib, ... }: let + # The services that ask for it. Their units come from templates, since + # units can't be made from config.systemd.services itself. + watched = lib.filterAttrs + (_: service: service.restartIfCredentialsChange || service.reloadIfCredentialsChange) + config.systemd.services; + + action = service: + if service.reloadIfCredentialsChange then "reload" else "restart"; + + template = action: { + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.systemd}/bin/systemctl ${builtins.getAttr action { + reload = "try-reload-or-restart"; + restart = "try-restart"; + }} %i"; + }; + }; +in { # stockholm's repo-sync still uses the old name. imports = [ - (lib.mkAliasOptionModule [ "krebs" "systemd" "services" ] [ "hrm" "systemd" "services" ]) + (lib.mkAliasOptionModule [ "krebs" "systemd" "services" ] [ "systemd" "services" ]) ]; - options.hrm.systemd.services = lib.mkOption { - default = {}; - type = lib.types.attrsOf (lib.types.submodule (cfg_: let - serviceName = cfg_.config._module.args.name; - cfg = config.systemd.services.${serviceName} // cfg_.config; - in { + options.systemd.services = lib.mkOption { + type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: { options = { credentialPaths = lib.mkOption { default = @@ -20,11 +35,11 @@ mylib.types.absolute-pathname.check (map (mylib.compose [ mylib.maybeHead (builtins.match "[^:]*:(.*)") ]) - (lib.toList cfg.serviceConfig.LoadCredential))); + (lib.toList config.serviceConfig.LoadCredential or []))); readOnly = true; }; credentialUnitName = lib.mkOption { - default = "trigger-${mylib.systemd.encodeName serviceName}"; + default = "trigger-${mylib.systemd.encodeName name}"; readOnly = true; }; restartIfCredentialsChange = lib.mkOption { @@ -46,55 +61,36 @@ type = lib.types.bool; }; }; + # Without it, a reload leaves the credentials as they were. + config.serviceConfig = lib.mkIf config.reloadIfCredentialsChange { + RefreshOnReload = "credentials"; + }; })); }; - config.assertions = lib.mapAttrsToList (serviceName: cfg: let - serviceConfig = config.systemd.services.${serviceName}.serviceConfig; - in { - # With RefreshOnReload= set, systemd reloads a service without ExecReload= - # by refreshing its credentials only, without telling the service. - assertion = - cfg.reloadIfCredentialsChange -> - serviceConfig ? ExecReload || serviceConfig.Type or "" == "notify-reload"; - message = "hrm.systemd.services.${serviceName}.reloadIfCredentialsChange needs ExecReload= or Type=notify-reload"; - }) config.hrm.systemd.services; - - config.systemd = lib.mkMerge (lib.mapAttrsToList (serviceName: cfg: { - # Without it, a reload leaves the credentials as they were. - services.${serviceName} = lib.mkIf cfg.reloadIfCredentialsChange { - serviceConfig.RefreshOnReload = "credentials"; - }; - paths.${cfg.credentialUnitName} = { - wantedBy = [ "multi-user.target" ]; - pathConfig.PathChanged = cfg.credentialPaths; - }; - services.${cfg.credentialUnitName} = { - serviceConfig = { - Type = "oneshot"; - StateDirectory = "credentials"; - ExecStart = pkgs.writeDash "${cfg.credentialUnitName}.sh" '' - set -efu - - PATH=${lib.makeBinPath [ - pkgs.coreutils - pkgs.diffutils - pkgs.systemd - ]} + config = { + assertions = lib.mapAttrsToList (serviceName: service: { + # With RefreshOnReload= set, systemd reloads a service without ExecReload= + # by refreshing its credentials only, without telling the service. + assertion = + service.reloadIfCredentialsChange -> + service.serviceConfig ? ExecReload || service.serviceConfig.Type or "" == "notify-reload"; + message = "systemd.services.${serviceName}.reloadIfCredentialsChange needs ExecReload= or Type=notify-reload"; + }) config.systemd.services; - cache=/var/lib/credentials/${mylib.shell.escape serviceName}.sha1sum - tmpfile=$(mktemp "$cache.XXXXXXXX") - trap 'rm -f "$tmpfile"' EXIT - - sha1sum ${toString cfg.credentialPaths} > "$tmpfile" - if test -f "$cache" && cmp -s "$tmpfile" "$cache"; then - exit - fi - mv "$tmpfile" "$cache" + systemd.paths = lib.mapAttrs' (serviceName: service: + lib.nameValuePair service.credentialUnitName { + wantedBy = [ "multi-user.target" ]; + pathConfig = { + PathChanged = service.credentialPaths; + Unit = "credentials-${action service}@${serviceName}.service"; + }; + } + ) watched; - systemctl ${if cfg.reloadIfCredentialsChange then "try-reload-or-restart" else "try-restart"} ${mylib.shell.escape serviceName} - ''; - }; - }; - }) (lib.filterAttrs (_: cfg: cfg.restartIfCredentialsChange || cfg.reloadIfCredentialsChange) config.hrm.systemd.services)); + # Unconditionally: whether a service asks for them depends on + # config.systemd.services. + systemd.services."credentials-reload@" = template "reload"; + systemd.services."credentials-restart@" = template "restart"; + }; } -- cgit v1.3.1