summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-18 13:10:22 +0000
committertv <tv@krebsco.de>2026-11-18 14:30:34 +0100
commit0aaaa4bee0921b3681da264db507a15c389ded53 (patch)
tree4899a84380cfc8e90496e72f86cb33265b5ca0fd
parent728c8fa33aef16fdeeae0a4a6775ee7426d78f66 (diff)
systemd-credentials: optionally reload instead of restart
reloadIfCredentialsChange reloads the service when its credentials change, with RefreshOnReload=credentials so the reload sees the new ones. A service that can't be reloaded is restarted. It is off for every service so far: tinc's reload doesn't read its private key again, and the others haven't been checked. An assertion requires ExecReload= or Type=notify-reload, because with RefreshOnReload= set, systemd "reloads" any other service by refreshing its credentials without telling it.
-rw-r--r--modules/systemd-credentials.nix28
1 files changed, 26 insertions, 2 deletions
diff --git a/modules/systemd-credentials.nix b/modules/systemd-credentials.nix
index 33845dc..b8c074c 100644
--- a/modules/systemd-credentials.nix
+++ b/modules/systemd-credentials.nix
@@ -36,11 +36,35 @@
'';
type = lib.types.bool;
};
+ reloadIfCredentialsChange = lib.mkOption {
+ default = false;
+ description = ''
+ Whether to reload the service, or restart it if it isn't running
+ or can't be reloaded, whenever any of its credentials change.
+ Its reload must read the credentials again.
+ '';
+ type = lib.types.bool;
+ };
};
}));
};
+ 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;
@@ -68,9 +92,9 @@
fi
mv "$tmpfile" "$cache"
- systemctl try-restart ${mylib.shell.escape serviceName}
+ systemctl ${if cfg.reloadIfCredentialsChange then "try-reload-or-restart" else "try-restart"} ${mylib.shell.escape serviceName}
'';
};
};
- }) (lib.filterAttrs (_: cfg: cfg.restartIfCredentialsChange) config.hrm.systemd.services));
+ }) (lib.filterAttrs (_: cfg: cfg.restartIfCredentialsChange || cfg.reloadIfCredentialsChange) config.hrm.systemd.services));
}