summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/systemd.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2021-12-23 01:10:22 +0100
committertv <tv@krebsco.de>2021-12-23 01:59:25 +0100
commitd6ebd497f09ed8994594a32f8848d218beea93a3 (patch)
tree2a546c692d1ab5598f300a3762a7e5ba486bd172 /krebs/3modules/systemd.nix
parent9d65a3cdd8d73fd92418ef317b671bd14d105141 (diff)
krebs.systemd.services: restart by LoadCredential
Diffstat (limited to 'krebs/3modules/systemd.nix')
-rw-r--r--krebs/3modules/systemd.nix51
1 files changed, 51 insertions, 0 deletions
diff --git a/krebs/3modules/systemd.nix b/krebs/3modules/systemd.nix
new file mode 100644
index 00000000..c30b2264
--- /dev/null
+++ b/krebs/3modules/systemd.nix
@@ -0,0 +1,51 @@
+{ config, options, pkgs, ... }: let {
+ lib = import ../../lib;
+
+ body.options.krebs.systemd.services = lib.mkOption {
+ default = {};
+ type = lib.types.attrs;
+ description = ''
+ Definition of systemd service units with bonus features.
+
+ Services defined using this option will be restarted whenever any file
+ (described by an absolute path) used in LoadCredential changes.
+ '';
+ };
+
+ body.config.systemd =
+ lib.mkMerge
+ (lib.flatten
+ (lib.mapAttrsToList (serviceName: cfg: let
+ prefix = [ "krebs" "systemd" "services" serviceName ];
+ opts = options.systemd.services.type.getSubOptions prefix;
+
+ paths =
+ lib.filter
+ lib.types.absolute-pathname.check
+ (map
+ (lib.compose [ lib.maybeHead (lib.match "[^:]*:(.*)") ])
+ (cfg.serviceConfig.LoadCredential or []));
+ in
+ lib.singleton {
+ services.${serviceName} = cfg;
+ }
+ ++
+ lib.optionals (cfg.enable or opts.enable.default) (map (path: let
+ triggerName = "trigger-${lib.systemd.encodeName path}";
+ in {
+ paths.${triggerName} = {
+ wantedBy = ["multi-user.target"];
+ pathConfig.PathChanged = path;
+ };
+ services.${triggerName} = {
+ serviceConfig = {
+ Type = "oneshot";
+ ExecStart = lib.singleton (toString [
+ "${pkgs.systemd}/bin/systemctl restart"
+ (lib.shell.escape serviceName)
+ ]);
+ };
+ };
+ }) paths)
+ ) config.krebs.systemd.services));
+}