summaryrefslogtreecommitdiff
path: root/sys/jobs/pki-expiry/default.nix
blob: d0d551fc656d637259d8b7ed7ec749f0e1e35828 (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
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.local.jobs.pkiExpiry;
  inherit (config.local) pki;
in {
  options.local.jobs.pkiExpiry = {
    enable = mkEnableOption "PKI expiration reminder";
  };

  config = mkIf cfg.enable {
    systemd = {
      services.pki-expiry = {
        after = ["postfix.service"];
        path = ["/run/wrappers"];

        environment.PKI_PUBLIC = let
          mkdir = "mkdir -p $out/{ca,cert,crl}";

          cas = mapAttrsToList (_: ca: "ln -s ${ca.cert} $out/ca/${ca.path}") pki.ca;
          crls = mapAttrsToList (_: ca: "ln -s ${ca.crl} $out/crl/${ca.path}") pki.ca;

          certs =
            mapAttrsToList
            (path: leaf: "ln -s ${leaf.cert} $out/cert/${path}")
            (filterAttrs (_: object: ! object ? leaves) pki.byPath);

          pkiPublic = pkgs.runCommandLocal "pki-public" {} (concatLines ([mkdir] ++ cas ++ crls ++ certs));
        in "${pkiPublic}";

        serviceConfig = {
          Type = "oneshot";
          StateDirectory = "pki-expiry";
          WorkingDirectory = "/var/lib/pki-expiry";

          ExecStart = let
            script = pkgs.writeShellApplication {
              name = "pki-expiry";
              text = readFile ./pki-expiry.sh;
              runtimeInputs = with pkgs; [diffutils openssl];
            };
          in "${getExe script}";
        };
      };

      timers.pki-expiry = {
        wantedBy = ["timers.target"];

        timerConfig = {
          OnStartupSec = "10m";
          OnUnitInactiveSec = "3d";
        };
      };
    };
  };
}