1 { pkgs, lib, config, ... }:
2 let cfg = config.gnupg;
4 unlines = builtins.concatStringsSep "\n";
5 unwords = builtins.concatStringsSep " ";
7 generateKeys = keys: unlines (lib.mapAttrsToList generateKey keys);
11 , algo ? "future-default"
19 info " generateKey uid=\"${uid}\""
20 if ! ${gpg-with-home}/bin/gpg-with-home --list-secret-keys -- "=${uid}" >/dev/null 2>/dev/null
22 ${pkgs.pass}/bin/pass "${passPath}" |
23 ${gpg-with-home}/bin/gpg-with-home \
24 --batch --pinentry-mode loopback --passphrase-fd 0 \
25 --quick-generate-key "${uid}" "${algo}" "${unwords usage}" "${expire}"
28 fpr=$(${gpg-fingerprint}/bin/gpg-fingerprint -- "=${uid}" | head1)
29 caps=$(${gpg-with-home}/bin/gpg-with-home \
30 --with-colons --fixed-list-mode --with-fingerprint \
31 --list-secret-keys -- "=${uid}" |
32 ${pkgs.gnugrep}/bin/grep '^ssb:' |
33 ${pkgs.coreutils}/bin/cut -d : -f 12 || true)
35 + unlines (map (generateSubKey primary) subKeys)
36 + generateBackupKey "$fpr" primary
40 { expire ? primary.expire
46 info " generateSubKey usage=[${unwords usage}]"
47 if ! printf '%s\n' "$caps" | ${pkgs.gnugrep}/bin/grep -Fqx "${lettersKeyUsage usage}"
49 ${pkgs.pass}/bin/pass "${primary.passPath}" |
50 ${gpg-with-home}/bin/gpg-with-home \
51 --batch --pinentry-mode loopback --passphrase-fd 0 \
52 --quick-add-key "$fpr" "${algo}" "${unwords usage}" "${expire}"
58 , backupRecipients ? []
62 lib.optionalString (backupRecipients != [])
64 info " generateBackupKey backupRecipients=[${unwords (map (s: "\\\"${s}\\\"") backupRecipients)}]"
65 mkdir -p "${cfg.gnupgHome}/backup/${uid}/"
66 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.pubkey.asc"
68 ${gpg-with-home}/bin/gpg-with-home \
70 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.pubkey.asc" \
71 --export-options export-backup \
74 '' + (if backupRecipients == [""] then
76 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.revoke.asc"
78 ${pkgs.pass}/bin/pass "${passPath}" |
79 ${gpg-with-home}/bin/gpg-with-home \
80 --pinentry-mode loopback --passphrase-fd 0 \
81 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.revoke.asc" \
84 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.privkey.sec"
86 ${pkgs.pass}/bin/pass "${passPath}" |
87 ${gpg-with-home}/bin/gpg-with-home \
88 --batch --pinentry-mode loopback --passphrase-fd 0 \
89 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.privkey.sec" \
90 --export-options export-backup \
91 --export-secret-key "${fpr}"
93 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.subkeys.sec"
95 ${pkgs.pass}/bin/pass "${passPath}" |
96 ${gpg-with-home}/bin/gpg-with-home \
97 --batch --pinentry-mode loopback --passphrase-fd 0 \
98 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.subkeys.sec" \
99 --export-options export-backup \
100 --export-secret-subkeys "${fpr}"
103 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.revoke.asc.gpg"
105 ${pkgs.pass}/bin/pass "${passPath}" |
106 ${gpg-with-home}/bin/gpg-with-home \
107 --pinentry-mode loopback --passphrase-fd 0 \
108 --armor --gen-revoke "${fpr}" |
109 gpg --encrypt ${recipients backupRecipients} \
110 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.revoke.asc.gpg"
112 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.privkey.sec.gpg"
114 ${pkgs.pass}/bin/pass "${passPath}" |
115 ${gpg-with-home}/bin/gpg-with-home \
116 --batch --pinentry-mode loopback --passphrase-fd 0 \
117 --armor --export-options export-backup \
118 --export-secret-key "${fpr}" |
119 gpg --encrypt ${recipients backupRecipients} \
120 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.privkey.sec.gpg"
122 if ! test -s "${cfg.gnupgHome}/backup/${uid}/${fpr}.subkeys.sec.gpg"
124 ${pkgs.pass}/bin/pass "${passPath}" |
125 ${gpg-with-home}/bin/gpg-with-home \
126 --batch --pinentry-mode loopback --passphrase-fd 0 \
127 --armor --export-options export-backup \
128 --export-secret-subkeys "${fpr}" |
129 gpg --encrypt ${recipients backupRecipients} \
130 --armor --yes --output "${cfg.gnupgHome}/backup/${uid}/${fpr}.subkeys.sec.gpg"
133 recipients = rs: unwords (map (r: ''--recipient "${refKey r}"'') rs);
134 refKey = key: if builtins.typeOf key == "string" then key else "=${key.uid}";
135 signer = s: if s == null
137 else ''--sign --default-key "${refKey s}"'';
138 lettersKeyUsage = usage:
139 (if builtins.elem "encrypt" usage then "e" else "") +
140 (if builtins.elem "sign" usage then "s" else "") +
141 (if builtins.elem "cert" usage then "c" else "") +
142 (if builtins.elem "auth" usage then "a" else "");
144 passOfFingerprint = key:
146 # which fills a map from the fingerprints of the given key
147 # to its password file.
149 # shell.gnupg.pass.passOfFingerprint
150 for fpr in $(${gpg-fingerprint}/bin/gpg-fingerprint -- "=${key.uid}")
151 do eval "pass_$fpr=\"${key.passPath}\""
156 # which installs an exit and keyboard interruption (^C) trap
157 # removing any pass from gpg-agent
158 # whose keygrip is registered in $keygrips.
163 for keygrip in $keygrips
165 echo >&2 "gpg: forget: keygrip=$keygrip"
166 GNUPGHOME=${cfg.gnupgHome} \
167 ${pkgs.gnupg}/bin/gpg-connect-agent </dev/null >&2 "CLEAR_PASSPHRASE $keygrip" ||
172 trap 'forgetPass' EXIT INT
174 presetPass = keys: uid:
176 # which preset the pass of given uid into gpg-agent,
177 # using keys to find where the pass is stored.
179 ${unlines (map passOfFingerprint keys)}
181 GNUPGHOME=${cfg.gnupgHome} \
182 ${pkgs.gnupg}/bin/gpgconf --launch gpg-agent
184 fpr="$(${gpg-fingerprint}/bin/fingerprint -- "${uid}" | head1)"
185 eval pass="\''${pass_$fpr}"
188 for keygrip in $(${cfg.gpg-keygrip}/bin/gpg-keygrip -- "$fpr")
190 keygrips="$keygrips $keygrip"
191 echo >&2 "gpg: preset: keygrip=$keygrip pass=$pass"
192 ${pkgs.pass}/bin/pass "$pass" |
193 GNUPGHOME=${cfg.gnupgHome} \
194 ${pkgs.gnupg}/libexec/gpg-preset-passphrase --preset ''${XTRACE:+--verbose} $keygrip
202 cat >/dev/null # NOTE: consuming all the input avoids useless triggering of pipefail
212 # A wrapper around gpg to set GNUPGHOME.
213 gpg-with-home = pkgs.writeScriptBin "gpg-with-home" ''
214 GNUPGHOME=${cfg.gnupgHome} \
215 exec ${pkgs.gnupg}/bin/gpg "$@"
218 # A wrapper around gpg to get fingerprints.
219 gpg-fingerprint = pkgs.writeScriptBin "gpg-fingerprint" ''
221 ${gpg-with-home}/bin/gpg-with-home \
222 --with-colons --fixed-list-mode --with-fingerprint --with-subkey-fingerprint \
223 --list-public-keys "$@" |
224 while IFS=: read -r t x x x key x x x x uid x
227 while IFS=: read -r t x x x x x x x x fpr x
228 do case $t in (fpr) printf '%s\n' "$fpr"; break;;
234 # A wrapper around gpg to get keygrips.
235 gpg-keygrip = pkgs.writeScriptBin "gpg-keygrip" ''
237 ${gpg-with-home}/bin/gpg-with-home \
238 --with-colons --fixed-list-mode --with-keygrip \
239 --list-public-keys "$@" |
240 while IFS=: read -r t x x x key x x x x uid x
243 while IFS=: read -r t x x x x x x x x grp x
244 do case $t in (grp) printf '%s\n' "$grp"; break;;
250 # A wrapper around gpg to get uids.
251 gpg-uid = pkgs.writeScriptBin "gpg-uid" ''
253 ${gpg-with-home}/bin/gpg-with-home \
254 --with-colons --fixed-list-mode \
255 --list-public-keys "$@" |
256 while IFS=: read -r t st x x x x x id x uid x
260 (u) printf '%s\n' "$uid";;
266 # Initialize the keyring according to cfg.keys.
267 gpg-init = pkgs.writeShellScriptBin "gpg-init" (''
272 ${pkgs.coreutils}/bin/install -dm0700 -D ${cfg.gnupgHome}
273 ${pkgs.coreutils}/bin/ln -snf ${cfg.gpgConf} ${cfg.gnupgHome}/gpg.conf
274 ${pkgs.coreutils}/bin/ln -snf ${cfg.gpgAgentConf} ${cfg.gnupgHome}/gpg-agent.conf
275 ${pkgs.coreutils}/bin/ln -snf ${cfg.dirmngrConf} ${cfg.gnupgHome}/dirmngr.conf
277 generateKeys cfg.keys);
281 enable = lib.mkEnableOption "GnuPG shell utilities";
282 gnupgHome = lib.mkOption {
284 default = "sec/gnupg";
288 keys = lib.mkOption {
291 { "John Doe. <contact@example.coop>" = {
294 usage = ["cert" "sign"];
295 passPath = "example.coop/gpg/contact";
297 { algo = "rsa4096"; expire = "1y"; usage = ["sign"];}
298 { algo = "rsa4096"; expire = "1y"; usage = ["encrypt"];}
299 { algo = "rsa4096"; expire = "1y"; usage = ["auth"];}
301 backupRecipients = ["@john@doe.pro"];
304 type = types.attrsOf (types.submodule ({uid, ...}: {
305 #config.uid = lib.mkDefault uid;
309 example = "John Doe <john.doe@example.coop>";
315 algo = lib.mkOption {
316 type = types.enum [ "rsa4096" ];
317 default = "future-default";
320 Cryptographic algorithm.
323 expire = lib.mkOption {
331 usage = lib.mkOption {
332 type = with types; listOf (enum [ "cert" "sign" "encrypt" "auth" "default" ]);
333 default = ["default"];
334 example = ["cert" "sign" "encrypt" "auth"];
339 passPath = lib.mkOption {
341 example = "gnupg/coop/example/contact@";
346 subKeys = lib.mkOption {
347 type = types.listOf (types.submodule {
349 algo = lib.mkOption {
350 type = types.enum [ "rsa4096" ];
354 Cryptographic algorithm.
357 expire = lib.mkOption {
365 usage = lib.mkOption {
366 type = with types; listOf (enum [ "sign" "encrypt" "auth" "default" ]);
367 default = ["default"];
368 example = ["sign" "encrypt" "auth"];
376 backupRecipients = lib.mkOption {
377 type = with types; listOf str;
379 example = ["@john@doe.pro"];
381 Backup keys used to encrypt the a backup copy of the secret keys.
387 dirmngrConf = lib.mkOption {
389 apply = s: pkgs.writeText "dirmngr.conf" s;
392 hkp-cacert ${cfg.keyserverPEM}
393 keyserver hkps://keys.mayfirst.org
395 #log-file ${cfg.gnupgHome}/dirmngr.log
399 GnuPG's dirmngr.conf content.
402 keyserverPEM = lib.mkOption {
404 apply = s: pkgs.writeText "keyserver.pem" s;
405 default = builtins.readFile gnupg/keyserver.pem;
407 dirmngr's hkp-cacert content.
410 gpgAgentConf = lib.mkOption {
412 apply = s: pkgs.writeText "gpg-agent.conf" s;
414 allow-preset-passphrase
415 default-cache-ttl 17200
416 default-cache-ttl-ssh 17200
419 max-cache-ttl-ssh 17200
422 GnuPG's gpg-agent.conf content.
425 gpgConf = lib.mkOption {
427 apply = s: pkgs.writeText "gpg.conf" s;
429 auto-key-locate keyserver
430 cert-digest-algo SHA512
432 default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 TWOFISH BZIP2 ZLIB ZIP Uncompressed
435 keyserver-options no-honor-keyserver-url
439 personal-cipher-preferences AES256 AES CAST5
440 personal-digest-preferences SHA512
442 s2k-cipher-algo AES256
444 s2k-digest-algo SHA512
446 tofu-default-policy unknown
452 GnuPG's gpg.conf content.
456 config = lib.mkIf cfg.enable {
457 nix-shell.buildInputs = [
464 nix-shell.shellHook = ''
466 export GNUPGHOME=${cfg.gnupgHome}
467 install -dm700 "$GNUPGHOME"
468 export GPG_TTY=$(${pkgs.coreutils}/bin/tty)
469 ${pkgs.gnupg}/bin/gpgconf --launch gpg-agent
470 export SSH_AUTH_SOCK=$(${pkgs.gnupg}/bin/gpgconf --list-dirs agent-ssh-socket)