2017-05-25 20:14:39 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.gnupg;
|
|
|
|
|
2018-10-27 15:02:16 +01:00
|
|
|
xserverCfg = config.services.xserver;
|
|
|
|
|
|
|
|
defaultPinentryFlavor =
|
|
|
|
if xserverCfg.desktopManager.lxqt.enable
|
|
|
|
|| xserverCfg.desktopManager.plasma5.enable then
|
|
|
|
"qt"
|
|
|
|
else if xserverCfg.desktopManager.xfce.enable then
|
|
|
|
"gtk2"
|
2019-10-30 06:00:39 +00:00
|
|
|
else if xserverCfg.enable || config.programs.sway.enable then
|
2018-10-27 15:02:16 +01:00
|
|
|
"gnome3"
|
|
|
|
else
|
2022-05-18 17:31:34 +01:00
|
|
|
"curses";
|
2018-10-27 15:02:16 +01:00
|
|
|
|
2017-05-25 20:14:39 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options.programs.gnupg = {
|
2019-03-17 08:49:50 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.gnupg;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.gnupg";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-03-17 08:49:50 +00:00
|
|
|
The gpg package that should be used.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-05-25 20:14:39 +01:00
|
|
|
agent.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-05-25 20:14:39 +01:00
|
|
|
Enables GnuPG agent with socket-activation for every user session.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
agent.enableSSHSupport = mkOption {
|
|
|
|
type = types.bool;
|
2017-06-15 18:40:09 +01:00
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-05-25 20:14:39 +01:00
|
|
|
Enable SSH agent support in GnuPG agent. Also sets SSH_AUTH_SOCK
|
|
|
|
environment variable correctly. This will disable socket-activation
|
|
|
|
and thus always start a GnuPG agent per user session.
|
|
|
|
'';
|
|
|
|
};
|
2017-06-15 18:40:09 +01:00
|
|
|
|
|
|
|
agent.enableExtraSocket = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-06-15 18:40:09 +01:00
|
|
|
Enable extra socket for GnuPG agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
agent.enableBrowserSocket = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-06-15 18:40:09 +01:00
|
|
|
Enable browser socket for GnuPG agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-10-27 15:02:16 +01:00
|
|
|
agent.pinentryFlavor = mkOption {
|
|
|
|
type = types.nullOr (types.enum pkgs.pinentry.flavors);
|
|
|
|
example = "gnome3";
|
2020-07-09 22:18:24 +01:00
|
|
|
default = defaultPinentryFlavor;
|
2022-08-20 21:27:20 +01:00
|
|
|
defaultText = literalMD ''matching the configured desktop environment'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-10-27 15:02:16 +01:00
|
|
|
Which pinentry interface to use. If not null, the path to the
|
|
|
|
pinentry binary will be passed to gpg-agent via commandline and
|
|
|
|
thus overrides the pinentry option in gpg-agent.conf in the user's
|
|
|
|
home directory.
|
|
|
|
If not set at all, it'll pick an appropriate flavor depending on the
|
2018-10-27 15:03:13 +01:00
|
|
|
system configuration (qt flavor for lxqt and plasma5, gtk2 for xfce
|
2018-10-27 15:02:16 +01:00
|
|
|
4.12, gnome3 on all other systems with X enabled, ncurses otherwise).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-06-15 18:40:09 +01:00
|
|
|
dirmngr.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-06-15 18:40:09 +01:00
|
|
|
Enables GnuPG network certificate management daemon with socket-activation for every user session.
|
|
|
|
'';
|
|
|
|
};
|
2017-05-25 20:14:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.agent.enable {
|
2018-10-27 15:02:16 +01:00
|
|
|
# This overrides the systemd user unit shipped with the gnupg package
|
|
|
|
systemd.user.services.gpg-agent = mkIf (cfg.agent.pinentryFlavor != null) {
|
|
|
|
serviceConfig.ExecStart = [ "" ''
|
2020-01-13 19:10:32 +00:00
|
|
|
${cfg.package}/bin/gpg-agent --supervised \
|
2018-10-27 15:02:16 +01:00
|
|
|
--pinentry-program ${pkgs.pinentry.${cfg.agent.pinentryFlavor}}/bin/pinentry
|
|
|
|
'' ];
|
|
|
|
};
|
|
|
|
|
2017-05-25 20:14:39 +01:00
|
|
|
systemd.user.sockets.gpg-agent = {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.user.sockets.gpg-agent-ssh = mkIf cfg.agent.enableSSHSupport {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
2017-06-15 18:40:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.user.sockets.gpg-agent-extra = mkIf cfg.agent.enableExtraSocket {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.user.sockets.gpg-agent-browser = mkIf cfg.agent.enableBrowserSocket {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
};
|
|
|
|
|
2017-06-27 00:33:23 +01:00
|
|
|
systemd.user.sockets.dirmngr = mkIf cfg.dirmngr.enable {
|
2017-06-15 18:40:09 +01:00
|
|
|
wantedBy = [ "sockets.target" ];
|
2017-05-25 20:14:39 +01:00
|
|
|
};
|
2018-10-27 15:02:16 +01:00
|
|
|
|
2019-10-31 20:15:59 +00:00
|
|
|
services.dbus.packages = mkIf (cfg.agent.pinentryFlavor == "gnome3") [ pkgs.gcr ];
|
|
|
|
|
2019-08-24 19:56:52 +01:00
|
|
|
environment.systemPackages = with pkgs; [ cfg.package ];
|
2019-03-17 08:49:50 +00:00
|
|
|
systemd.packages = [ cfg.package ];
|
2017-05-25 20:14:39 +01:00
|
|
|
|
2018-10-21 10:19:04 +01:00
|
|
|
environment.interactiveShellInit = ''
|
2017-05-25 20:14:39 +01:00
|
|
|
# Bind gpg-agent to this TTY if gpg commands are used.
|
|
|
|
export GPG_TTY=$(tty)
|
2022-08-27 22:18:00 +01:00
|
|
|
'';
|
2017-05-25 20:14:39 +01:00
|
|
|
|
2022-08-27 22:18:00 +01:00
|
|
|
programs.ssh.extraConfig = optionalString cfg.agent.enableSSHSupport ''
|
|
|
|
# The SSH agent protocol doesn't have support for changing TTYs; however we
|
|
|
|
# can simulate this with the `exec` feature of openssh (see ssh_config(5))
|
|
|
|
# that hooks a command to the shell currently running the ssh program.
|
2022-09-04 11:39:21 +01:00
|
|
|
Match host * exec "${cfg.package}/bin/gpg-connect-agent --quiet updatestartuptty /bye >/dev/null 2>&1"
|
2022-08-27 22:18:00 +01:00
|
|
|
'';
|
2017-05-25 20:14:39 +01:00
|
|
|
|
2019-02-23 18:20:12 +00:00
|
|
|
environment.extraInit = mkIf cfg.agent.enableSSHSupport ''
|
2017-05-25 20:14:39 +01:00
|
|
|
if [ -z "$SSH_AUTH_SOCK" ]; then
|
2019-03-17 08:49:50 +00:00
|
|
|
export SSH_AUTH_SOCK=$(${cfg.package}/bin/gpgconf --list-dirs agent-ssh-socket)
|
2017-05-25 20:14:39 +01:00
|
|
|
fi
|
2019-02-23 18:20:12 +00:00
|
|
|
'';
|
2017-05-25 20:14:39 +01:00
|
|
|
|
|
|
|
assertions = [
|
2017-07-11 00:24:13 +01:00
|
|
|
{ assertion = cfg.agent.enableSSHSupport -> !config.programs.ssh.startAgent;
|
2017-05-25 20:14:39 +01:00
|
|
|
message = "You can't use ssh-agent and GnuPG agent with SSH support enabled at the same time!";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2021-11-18 23:26:27 +00:00
|
|
|
# uses attributes of the linked package
|
|
|
|
meta.buildDocsInSandbox = false;
|
2017-05-25 20:14:39 +01:00
|
|
|
}
|