aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/gnupg/default.nix
blob: 77499900e16e6a8149195a6810681bbfc2c9eee1 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{ config, lib, pkgs, ... }:

let

  inherit (lib)
    concatMapStringsSep concatStrings filterAttrs foldAttrs mapAttrs'
    mapAttrsToList mkOption optionalString ;

  inherit (lib.types)
    attrsOf submodule ;

  explicit = filterAttrs (n: v: n != "_module" && v != null);
  instances = explicit config.nixsap.apps.gnupg;

  keyrings =
    let
      ik = mapAttrsToList (_: i: {
        "${i.user}" = i.secretKeys ++ mapAttrsToList (_: f: f) i.passphrase;
      }) instances;
    in foldAttrs (l: r: l ++ r) [] ik;


  mkService = name: cfg:
    let

      pubring = pkgs.runCommand "gnupg-${name}-pubring.kbx" {} ''
        ${cfg.package}/bin/gpg2 \
          --homedir . \
          --import \
          ${concatMapStringsSep " " (k: "'${k}'") cfg.publicKeys}
        cp pubring.kbx $out
      '';

      start = pkgs.writeBashScriptBin "gnupg-${name}-start" ''
        set -euo pipefail
        umask 0077

        cat <<'CONF' > '${cfg.home}/gpg.conf'
        batch
        no-tty
        trust-model always
        yes
        CONF

        # XXX forking.
        # XXX is 30 years enough?
        ${cfg.package}/bin/gpg-agent \
          --homedir '${cfg.home}' \
          --allow-preset-passphrase \
          --batch \
          --max-cache-ttl 999999999 \
          --quiet \
          --daemon

        ${optionalString (cfg.publicKeys != []) ''
          ln -sf '${pubring}' '${cfg.home}/pubring.kbx'
        ''}

        export GNUPGHOME='${cfg.home}'

        ${optionalString (cfg.secretKeys != []) ''
          ${cfg.package}/bin/gpg2 --import \
            ${concatMapStringsSep " " (k: "'${k}'") cfg.secretKeys}
        ''}


        ${concatStrings (mapAttrsToList (cacheid: f: ''
            head -n 1 '${f}' \
            | ${cfg.package}/libexec/gpg-preset-passphrase \
              --verbose --preset '${cacheid}'
          '') cfg.passphrase)
        }


      '';

    in {
      name = "gnupg-${name}";
      value = {
        description = "gnupg (${name})";
        wantedBy = [ "multi-user.target" ];
        after = [ "keys.target" "local-fs.target" ];
        preStart = ''
          mkdir -p -- '${cfg.home}'
          rm -rf -- '${cfg.home}/'*
          chmod u=rwX,g=,o= -- '${cfg.home}'
          chown '${cfg.user}.${cfg.user}' -- '${cfg.home}'
        '';

        serviceConfig = {
          ExecStart = "${start}/bin/gnupg-${name}-start";
          PermissionsStartOnly = true;
          Restart = "always";
          Type = "forking";
          User = cfg.user;
        };
      };
    };

in {

  options = {
    nixsap.apps.gnupg = mkOption {
      description = "GnuPG instances";
      default = {};
      type = attrsOf (submodule (import ./instance.nix pkgs));
    };
  };

  config = {
    nixsap.deployment.keyrings = keyrings;
    systemd.services = mapAttrs' mkService instances;
  };

}