{ config, pkgs, lib, mylib, ... }: { # stockholm's repo-sync and exim-smarthost still use the old name. imports = [ (lib.mkAliasOptionModule [ "krebs" "systemd" "services" ] [ "hrm" "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 = { credentialPaths = lib.mkOption { default = lib.sort lib.lessThan (lib.filter mylib.types.absolute-pathname.check (map (mylib.compose [ mylib.maybeHead (builtins.match "[^:]*:(.*)") ]) (lib.toList cfg.serviceConfig.LoadCredential))); readOnly = true; }; credentialUnitName = lib.mkOption { default = "trigger-${mylib.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; }; 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; }; 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/${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" 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)); }