summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/systemd.nix
blob: 00538d5f3162d23661f2d7c866c59e5fe5ffed4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{ config, pkgs, ... }: let {
  lib = import ../../lib;

  body.options.krebs.systemd.services = lib.mkOption {
    default = {};
    type = lib.types.attrsOf (lib.types.submodule {
      options = {
        serviceConfig.LoadCredential = lib.mkOption {
          apply = lib.toList;
          type =
            lib.types.either lib.types.str (lib.types.listOf lib.types.str);
        };
      };
    });
  };

  body.config.systemd =
    lib.mkMerge
      (lib.flatten
        (lib.mapAttrsToList (serviceName: cfg: let
          paths =
            lib.filter
              lib.types.absolute-pathname.check
              (map
                (lib.compose [ lib.maybeHead (lib.match "[^:]*:(.*)") ])
                cfg.serviceConfig.LoadCredential);
        in
          lib.singleton {
            services.${serviceName} = {
              serviceConfig = {
                LoadCredential = cfg.serviceConfig.LoadCredential;
              };
            };
          }
          ++
          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));
}