2019-05-24 09:01:10 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.gpg;
|
|
|
|
|
2021-02-21 06:37:46 +01:00
|
|
|
mkKeyValue = key: value:
|
2022-02-27 02:19:28 +01:00
|
|
|
if isString value then "${key} ${value}" else optionalString value key;
|
2019-05-24 09:01:10 +02:00
|
|
|
|
2021-02-21 06:37:46 +01:00
|
|
|
cfgText = generators.toKeyValue {
|
|
|
|
inherit mkKeyValue;
|
|
|
|
listsAsDuplicateKeys = true;
|
|
|
|
} cfg.settings;
|
|
|
|
|
2021-04-28 20:39:58 +02:00
|
|
|
scdaemonCfgText = generators.toKeyValue {
|
|
|
|
inherit mkKeyValue;
|
|
|
|
listsAsDuplicateKeys = true;
|
|
|
|
} cfg.scdaemonSettings;
|
|
|
|
|
2021-02-21 06:37:46 +01:00
|
|
|
primitiveType = types.oneOf [ types.str types.bool ];
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
publicKeyOpts = { config, ... }: {
|
2019-08-20 10:30:13 +02:00
|
|
|
options = {
|
|
|
|
text = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-08-20 10:30:13 +02:00
|
|
|
Text of an OpenPGP public key.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
source = mkOption {
|
|
|
|
type = types.path;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-08-20 10:30:13 +02:00
|
|
|
Path of an OpenPGP public key file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
trust = mkOption {
|
2022-02-27 02:19:28 +01:00
|
|
|
type = types.nullOr (types.enum [
|
|
|
|
"unknown"
|
|
|
|
1
|
|
|
|
"never"
|
|
|
|
2
|
|
|
|
"marginal"
|
|
|
|
3
|
|
|
|
"full"
|
|
|
|
4
|
|
|
|
"ultimate"
|
|
|
|
5
|
|
|
|
]);
|
2019-08-20 10:30:13 +02:00
|
|
|
default = null;
|
2021-12-16 04:54:56 +01:00
|
|
|
apply = v:
|
|
|
|
if isString v then
|
|
|
|
{
|
|
|
|
unknown = 1;
|
|
|
|
never = 2;
|
|
|
|
marginal = 3;
|
|
|
|
full = 4;
|
|
|
|
ultimate = 5;
|
|
|
|
}.${v}
|
2022-02-27 02:19:28 +01:00
|
|
|
else
|
|
|
|
v;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-08-20 10:30:13 +02:00
|
|
|
The amount of trust you have in the key ownership and the care the
|
|
|
|
owner puts into signing other keys. The available levels are
|
2023-07-01 02:40:42 +02:00
|
|
|
|
|
|
|
`unknown` or `1`
|
|
|
|
: I don't know or won't say.
|
|
|
|
|
|
|
|
`never` or `2`
|
|
|
|
: I do **not** trust.
|
|
|
|
|
|
|
|
`marginal` or `3`
|
|
|
|
: I trust marginally.
|
|
|
|
|
|
|
|
`full` or `4`
|
|
|
|
: I trust fully.
|
|
|
|
|
|
|
|
`ultimate` or `5`
|
|
|
|
: I trust ultimately.
|
|
|
|
|
|
|
|
See the [Key Management chapter](https://www.gnupg.org/gph/en/manual/x334.html)
|
|
|
|
of the GNU Privacy Handbook for more.
|
2019-08-20 10:30:13 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2022-02-27 02:19:28 +01:00
|
|
|
source =
|
|
|
|
mkIf (config.text != null) (pkgs.writeText "gpg-pubkey" config.text);
|
2019-08-20 10:30:13 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
importTrustBashFunctions = let gpg = "${cfg.package}/bin/gpg";
|
|
|
|
in ''
|
|
|
|
function gpgKeyId() {
|
|
|
|
${gpg} --show-key --with-colons "$1" \
|
|
|
|
| grep ^pub: \
|
|
|
|
| cut -d: -f5
|
|
|
|
}
|
|
|
|
|
|
|
|
function importTrust() {
|
2022-04-16 18:45:41 +02:00
|
|
|
local keyIds trust
|
|
|
|
IFS='\n' read -ra keyIds <<< "$(gpgKeyId "$1")"
|
2022-02-27 02:19:28 +01:00
|
|
|
trust="$2"
|
2022-04-16 18:45:41 +02:00
|
|
|
for id in "''${keyIds[@]}" ; do
|
2022-02-27 02:19:28 +01:00
|
|
|
{ echo trust; echo "$trust"; (( trust == 5 )) && echo y; echo quit; } \
|
2022-04-16 18:45:41 +02:00
|
|
|
| ${gpg} --no-tty --command-fd 0 --edit-key "$id"
|
|
|
|
done
|
2022-02-27 02:19:28 +01:00
|
|
|
}
|
2022-04-16 18:45:41 +02:00
|
|
|
|
2019-08-20 10:30:13 +02:00
|
|
|
'';
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
keyringFiles = let
|
|
|
|
gpg = "${cfg.package}/bin/gpg";
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
importKey = { source, trust, ... }: ''
|
|
|
|
${gpg} --import ${source}
|
|
|
|
${optionalString (trust != null)
|
|
|
|
''importTrust "${source}" ${toString trust}''}
|
|
|
|
'';
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
importKeys = concatMapStringsSep "\n" importKey cfg.publicKeys;
|
|
|
|
in pkgs.runCommand "gpg-pubring" { buildInputs = [ cfg.package ]; } ''
|
|
|
|
export GNUPGHOME
|
|
|
|
GNUPGHOME=$(mktemp -d)
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
${importTrustBashFunctions}
|
|
|
|
${importKeys}
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
mkdir $out
|
|
|
|
cp $GNUPGHOME/pubring.kbx $out/pubring.kbx
|
|
|
|
if [[ -e $GNUPGHOME/trustdb.gpg ]] ; then
|
|
|
|
cp $GNUPGHOME/trustdb.gpg $out/trustdb.gpg
|
|
|
|
fi
|
|
|
|
'';
|
2019-08-20 10:30:13 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
in {
|
2019-05-24 09:01:10 +02:00
|
|
|
options.programs.gpg = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "GnuPG";
|
2019-05-24 09:01:10 +02:00
|
|
|
|
2021-04-27 22:40:05 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.gnupg;
|
2021-10-09 11:14:08 +02:00
|
|
|
defaultText = literalExpression "pkgs.gnupg";
|
|
|
|
example = literalExpression "pkgs.gnupg23";
|
2022-02-27 02:19:28 +01:00
|
|
|
description =
|
2023-07-27 18:54:08 +02:00
|
|
|
"The Gnupg package to use (also used by the gpg-agent service).";
|
2021-04-27 22:40:05 +02:00
|
|
|
};
|
|
|
|
|
2019-05-24 09:01:10 +02:00
|
|
|
settings = mkOption {
|
2022-02-27 02:19:28 +01:00
|
|
|
type =
|
|
|
|
types.attrsOf (types.either primitiveType (types.listOf types.str));
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-02-21 06:37:46 +01:00
|
|
|
{
|
|
|
|
no-comments = false;
|
|
|
|
s2k-cipher-algo = "AES128";
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-05-24 09:01:10 +02:00
|
|
|
GnuPG configuration options. Available options are described
|
2023-02-26 09:30:45 +01:00
|
|
|
in
|
2023-07-01 01:30:13 +02:00
|
|
|
[
|
|
|
|
{manpage}`gpg(1)`
|
|
|
|
](https://gnupg.org/documentation/manpage.html).
|
|
|
|
|
2021-05-19 06:58:38 +02:00
|
|
|
Note that lists are converted to duplicate keys.
|
2019-05-24 09:01:10 +02:00
|
|
|
'';
|
|
|
|
};
|
2019-10-25 17:12:40 +02:00
|
|
|
|
2021-04-28 20:39:58 +02:00
|
|
|
scdaemonSettings = mkOption {
|
2022-02-27 02:19:28 +01:00
|
|
|
type =
|
|
|
|
types.attrsOf (types.either primitiveType (types.listOf types.str));
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2021-04-28 20:39:58 +02:00
|
|
|
{
|
|
|
|
disable-ccid = true;
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-04-28 20:39:58 +02:00
|
|
|
SCdaemon configuration options. Available options are described
|
2023-02-26 09:30:45 +01:00
|
|
|
in
|
2023-07-01 01:30:13 +02:00
|
|
|
[
|
|
|
|
{manpage}`scdaemon(1)`
|
|
|
|
](https://www.gnupg.org/documentation/manuals/gnupg/Scdaemon-Options.html).
|
2021-04-28 20:39:58 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-10-25 17:12:40 +02:00
|
|
|
homedir = mkOption {
|
|
|
|
type = types.path;
|
2022-02-27 02:19:28 +01:00
|
|
|
example = literalExpression ''"''${config.xdg.dataHome}/gnupg"'';
|
2019-10-25 17:12:40 +02:00
|
|
|
default = "${config.home.homeDirectory}/.gnupg";
|
2022-02-27 02:19:28 +01:00
|
|
|
defaultText =
|
|
|
|
literalExpression ''"''${config.home.homeDirectory}/.gnupg"'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Directory to store keychains and configuration.";
|
2019-10-25 17:12:40 +02:00
|
|
|
};
|
2019-08-20 10:30:13 +02:00
|
|
|
|
|
|
|
mutableKeys = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
If set to `true`, you may manage your keyring as a user
|
|
|
|
using the `gpg` command. Upon activation, the keyring
|
2019-08-20 10:30:13 +02:00
|
|
|
will have managed keys added without overwriting unmanaged keys.
|
2023-07-01 01:30:13 +02:00
|
|
|
|
|
|
|
If set to `false`, the path
|
|
|
|
{file}`$GNUPGHOME/pubring.kbx` will become an immutable
|
2019-08-20 10:30:13 +02:00
|
|
|
link to the Nix store, denying modifications.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutableTrust = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
If set to `true`, you may manage trust as a user using
|
|
|
|
the {command}`gpg` command. Upon activation, trusted keys have
|
2019-08-20 10:30:13 +02:00
|
|
|
their trust set without overwriting unmanaged keys.
|
2023-07-01 01:30:13 +02:00
|
|
|
|
|
|
|
If set to `false`, the path
|
|
|
|
{file}`$GNUPGHOME/trustdb.gpg` will be
|
|
|
|
*overwritten* on each activation, removing trust for
|
2019-08-20 10:30:13 +02:00
|
|
|
any unmanaged keys. Be careful to make a backup of your old
|
2023-07-01 01:30:13 +02:00
|
|
|
{file}`trustdb.gpg` before switching to immutable trust!
|
2019-08-20 10:30:13 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
publicKeys = mkOption {
|
|
|
|
type = types.listOf (types.submodule publicKeyOpts);
|
|
|
|
example = literalExpression ''
|
|
|
|
[ { source = ./pubkeys.txt; } ]
|
|
|
|
'';
|
|
|
|
default = [ ];
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-08-20 10:30:13 +02:00
|
|
|
A list of public keys to be imported into GnuPG. Note, these key files
|
|
|
|
will be copied into the world-readable Nix store.
|
|
|
|
'';
|
|
|
|
};
|
2019-05-24 09:01:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
programs.gpg.settings = {
|
|
|
|
personal-cipher-preferences = mkDefault "AES256 AES192 AES";
|
|
|
|
personal-digest-preferences = mkDefault "SHA512 SHA384 SHA256";
|
|
|
|
personal-compress-preferences = mkDefault "ZLIB BZIP2 ZIP Uncompressed";
|
2022-02-27 02:19:28 +01:00
|
|
|
default-preference-list = mkDefault
|
|
|
|
"SHA512 SHA384 SHA256 AES256 AES192 AES ZLIB BZIP2 ZIP Uncompressed";
|
2019-05-24 09:01:10 +02:00
|
|
|
cert-digest-algo = mkDefault "SHA512";
|
|
|
|
s2k-digest-algo = mkDefault "SHA512";
|
|
|
|
s2k-cipher-algo = mkDefault "AES256";
|
|
|
|
charset = mkDefault "utf-8";
|
|
|
|
fixed-list-mode = mkDefault true;
|
|
|
|
no-comments = mkDefault true;
|
|
|
|
no-emit-version = mkDefault true;
|
|
|
|
keyid-format = mkDefault "0xlong";
|
|
|
|
list-options = mkDefault "show-uid-validity";
|
|
|
|
verify-options = mkDefault "show-uid-validity";
|
|
|
|
with-fingerprint = mkDefault true;
|
|
|
|
require-cross-certification = mkDefault true;
|
|
|
|
no-symkey-cache = mkDefault true;
|
|
|
|
use-agent = mkDefault true;
|
|
|
|
};
|
|
|
|
|
2021-04-28 20:39:58 +02:00
|
|
|
programs.gpg.scdaemonSettings = {
|
|
|
|
# no defaults for scdaemon
|
|
|
|
};
|
|
|
|
|
2021-04-27 22:40:05 +02:00
|
|
|
home.packages = [ cfg.package ];
|
2022-02-27 02:19:28 +01:00
|
|
|
home.sessionVariables = { GNUPGHOME = cfg.homedir; };
|
2019-05-24 09:01:10 +02:00
|
|
|
|
2019-10-25 17:12:40 +02:00
|
|
|
home.file."${cfg.homedir}/gpg.conf".text = cfgText;
|
2021-04-28 20:39:58 +02:00
|
|
|
|
|
|
|
home.file."${cfg.homedir}/scdaemon.conf".text = scdaemonCfgText;
|
2019-08-20 10:30:13 +02:00
|
|
|
|
|
|
|
# Link keyring if keys are not mutable
|
|
|
|
home.file."${cfg.homedir}/pubring.kbx" =
|
2022-02-27 02:19:28 +01:00
|
|
|
mkIf (!cfg.mutableKeys && cfg.publicKeys != [ ]) {
|
2019-08-20 10:30:13 +02:00
|
|
|
source = "${keyringFiles}/pubring.kbx";
|
|
|
|
};
|
|
|
|
|
2022-04-05 05:40:15 +02:00
|
|
|
home.activation = {
|
|
|
|
createGpgHomedir =
|
|
|
|
hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] ''
|
|
|
|
$DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${escapeShellArg cfg.homedir}
|
|
|
|
'';
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
importGpgKeys = let
|
|
|
|
gpg = "${cfg.package}/bin/gpg";
|
|
|
|
|
|
|
|
importKey = { source, trust, ... }:
|
|
|
|
# Import mutable keys
|
|
|
|
optional cfg.mutableKeys
|
|
|
|
"$DRY_RUN_CMD ${gpg} $QUIET_ARG --import ${source}"
|
|
|
|
|
|
|
|
# Import mutable trust
|
|
|
|
++ optional (trust != null && cfg.mutableTrust)
|
|
|
|
''$DRY_RUN_CMD importTrust "${source}" ${toString trust}'';
|
|
|
|
|
|
|
|
anyTrust = any (k: k.trust != null) cfg.publicKeys;
|
|
|
|
|
|
|
|
importKeys = concatStringsSep "\n" (concatMap importKey cfg.publicKeys);
|
|
|
|
|
|
|
|
# If any key/trust should be imported then create the block. Otherwise
|
|
|
|
# leave it empty.
|
|
|
|
block = concatStringsSep "\n" (optional (importKeys != "") ''
|
|
|
|
export GNUPGHOME=${escapeShellArg cfg.homedir}
|
|
|
|
if [[ ! -v VERBOSE ]]; then
|
|
|
|
QUIET_ARG="--quiet"
|
|
|
|
else
|
|
|
|
QUIET_ARG=""
|
|
|
|
fi
|
|
|
|
${importTrustBashFunctions}
|
|
|
|
${importKeys}
|
|
|
|
unset GNUPGHOME QUIET_ARG keyId importTrust
|
|
|
|
'' ++ optional (!cfg.mutableTrust && anyTrust) ''
|
|
|
|
install -m 0700 ${keyringFiles}/trustdb.gpg "${cfg.homedir}/trustdb.gpg"'');
|
2022-04-05 05:40:15 +02:00
|
|
|
in mkIf (cfg.publicKeys != [ ])
|
|
|
|
(lib.hm.dag.entryAfter [ "linkGeneration" ] block);
|
2019-08-20 10:30:13 +02:00
|
|
|
};
|
2019-05-24 09:01:10 +02:00
|
|
|
};
|
|
|
|
}
|