summaryrefslogtreecommitdiffstats
path: root/modules/systemd-credentials.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-18 12:56:08 +0000
committertv <tv@krebsco.de>2026-11-18 14:16:36 +0100
commit6e8bb6c8e3cc18b505184746dd635afd7a5b2bc3 (patch)
tree42137ce889c3a198698f8a507b1bb5fbb771e8b6 /modules/systemd-credentials.nix
parent94961224c07d1989cd90329c32b7c365aa995769 (diff)
systemd-credentials: rename krebs.systemd to hrm.systemd
The file was modules/krebs-systemd.nix to stay clear of modules/systemd.nix. krebs.systemd.services stays as an alias for stockholm's tinc, repo-sync, and exim-smarthost.
Diffstat (limited to 'modules/systemd-credentials.nix')
-rw-r--r--modules/systemd-credentials.nix78
1 files changed, 78 insertions, 0 deletions
diff --git a/modules/systemd-credentials.nix b/modules/systemd-credentials.nix
new file mode 100644
index 0000000..ab7fbd9
--- /dev/null
+++ b/modules/systemd-credentials.nix
@@ -0,0 +1,78 @@
+{ config, pkgs, lib, ... }: let {
+
+ slib = lib.slib or (import ../../lib/pure.nix { inherit lib; });
+
+ # stockholm's tinc, repo-sync, and exim-smarthost still use the old name.
+ body.imports = [
+ (lib.mkAliasOptionModule [ "krebs" "systemd" "services" ] [ "hrm" "systemd" "services" ])
+ ];
+
+ body.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 = {
+ credentialPaths = lib.mkOption {
+ default =
+ lib.sort
+ lib.lessThan
+ (lib.filter
+ slib.types.absolute-pathname.check
+ (map
+ (slib.compose [ slib.maybeHead (builtins.match "[^:]*:(.*)") ])
+ (lib.toList cfg.serviceConfig.LoadCredential)));
+ readOnly = true;
+ };
+ credentialUnitName = lib.mkOption {
+ default = "trigger-${slib.systemd.encodeName serviceName}";
+ readOnly = true;
+ };
+ restartIfCredentialsChange = lib.mkOption {
+ default = false;
+ description = ''
+ Whether to restart the service whenever any of its credentials
+ change. Only credentials with an absolute path in LoadCredential=
+ are supported.
+ '';
+ type = lib.types.bool;
+ };
+ };
+ }));
+ };
+
+ body.config.systemd = lib.mkMerge (lib.mapAttrsToList (serviceName: cfg: {
+ 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
+ ]}
+
+ cache=/var/lib/credentials/${slib.shell.escape serviceName}.sha1sum
+ tmpfile=$(mktemp -t "$(basename "$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"
+
+ systemctl restart ${slib.shell.escape serviceName}
+ '';
+ };
+ };
+ }) config.hrm.systemd.services);
+}