summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/secret.nix
blob: c35dceba389409401a73ee48253449718c93b3a9 (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
55
56
57
58
59
60
61
62
{ config, lib, pkgs, ... }:
with import ../../lib/pure.nix { inherit lib; }; let
  cfg = config.krebs.secret;
in {
  options.krebs.secret = {
    directory = mkOption {
      default = toString <secrets>;
      type = types.absolute-pathname;
    };
    files = mkOption {
      type = with pkgs.stockholm.lib.types; attrsOf secret-file;
      default = {};
      apply = mapAttrs (name: secret-file:
        if types.absolute-pathname.check secret-file.source-path then
          secret-file
        else
          secret-file // {
            source-path = "${config.krebs.secret.directory}/secret-file.source-path";
          }
      );
    };
  };
  config = lib.mkIf (cfg.files != {}) {
    systemd.paths =
      mapAttrs'
        (name: file: nameValuePair "secret-trigger-${systemd.encodeName name}" {
          wantedBy = ["multi-user.target"];
          pathConfig.PathChanged = file.source-path;
        })
        cfg.files;
    systemd.services =
      mapAttrs'
        (name: file: nameValuePair "secret-trigger-${systemd.encodeName name}" {
          serviceConfig = {
            Type = "oneshot";
            ExecStart = "${pkgs.systemd}/bin/systemctl restart ${shell.escape file.service}";
          };
        })
        cfg.files
      //
      mapAttrs'
        (name: file: nameValuePair "secret-${systemd.encodeName name}" {
          wantedBy = ["multi-user.target"];
          serviceConfig = {
            Type = "oneshot";
            RemainAfterExit = "yes";
            ExecStart = toString [
              "${pkgs.coreutils}/bin/install"
              "-D"
              "--compare"
              "--verbose"
              "--mode=${file.mode}"
              "--owner=${file.owner.name}"
              "--group=${file.group-name}"
              file.source-path
              file.path
            ];
          };
        })
        cfg.files;
  };
}