summaryrefslogtreecommitdiff
path: root/sys/jobs/pki-expiry/default.nix
blob: b61d6f5c83331c1a22b853986cc71423d1981d54 (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
{ 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.runCommandNoCCLocal "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";
        };
      };
    };
  };
}