2019-10-25 17:12:40 +02:00
|
|
|
{ config, options, lib, pkgs, ... }:
|
2017-01-07 19:16:26 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.gpg-agent;
|
|
|
|
|
2019-10-25 17:12:40 +02:00
|
|
|
homedir = config.programs.gpg.homedir;
|
|
|
|
|
2017-08-21 15:01:36 +02:00
|
|
|
gpgInitStr = ''
|
|
|
|
GPG_TTY="$(tty)"
|
|
|
|
export GPG_TTY
|
2018-03-04 22:18:17 +01:00
|
|
|
''
|
|
|
|
+ optionalString cfg.enableSshSupport
|
|
|
|
"${pkgs.gnupg}/bin/gpg-connect-agent updatestartuptty /bye > /dev/null";
|
2017-08-21 15:01:36 +02:00
|
|
|
|
2019-10-25 17:12:40 +02:00
|
|
|
# mimic `gpgconf` output for use in `systemd` unit definitions.
|
|
|
|
# we cannot use `gpgconf` directly because it heavily depends on system
|
|
|
|
# state, but we need the values at build time. original:
|
|
|
|
# https://github.com/gpg/gnupg/blob/c6702d77d936b3e9d91b34d8fdee9599ab94ee1b/common/homedir.c#L672-L681
|
|
|
|
gpgconf = dir: let
|
|
|
|
f = pkgs.runCommand dir {} ''
|
|
|
|
PATH=${pkgs.coreutils}/bin:${pkgs.xxd}/bin:$PATH
|
|
|
|
|
|
|
|
if [[ ${homedir} = ${options.programs.gpg.homedir.default} ]]
|
|
|
|
then
|
|
|
|
echo -n "%t/gnupg/${dir}" > $out
|
|
|
|
else
|
|
|
|
hash=$(echo -n ${homedir} | sha1sum -b | xxd -r -p | base32 | \
|
|
|
|
cut -c -24 | tr '[:upper:]' '[:lower:]' | \
|
|
|
|
tr abcdefghijklmnopqrstuvwxyz234567 \
|
|
|
|
ybndrfg8ejkmcpqxot1uwisza345h769)
|
|
|
|
echo -n "%t/gnupg/d.$hash/${dir}" > $out
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
in "${builtins.readFile f}";
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2017-09-26 23:40:31 +02:00
|
|
|
meta.maintainers = [ maintainers.rycee ];
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
options = {
|
|
|
|
services.gpg-agent = {
|
|
|
|
enable = mkEnableOption "GnuPG private key agent";
|
|
|
|
|
|
|
|
defaultCacheTtl = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
2017-09-24 19:39:41 +02:00
|
|
|
Set the time a cache entry is valid to the given number of
|
|
|
|
seconds.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultCacheTtlSsh = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Set the time a cache entry used for SSH keys is valid to the
|
|
|
|
given number of seconds.
|
2017-01-07 19:16:26 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-06-28 19:33:47 +02:00
|
|
|
maxCacheTtl = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Set the maximum time a cache entry is valid to n seconds. After this
|
|
|
|
time a cache entry will be expired even if it has been accessed
|
|
|
|
recently or has been set using gpg-preset-passphrase. The default is
|
|
|
|
2 hours (7200 seconds).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
maxCacheTtlSsh = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Set the maximum time a cache entry used for SSH keys is valid to n
|
|
|
|
seconds. After this time a cache entry will be expired even if it has
|
|
|
|
been accessed recently or has been set using gpg-preset-passphrase.
|
|
|
|
The default is 2 hours (7200 seconds).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
enableSshSupport = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2017-09-24 19:39:41 +02:00
|
|
|
description = ''
|
|
|
|
Whether to use the GnuPG key agent for SSH keys.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-06-18 13:59:40 +02:00
|
|
|
sshKeys = mkOption {
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Which GPG keys (by keygrip) to expose as SSH keys.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-03-11 22:46:41 +01:00
|
|
|
enableExtraSocket = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable extra socket of the GnuPG key agent (useful for GPG
|
|
|
|
Agent forwarding).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verbose = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to produce verbose output.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-09-24 19:39:41 +02:00
|
|
|
grabKeyboardAndMouse = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Tell the pinentry to grab the keyboard and mouse. This
|
|
|
|
option should in general be used to avoid X-sniffing
|
|
|
|
attacks. When disabled, this option passes
|
|
|
|
<option>no-grab</option> setting to gpg-agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableScDaemon = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Make use of the scdaemon tool. This option has the effect of
|
|
|
|
enabling the ability to do smartcard operations. When
|
|
|
|
disabled, this option passes
|
|
|
|
<option>disable-scdaemon</option> setting to gpg-agent.
|
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
2018-08-18 20:57:09 +02:00
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
allow-emacs-pinentry
|
|
|
|
allow-loopback-pinentry
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Extra configuration lines to append to the gpg-agent
|
|
|
|
configuration file.
|
|
|
|
'';
|
|
|
|
};
|
2019-12-30 15:24:07 +01:00
|
|
|
|
|
|
|
pinentryFlavor = mkOption {
|
|
|
|
type = types.nullOr (types.enum pkgs.pinentry.flavors);
|
|
|
|
example = "gnome3";
|
|
|
|
default = "gtk2";
|
|
|
|
description = ''
|
|
|
|
Which pinentry interface to use. If not
|
|
|
|
<literal>null</literal>, it sets
|
|
|
|
<option>pinentry-program</option> in
|
|
|
|
<filename>gpg-agent.conf</filename>. Beware that
|
|
|
|
<literal>pinentry-gnome3</literal> may not work on non-Gnome
|
|
|
|
systems. You can fix it by adding the following to your
|
|
|
|
system configuration:
|
|
|
|
<programlisting language="nix">
|
|
|
|
services.dbus.packages = [ pkgs.gcr ];
|
|
|
|
</programlisting>
|
|
|
|
For this reason, the default is <literal>gtk2</literal> for
|
|
|
|
now.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{
|
2019-10-25 17:12:40 +02:00
|
|
|
home.file."${homedir}/gpg-agent.conf".text = concatStringsSep "\n" (
|
2017-09-24 19:39:41 +02:00
|
|
|
optional (cfg.enableSshSupport) "enable-ssh-support"
|
|
|
|
++
|
|
|
|
optional (!cfg.grabKeyboardAndMouse) "no-grab"
|
|
|
|
++
|
|
|
|
optional (!cfg.enableScDaemon) "disable-scdaemon"
|
2017-06-29 23:33:28 +02:00
|
|
|
++
|
|
|
|
optional (cfg.defaultCacheTtl != null)
|
|
|
|
"default-cache-ttl ${toString cfg.defaultCacheTtl}"
|
2017-09-24 19:39:41 +02:00
|
|
|
++
|
|
|
|
optional (cfg.defaultCacheTtlSsh != null)
|
|
|
|
"default-cache-ttl-ssh ${toString cfg.defaultCacheTtlSsh}"
|
2018-06-28 19:33:47 +02:00
|
|
|
++
|
|
|
|
optional (cfg.maxCacheTtl != null)
|
|
|
|
"max-cache-ttl ${toString cfg.maxCacheTtl}"
|
|
|
|
++
|
|
|
|
optional (cfg.maxCacheTtlSsh != null)
|
|
|
|
"max-cache-ttl-ssh ${toString cfg.maxCacheTtlSsh}"
|
2018-08-18 20:57:09 +02:00
|
|
|
++
|
2019-12-30 15:24:07 +01:00
|
|
|
optional (cfg.pinentryFlavor != null)
|
|
|
|
"pinentry-program ${pkgs.pinentry.${cfg.pinentryFlavor}}/bin/pinentry"
|
|
|
|
++
|
2018-08-18 20:57:09 +02:00
|
|
|
[ cfg.extraConfig ]
|
2017-06-29 23:33:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
home.sessionVariables =
|
|
|
|
optionalAttrs cfg.enableSshSupport {
|
2018-02-08 22:54:29 +01:00
|
|
|
SSH_AUTH_SOCK = "$(${pkgs.gnupg}/bin/gpgconf --list-dirs agent-ssh-socket)";
|
2017-06-29 23:33:28 +02:00
|
|
|
};
|
|
|
|
|
2017-08-21 15:01:36 +02:00
|
|
|
programs.bash.initExtra = gpgInitStr;
|
|
|
|
programs.zsh.initExtra = gpgInitStr;
|
2021-03-03 21:59:13 +01:00
|
|
|
programs.fish.interactiveShellInit = ''
|
|
|
|
set -gx GPG_TTY (tty)
|
|
|
|
'';
|
2017-06-29 23:33:28 +02:00
|
|
|
}
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2019-06-18 13:59:40 +02:00
|
|
|
(mkIf (cfg.sshKeys != null) {
|
|
|
|
# Trailing newlines are important
|
2019-10-25 17:12:40 +02:00
|
|
|
home.file."${homedir}/sshcontrol".text = concatMapStrings (s: "${s}\n") cfg.sshKeys;
|
2019-06-18 13:59:40 +02:00
|
|
|
})
|
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
# The systemd units below are direct translations of the
|
|
|
|
# descriptions in the
|
|
|
|
#
|
|
|
|
# ${pkgs.gnupg}/share/doc/gnupg/examples/systemd-user
|
|
|
|
#
|
|
|
|
# directory.
|
|
|
|
{
|
|
|
|
systemd.user.services.gpg-agent = {
|
|
|
|
Unit = {
|
|
|
|
Description = "GnuPG cryptographic agent and passphrase cache";
|
|
|
|
Documentation = "man:gpg-agent(1)";
|
|
|
|
Requires = "gpg-agent.socket";
|
|
|
|
After = "gpg-agent.socket";
|
|
|
|
# This is a socket-activated service:
|
|
|
|
RefuseManualStart = true;
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
Service = {
|
2018-03-11 22:46:41 +01:00
|
|
|
ExecStart = "${pkgs.gnupg}/bin/gpg-agent --supervised"
|
|
|
|
+ optionalString cfg.verbose " --verbose";
|
2017-06-29 23:33:28 +02:00
|
|
|
ExecReload = "${pkgs.gnupg}/bin/gpgconf --reload gpg-agent";
|
2021-04-19 01:52:31 +02:00
|
|
|
Environment = "GNUPGHOME=${homedir}";
|
2017-06-29 23:33:28 +02:00
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
systemd.user.sockets.gpg-agent = {
|
|
|
|
Unit = {
|
|
|
|
Description = "GnuPG cryptographic agent and passphrase cache";
|
|
|
|
Documentation = "man:gpg-agent(1)";
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket = {
|
2019-10-25 17:12:40 +02:00
|
|
|
ListenStream = gpgconf "S.gpg-agent";
|
2017-06-29 23:33:28 +02:00
|
|
|
FileDescriptorName = "std";
|
|
|
|
SocketMode = "0600";
|
|
|
|
DirectoryMode = "0700";
|
|
|
|
};
|
|
|
|
|
|
|
|
Install = {
|
|
|
|
WantedBy = [ "sockets.target" ];
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
2017-06-29 23:33:28 +02:00
|
|
|
}
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
(mkIf cfg.enableSshSupport {
|
|
|
|
systemd.user.sockets.gpg-agent-ssh = {
|
|
|
|
Unit = {
|
|
|
|
Description = "GnuPG cryptographic agent (ssh-agent emulation)";
|
|
|
|
Documentation = "man:gpg-agent(1) man:ssh-add(1) man:ssh-agent(1) man:ssh(1)";
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket = {
|
2019-10-25 17:12:40 +02:00
|
|
|
ListenStream = gpgconf "S.gpg-agent.ssh";
|
2017-06-29 23:33:28 +02:00
|
|
|
FileDescriptorName = "ssh";
|
|
|
|
Service = "gpg-agent.service";
|
|
|
|
SocketMode = "0600";
|
|
|
|
DirectoryMode = "0700";
|
|
|
|
};
|
|
|
|
|
|
|
|
Install = {
|
|
|
|
WantedBy = [ "sockets.target" ];
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
2017-06-29 23:33:28 +02:00
|
|
|
})
|
2018-03-11 22:46:41 +01:00
|
|
|
|
|
|
|
(mkIf cfg.enableExtraSocket {
|
|
|
|
systemd.user.sockets.gpg-agent-extra = {
|
|
|
|
Unit = {
|
|
|
|
Description = "GnuPG cryptographic agent and passphrase cache (restricted)";
|
|
|
|
Documentation = "man:gpg-agent(1) man:ssh(1)";
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket = {
|
2019-10-25 17:12:40 +02:00
|
|
|
ListenStream = gpgconf "S.gpg-agent.extra";
|
2018-03-11 22:46:41 +01:00
|
|
|
FileDescriptorName = "extra";
|
|
|
|
Service = "gpg-agent.service";
|
|
|
|
SocketMode = "0600";
|
|
|
|
DirectoryMode = "0700";
|
|
|
|
};
|
|
|
|
|
|
|
|
Install = {
|
|
|
|
WantedBy = [ "sockets.target" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
2017-06-29 23:33:28 +02:00
|
|
|
]);
|
2017-01-07 19:16:26 +01:00
|
|
|
}
|