summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-01 00:46:56 +0000
committertv <tv@krebsco.de>2026-12-01 01:33:34 +0000
commit8085f764b6e344d5eae5f2aaf710ae4f3e4098ea (patch)
treeee9c8d3488d971a79739dee2ff4481f3be378bab
parent10eb6546fc88c73154d622048248266c64b3c53a (diff)
systemd-credentials: make the options part of systemd.servicesHEADmaster
systemd.services.<name>.restartIfCredentialsChange and reloadIfCredentialsChange replace hrm.systemd.services.<name>.…; 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.
-rw-r--r--configs/retiolum.nix2
-rw-r--r--modules/charybdis/default.nix2
-rw-r--r--modules/continuum.nix7
-rw-r--r--modules/ejabberd/default.nix2
-rw-r--r--modules/exim-smarthost.nix2
-rw-r--r--modules/systemd-credentials.nix108
-rw-r--r--modules/unbound.nix2
-rw-r--r--modules/x0vncserver.nix2
-rw-r--r--systems/ne/config.nix2
9 files changed, 60 insertions, 69 deletions
diff --git a/configs/retiolum.nix b/configs/retiolum.nix
index 998cbccb..e7478f94 100644
--- a/configs/retiolum.nix
+++ b/configs/retiolum.nix
@@ -60,7 +60,7 @@ in {
StrictSubnets = yes
'';
};
- hrm.systemd.services.tincr-retiolum.restartIfCredentialsChange = true;
+ systemd.services.tincr-retiolum.restartIfCredentialsChange = true;
# tincd doesn't set the MTU. This is what kartei's retiolum module uses,
# measured across the mesh, so paths that lose large packets don't
# stall TCP.
diff --git a/modules/charybdis/default.nix b/modules/charybdis/default.nix
index 9c7465f2..5e8ccd31 100644
--- a/modules/charybdis/default.nix
+++ b/modules/charybdis/default.nix
@@ -38,7 +38,7 @@ in {
environment.etc."charybdis-ircd.motd".text = cfg.motd;
- hrm.systemd.services.charybdis.restartIfCredentialsChange = true;
+ systemd.services.charybdis.restartIfCredentialsChange = true;
systemd.services.charybdis = {
wantedBy = [ "multi-user.target" ];
diff --git a/modules/continuum.nix b/modules/continuum.nix
index e554e3c6..c8273276 100644
--- a/modules/continuum.nix
+++ b/modules/continuum.nix
@@ -92,15 +92,10 @@ in {
});
};
- config.hrm.systemd.services = lib.mapAttrs' (name: instance:
- lib.nameValuePair "continuum-${name}" {
- restartIfCredentialsChange = true;
- }
- ) (lib.filterAttrs (_: instance: instance.credentials != {}) cfg.instances);
-
config.systemd.services = lib.mapAttrs' (name: instance:
lib.nameValuePair "continuum-${name}" {
description = "continuum ${name}";
+ restartIfCredentialsChange = instance.credentials != {};
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
diff --git a/modules/ejabberd/default.nix b/modules/ejabberd/default.nix
index d5c1b28f..da139fe2 100644
--- a/modules/ejabberd/default.nix
+++ b/modules/ejabberd/default.nix
@@ -126,7 +126,7 @@ in {
})
];
- hrm.systemd.services.ejabberd.restartIfCredentialsChange = true;
+ systemd.services.ejabberd.restartIfCredentialsChange = true;
systemd.services.ejabberd = {
wantedBy = [ "multi-user.target" ];
diff --git a/modules/exim-smarthost.nix b/modules/exim-smarthost.nix
index 58d88a40..abbe8bd5 100644
--- a/modules/exim-smarthost.nix
+++ b/modules/exim-smarthost.nix
@@ -115,7 +115,7 @@ let
};
imp = {
- hrm.systemd.services.exim.restartIfCredentialsChange = true;
+ systemd.services.exim.restartIfCredentialsChange = true;
systemd.services.exim.serviceConfig.LoadCredential =
map (dkim: "${dkim.domain}.dkim_private_key:${dkim.private_key}") cfg.dkim;
krebs.exim = {
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";
+ };
}
diff --git a/modules/unbound.nix b/modules/unbound.nix
index c7937892..64f69364 100644
--- a/modules/unbound.nix
+++ b/modules/unbound.nix
@@ -73,7 +73,7 @@
tls-service-key = "/run/credentials/unbound.service/tls-service-key";
tls-service-pem = "/run/credentials/unbound.service/tls-service-pem";
};
- hrm.systemd.services.unbound.restartIfCredentialsChange = true;
+ systemd.services.unbound.restartIfCredentialsChange = true;
systemd.services.unbound.serviceConfig.LoadCredential = [
"tls-service-key:/var/lib/acme/${cfg.useACMEHost}/key.pem"
"tls-service-pem:/var/lib/acme/${cfg.useACMEHost}/fullchain.pem"
diff --git a/modules/x0vncserver.nix b/modules/x0vncserver.nix
index 48c8da1a..d0193673 100644
--- a/modules/x0vncserver.nix
+++ b/modules/x0vncserver.nix
@@ -25,7 +25,7 @@ in {
};
};
config = lib.mkIf cfg.enable {
- hrm.systemd.services.x0vncserver.restartIfCredentialsChange = true;
+ systemd.services.x0vncserver.restartIfCredentialsChange = true;
systemd.services.x0vncserver = {
after = [ "graphical.target" ];
requires = [ "graphical.target" ];
diff --git a/systems/ne/config.nix b/systems/ne/config.nix
index 2cc0ad1e..13c5894c 100644
--- a/systems/ne/config.nix
+++ b/systems/ne/config.nix
@@ -214,7 +214,7 @@
'';
};
# secrets ne gencert cgit.ne.r
- hrm.systemd.services.nginx.reloadIfCredentialsChange = true;
+ systemd.services.nginx.reloadIfCredentialsChange = true;
systemd.services.nginx.serviceConfig.LoadCredential = [
"cgit.ne.r.crt:${config.krebs.secret.directory}/cgit.ne.r.crt"
"cgit.ne.r.key:${config.krebs.secret.directory}/cgit.ne.r.key"