fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Anselm Schülergpg: fix typo (#4277)8d243f7d

master
9.2 KiB322 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.gpg;
7
8  mkKeyValue = key: value:
9    if isString value then "${key} ${value}" else optionalString value key;
10
11  cfgText = generators.toKeyValue {
12    inherit mkKeyValue;
13    listsAsDuplicateKeys = true;
14  } cfg.settings;
15
16  scdaemonCfgText = generators.toKeyValue {
17    inherit mkKeyValue;
18    listsAsDuplicateKeys = true;
19  } cfg.scdaemonSettings;
20
21  primitiveType = types.oneOf [ types.str types.bool ];
22
23  publicKeyOpts = { config, ... }: {
24    options = {
25      text = mkOption {
26        type = types.nullOr types.str;
27        default = null;
28        description = ''
29          Text of an OpenPGP public key.
30        '';
31      };
32
33      source = mkOption {
34        type = types.path;
35        description = ''
36          Path of an OpenPGP public key file.
37        '';
38      };
39
40      trust = mkOption {
41        type = types.nullOr (types.enum [
42          "unknown"
43          1
44          "never"
45          2
46          "marginal"
47          3
48          "full"
49          4
50          "ultimate"
51          5
52        ]);
53        default = null;
54        apply = v:
55          if isString v then
56            {
57              unknown = 1;
58              never = 2;
59              marginal = 3;
60              full = 4;
61              ultimate = 5;
62            }.${v}
63          else
64            v;
65        description = ''
66          The amount of trust you have in the key ownership and the care the
67          owner puts into signing other keys. The available levels are
68
69          `unknown` or `1`
70          : I don't know or won't say.
71
72          `never` or `2`
73          : I do **not** trust.
74
75          `marginal` or `3`
76          : I trust marginally.
77
78          `full` or `4`
79          : I trust fully.
80
81          `ultimate` or `5`
82          : I trust ultimately.
83
84          See the [Key Management chapter](https://www.gnupg.org/gph/en/manual/x334.html)
85          of the GNU Privacy Handbook for more.
86        '';
87      };
88    };
89
90    config = {
91      source =
92        mkIf (config.text != null) (pkgs.writeText "gpg-pubkey" config.text);
93    };
94  };
95
96  importTrustBashFunctions = let gpg = "${cfg.package}/bin/gpg";
97  in ''
98    function gpgKeyId() {
99      ${gpg} --show-key --with-colons "$1" \
100        | grep ^pub: \
101        | cut -d: -f5
102    }
103
104    function importTrust() {
105      local keyIds trust
106      IFS='\n' read -ra keyIds <<< "$(gpgKeyId "$1")"
107      trust="$2"
108      for id in "''${keyIds[@]}" ; do
109        { echo trust; echo "$trust"; (( trust == 5 )) && echo y; echo quit; } \
110          | ${gpg} --no-tty --command-fd 0 --edit-key "$id"
111      done
112    }
113
114  '';
115
116  keyringFiles = let
117    gpg = "${cfg.package}/bin/gpg";
118
119    importKey = { source, trust, ... }: ''
120      ${gpg} --import ${source}
121      ${optionalString (trust != null)
122      ''importTrust "${source}" ${toString trust}''}
123    '';
124
125    importKeys = concatMapStringsSep "\n" importKey cfg.publicKeys;
126  in pkgs.runCommand "gpg-pubring" { buildInputs = [ cfg.package ]; } ''
127    export GNUPGHOME
128    GNUPGHOME=$(mktemp -d)
129
130    ${importTrustBashFunctions}
131    ${importKeys}
132
133    mkdir $out
134    cp $GNUPGHOME/pubring.kbx $out/pubring.kbx
135    if [[ -e $GNUPGHOME/trustdb.gpg ]] ; then
136      cp $GNUPGHOME/trustdb.gpg $out/trustdb.gpg
137    fi
138  '';
139
140in {
141  options.programs.gpg = {
142    enable = mkEnableOption "GnuPG";
143
144    package = mkOption {
145      type = types.package;
146      default = pkgs.gnupg;
147      defaultText = literalExpression "pkgs.gnupg";
148      example = literalExpression "pkgs.gnupg23";
149      description =
150        "The Gnupg package to use (also used by the gpg-agent service).";
151    };
152
153    settings = mkOption {
154      type =
155        types.attrsOf (types.either primitiveType (types.listOf types.str));
156      example = literalExpression ''
157        {
158          no-comments = false;
159          s2k-cipher-algo = "AES128";
160        }
161      '';
162      description = ''
163        GnuPG configuration options. Available options are described
164        in
165        [
166          {manpage}`gpg(1)`
167        ](https://gnupg.org/documentation/manpage.html).
168
169        Note that lists are converted to duplicate keys.
170      '';
171    };
172
173    scdaemonSettings = mkOption {
174      type =
175        types.attrsOf (types.either primitiveType (types.listOf types.str));
176      example = literalExpression ''
177        {
178          disable-ccid = true;
179        }
180      '';
181      description = ''
182        SCdaemon configuration options. Available options are described
183        in
184        [
185          {manpage}`scdaemon(1)`
186        ](https://www.gnupg.org/documentation/manuals/gnupg/Scdaemon-Options.html).
187      '';
188    };
189
190    homedir = mkOption {
191      type = types.path;
192      example = literalExpression ''"''${config.xdg.dataHome}/gnupg"'';
193      default = "${config.home.homeDirectory}/.gnupg";
194      defaultText =
195        literalExpression ''"''${config.home.homeDirectory}/.gnupg"'';
196      description = "Directory to store keychains and configuration.";
197    };
198
199    mutableKeys = mkOption {
200      type = types.bool;
201      default = true;
202      description = ''
203        If set to `true`, you may manage your keyring as a user
204        using the `gpg` command. Upon activation, the keyring
205        will have managed keys added without overwriting unmanaged keys.
206
207        If set to `false`, the path
208        {file}`$GNUPGHOME/pubring.kbx` will become an immutable
209        link to the Nix store, denying modifications.
210      '';
211    };
212
213    mutableTrust = mkOption {
214      type = types.bool;
215      default = true;
216      description = ''
217        If set to `true`, you may manage trust as a user using
218        the {command}`gpg` command. Upon activation, trusted keys have
219        their trust set without overwriting unmanaged keys.
220
221        If set to `false`, the path
222        {file}`$GNUPGHOME/trustdb.gpg` will be
223        *overwritten* on each activation, removing trust for
224        any unmanaged keys. Be careful to make a backup of your old
225        {file}`trustdb.gpg` before switching to immutable trust!
226      '';
227    };
228
229    publicKeys = mkOption {
230      type = types.listOf (types.submodule publicKeyOpts);
231      example = literalExpression ''
232        [ { source = ./pubkeys.txt; } ]
233      '';
234      default = [ ];
235      description = ''
236        A list of public keys to be imported into GnuPG. Note, these key files
237        will be copied into the world-readable Nix store.
238      '';
239    };
240  };
241
242  config = mkIf cfg.enable {
243    programs.gpg.settings = {
244      personal-cipher-preferences = mkDefault "AES256 AES192 AES";
245      personal-digest-preferences = mkDefault "SHA512 SHA384 SHA256";
246      personal-compress-preferences = mkDefault "ZLIB BZIP2 ZIP Uncompressed";
247      default-preference-list = mkDefault
248        "SHA512 SHA384 SHA256 AES256 AES192 AES ZLIB BZIP2 ZIP Uncompressed";
249      cert-digest-algo = mkDefault "SHA512";
250      s2k-digest-algo = mkDefault "SHA512";
251      s2k-cipher-algo = mkDefault "AES256";
252      charset = mkDefault "utf-8";
253      fixed-list-mode = mkDefault true;
254      no-comments = mkDefault true;
255      no-emit-version = mkDefault true;
256      keyid-format = mkDefault "0xlong";
257      list-options = mkDefault "show-uid-validity";
258      verify-options = mkDefault "show-uid-validity";
259      with-fingerprint = mkDefault true;
260      require-cross-certification = mkDefault true;
261      no-symkey-cache = mkDefault true;
262      use-agent = mkDefault true;
263    };
264
265    programs.gpg.scdaemonSettings = {
266      # no defaults for scdaemon
267    };
268
269    home.packages = [ cfg.package ];
270    home.sessionVariables = { GNUPGHOME = cfg.homedir; };
271
272    home.file."${cfg.homedir}/gpg.conf".text = cfgText;
273
274    home.file."${cfg.homedir}/scdaemon.conf".text = scdaemonCfgText;
275
276    # Link keyring if keys are not mutable
277    home.file."${cfg.homedir}/pubring.kbx" =
278      mkIf (!cfg.mutableKeys && cfg.publicKeys != [ ]) {
279        source = "${keyringFiles}/pubring.kbx";
280      };
281
282    home.activation = {
283      createGpgHomedir =
284        hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] ''
285          $DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${escapeShellArg cfg.homedir}
286        '';
287
288      importGpgKeys = let
289        gpg = "${cfg.package}/bin/gpg";
290
291        importKey = { source, trust, ... }:
292          # Import mutable keys
293          optional cfg.mutableKeys
294          "$DRY_RUN_CMD ${gpg} $QUIET_ARG --import ${source}"
295
296          # Import mutable trust
297          ++ optional (trust != null && cfg.mutableTrust)
298          ''$DRY_RUN_CMD importTrust "${source}" ${toString trust}'';
299
300        anyTrust = any (k: k.trust != null) cfg.publicKeys;
301
302        importKeys = concatStringsSep "\n" (concatMap importKey cfg.publicKeys);
303
304        # If any key/trust should be imported then create the block. Otherwise
305        # leave it empty.
306        block = concatStringsSep "\n" (optional (importKeys != "") ''
307          export GNUPGHOME=${escapeShellArg cfg.homedir}
308          if [[ ! -v VERBOSE ]]; then
309            QUIET_ARG="--quiet"
310          else
311            QUIET_ARG=""
312          fi
313          ${importTrustBashFunctions}
314          ${importKeys}
315          unset GNUPGHOME QUIET_ARG keyId importTrust
316        '' ++ optional (!cfg.mutableTrust && anyTrust) ''
317          install -m 0700 ${keyringFiles}/trustdb.gpg "${cfg.homedir}/trustdb.gpg"'');
318      in mkIf (cfg.publicKeys != [ ])
319      (lib.hm.dag.entryAfter [ "linkGeneration" ] block);
320    };
321  };
322}