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;
|
2021-04-27 22:40:05 +02:00
|
|
|
gpgPkg = config.programs.gpg.package;
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2019-10-25 17:12:40 +02:00
|
|
|
homedir = config.programs.gpg.homedir;
|
|
|
|
|
2023-01-08 05:44:21 +01:00
|
|
|
gpgSshSupportStr = ''
|
|
|
|
${gpgPkg}/bin/gpg-connect-agent updatestartuptty /bye > /dev/null
|
|
|
|
'';
|
|
|
|
|
2017-08-21 15:01:36 +02:00
|
|
|
gpgInitStr = ''
|
|
|
|
GPG_TTY="$(tty)"
|
|
|
|
export GPG_TTY
|
2023-01-08 05:44:21 +01:00
|
|
|
'' + optionalString cfg.enableSshSupport gpgSshSupportStr;
|
|
|
|
|
|
|
|
gpgFishInitStr = ''
|
|
|
|
set -gx GPG_TTY (tty)
|
|
|
|
'' + optionalString cfg.enableSshSupport gpgSshSupportStr;
|
2017-08-21 15:01:36 +02:00
|
|
|
|
2023-11-25 16:56:07 +01:00
|
|
|
gpgNushellInitStr = ''
|
|
|
|
$env.GPG_TTY = (tty)
|
|
|
|
'' + optionalString cfg.enableSshSupport ''
|
|
|
|
${gpgPkg}/bin/gpg-connect-agent updatestartuptty /bye | ignore
|
|
|
|
|
2024-03-06 11:54:01 +01:00
|
|
|
$env.SSH_AUTH_SOCK = ($env.SSH_AUTH_SOCK? | default (${gpgPkg}/bin/gpgconf --list-dirs agent-ssh-socket))
|
2023-11-25 16:56:07 +01: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
|
2021-08-22 11:49:47 +02:00
|
|
|
gpgconf = dir:
|
2021-11-25 20:47:45 +01:00
|
|
|
let
|
2022-02-27 02:19:28 +01:00
|
|
|
hash =
|
|
|
|
substring 0 24 (hexStringToBase32 (builtins.hashString "sha1" homedir));
|
2021-11-25 20:47:45 +01:00
|
|
|
in if homedir == options.programs.gpg.homedir.default then
|
2021-08-22 11:49:47 +02:00
|
|
|
"%t/gnupg/${dir}"
|
|
|
|
else
|
2021-11-25 20:47:45 +01:00
|
|
|
"%t/gnupg/d.${hash}/${dir}";
|
2019-10-25 17:12:40 +02:00
|
|
|
|
2021-11-25 20:47:45 +01:00
|
|
|
# Act like `xxd -r -p | base32` but with z-base-32 alphabet and no trailing padding.
|
|
|
|
# Written in Nix for purity.
|
|
|
|
hexStringToBase32 = let
|
|
|
|
mod = a: b: a - a / b * b;
|
|
|
|
pow2 = elemAt [ 1 2 4 8 16 32 64 128 256 ];
|
|
|
|
splitChars = s: init (tail (splitString "" s));
|
|
|
|
|
|
|
|
base32Alphabet = splitChars "ybndrfg8ejkmcpqxot1uwisza345h769";
|
2022-02-27 02:19:28 +01:00
|
|
|
hexToIntTable = listToAttrs (genList (x: {
|
|
|
|
name = toLower (toHexString x);
|
|
|
|
value = x;
|
|
|
|
}) 16);
|
|
|
|
|
|
|
|
initState = {
|
|
|
|
ret = "";
|
|
|
|
buf = 0;
|
|
|
|
bufBits = 0;
|
|
|
|
};
|
|
|
|
go = { ret, buf, bufBits }:
|
|
|
|
hex:
|
2021-11-25 20:47:45 +01:00
|
|
|
let
|
|
|
|
buf' = buf * pow2 4 + hexToIntTable.${hex};
|
|
|
|
bufBits' = bufBits + 4;
|
|
|
|
extraBits = bufBits' - 5;
|
|
|
|
in if bufBits >= 5 then {
|
|
|
|
ret = ret + elemAt base32Alphabet (buf' / pow2 extraBits);
|
|
|
|
buf = mod buf' (pow2 extraBits);
|
|
|
|
bufBits = bufBits' - 5;
|
|
|
|
} else {
|
|
|
|
ret = ret;
|
|
|
|
buf = buf';
|
|
|
|
bufBits = bufBits';
|
|
|
|
};
|
|
|
|
in hexString: (foldl' go initState (splitChars hexString)).ret;
|
2019-10-25 17:12:40 +02:00
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
in {
|
2017-09-26 23:40:31 +02:00
|
|
|
meta.maintainers = [ maintainers.rycee ];
|
|
|
|
|
2024-01-14 18:09:06 +01:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "gpg-agent" "pinentryFlavor" ]
|
|
|
|
"Use services.gpg-agent.pinentryPackage instead")
|
|
|
|
];
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
options = {
|
|
|
|
services.gpg-agent = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "GnuPG private key agent";
|
2017-01-07 19:16:26 +01:00
|
|
|
|
|
|
|
defaultCacheTtl = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2023-07-02 01:45:18 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-09-24 19:39:41 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-06-28 19:33:47 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-06-28 19:33:47 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-09-24 19:39:41 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-06-18 13:59:40 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-03-11 22:46:41 +01:00
|
|
|
Whether to enable extra socket of the GnuPG key agent (useful for GPG
|
|
|
|
Agent forwarding).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verbose = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-03-11 22:46:41 +01:00
|
|
|
Whether to produce verbose output.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-09-24 19:39:41 +02:00
|
|
|
grabKeyboardAndMouse = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-09-24 19:39:41 +02:00
|
|
|
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
|
2023-07-01 01:30:13 +02:00
|
|
|
{option}`no-grab` setting to gpg-agent.
|
2017-09-24 19:39:41 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableScDaemon = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-09-24 19:39:41 +02:00
|
|
|
Make use of the scdaemon tool. This option has the effect of
|
|
|
|
enabling the ability to do smartcard operations. When
|
|
|
|
disabled, this option passes
|
2023-07-01 01:30:13 +02:00
|
|
|
{option}`disable-scdaemon` setting to gpg-agent.
|
2017-09-24 19:39:41 +02:00
|
|
|
'';
|
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
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-08-18 20:57:09 +02:00
|
|
|
Extra configuration lines to append to the gpg-agent
|
|
|
|
configuration file.
|
|
|
|
'';
|
|
|
|
};
|
2024-01-14 18:09:06 +01:00
|
|
|
pinentryPackage = mkOption {
|
|
|
|
type = types.nullOr types.package;
|
|
|
|
example = literalExpression "pkgs.pinentry-gnome3";
|
2023-12-24 03:27:45 +01:00
|
|
|
default = null;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-12-30 15:24:07 +01:00
|
|
|
Which pinentry interface to use. If not
|
2023-07-01 01:30:13 +02:00
|
|
|
`null`, it sets
|
|
|
|
{option}`pinentry-program` in
|
|
|
|
{file}`gpg-agent.conf`. Beware that
|
|
|
|
`pinentry-gnome3` may not work on non-Gnome
|
2019-12-30 15:24:07 +01:00
|
|
|
systems. You can fix it by adding the following to your
|
|
|
|
system configuration:
|
2023-07-01 01:30:13 +02:00
|
|
|
```nix
|
2019-12-30 15:24:07 +01:00
|
|
|
services.dbus.packages = [ pkgs.gcr ];
|
2023-07-01 01:30:13 +02:00
|
|
|
```
|
2019-12-30 15:24:07 +01:00
|
|
|
'';
|
|
|
|
};
|
2022-05-01 02:38:36 +02:00
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
enableBashIntegration = mkEnableOption "Bash integration" // {
|
2022-05-01 02:38:36 +02:00
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
enableZshIntegration = mkEnableOption "Zsh integration" // {
|
2022-05-01 02:38:36 +02:00
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
enableFishIntegration = mkEnableOption "Fish integration" // {
|
2022-05-01 02:38:36 +02:00
|
|
|
default = true;
|
|
|
|
};
|
2023-11-25 16:56:07 +01:00
|
|
|
|
|
|
|
enableNushellIntegration = mkEnableOption "Nushell integration" // {
|
|
|
|
default = true;
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{
|
2022-02-27 02:19:28 +01:00
|
|
|
home.file."${homedir}/gpg-agent.conf".text = concatStringsSep "\n"
|
|
|
|
(optional (cfg.enableSshSupport) "enable-ssh-support"
|
2022-08-30 20:18:36 +02:00
|
|
|
++ optional cfg.grabKeyboardAndMouse "grab"
|
2022-02-27 02:19:28 +01:00
|
|
|
++ optional (!cfg.enableScDaemon) "disable-scdaemon"
|
|
|
|
++ optional (cfg.defaultCacheTtl != null)
|
2017-06-29 23:33:28 +02:00
|
|
|
"default-cache-ttl ${toString cfg.defaultCacheTtl}"
|
2022-02-27 02:19:28 +01:00
|
|
|
++ optional (cfg.defaultCacheTtlSsh != null)
|
2017-09-24 19:39:41 +02:00
|
|
|
"default-cache-ttl-ssh ${toString cfg.defaultCacheTtlSsh}"
|
2022-02-27 02:19:28 +01:00
|
|
|
++ optional (cfg.maxCacheTtl != null)
|
2018-06-28 19:33:47 +02:00
|
|
|
"max-cache-ttl ${toString cfg.maxCacheTtl}"
|
2022-02-27 02:19:28 +01:00
|
|
|
++ optional (cfg.maxCacheTtlSsh != null)
|
2018-06-28 19:33:47 +02:00
|
|
|
"max-cache-ttl-ssh ${toString cfg.maxCacheTtlSsh}"
|
2024-01-14 18:09:06 +01:00
|
|
|
++ optional (cfg.pinentryPackage != null)
|
2024-03-14 10:01:39 +01:00
|
|
|
"pinentry-program ${lib.getExe cfg.pinentryPackage}"
|
2022-02-27 02:19:28 +01:00
|
|
|
++ [ cfg.extraConfig ]);
|
2017-06-29 23:33:28 +02:00
|
|
|
|
2021-08-21 07:43:41 +02:00
|
|
|
home.sessionVariablesExtra = optionalString cfg.enableSshSupport ''
|
|
|
|
if [[ -z "$SSH_AUTH_SOCK" ]]; then
|
|
|
|
export SSH_AUTH_SOCK="$(${gpgPkg}/bin/gpgconf --list-dirs agent-ssh-socket)"
|
|
|
|
fi
|
|
|
|
'';
|
2017-06-29 23:33:28 +02:00
|
|
|
|
2022-05-01 02:38:36 +02:00
|
|
|
programs.bash.initExtra = mkIf cfg.enableBashIntegration gpgInitStr;
|
|
|
|
programs.zsh.initExtra = mkIf cfg.enableZshIntegration gpgInitStr;
|
2023-01-08 05:44:21 +01:00
|
|
|
programs.fish.interactiveShellInit =
|
|
|
|
mkIf cfg.enableFishIntegration gpgFishInitStr;
|
2023-11-25 16:56:07 +01:00
|
|
|
|
|
|
|
programs.nushell.extraEnv =
|
|
|
|
mkIf cfg.enableNushellIntegration gpgNushellInitStr;
|
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
|
2022-02-27 02:19:28 +01:00
|
|
|
home.file."${homedir}/sshcontrol".text = concatMapStrings (s: ''
|
|
|
|
${s}
|
|
|
|
'') 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
|
|
|
|
#
|
2021-04-27 22:40:05 +02:00
|
|
|
# ${gpgPkg}/share/doc/gnupg/examples/systemd-user
|
2017-06-29 23:33:28 +02:00
|
|
|
#
|
|
|
|
# directory.
|
|
|
|
{
|
2022-04-24 16:25:54 +02:00
|
|
|
assertions = [
|
|
|
|
(hm.assertions.assertPlatform "services.gpg-agent" pkgs platforms.linux)
|
|
|
|
];
|
|
|
|
|
2017-06-29 23:33:28 +02:00
|
|
|
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 = {
|
2021-04-27 22:40:05 +02:00
|
|
|
ExecStart = "${gpgPkg}/bin/gpg-agent --supervised"
|
2018-03-11 22:46:41 +01:00
|
|
|
+ optionalString cfg.verbose " --verbose";
|
2021-04-27 22:40:05 +02:00
|
|
|
ExecReload = "${gpgPkg}/bin/gpgconf --reload gpg-agent";
|
2022-07-10 16:10:55 +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";
|
|
|
|
};
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
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)";
|
2022-02-27 02:19:28 +01:00
|
|
|
Documentation =
|
|
|
|
"man:gpg-agent(1) man:ssh-add(1) man:ssh-agent(1) man:ssh(1)";
|
2017-06-29 23:33:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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";
|
|
|
|
};
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
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 = {
|
2022-02-27 02:19:28 +01:00
|
|
|
Description =
|
|
|
|
"GnuPG cryptographic agent and passphrase cache (restricted)";
|
2018-03-11 22:46:41 +01:00
|
|
|
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";
|
|
|
|
};
|
|
|
|
|
2022-02-27 02:19:28 +01:00
|
|
|
Install = { WantedBy = [ "sockets.target" ]; };
|
2018-03-11 22:46:41 +01:00
|
|
|
};
|
|
|
|
})
|
2017-06-29 23:33:28 +02:00
|
|
|
]);
|
2017-01-07 19:16:26 +01:00
|
|
|
}
|