| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.programs.gnupg; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02: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 02:00:39 -04:00
										 |  |  |     else if xserverCfg.enable || config.programs.sway.enable then | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02:00
										 |  |  |       "gnome3" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options.programs.gnupg = { | 
					
						
							| 
									
										
										
										
											2019-03-17 09:49:50 +01:00
										 |  |  |     package = mkOption { | 
					
						
							|  |  |  |       type = types.package; | 
					
						
							|  |  |  |       default = pkgs.gnupg; | 
					
						
							|  |  |  |       defaultText = "pkgs.gnupg"; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         The gpg package that should be used. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |     agent.enable = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Enables GnuPG agent with socket-activation for every user session. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     agent.enableSSHSupport = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							| 
									
										
										
										
											2017-06-16 03:40:09 +10:00
										 |  |  |       default = false; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |       description = ''
 | 
					
						
							|  |  |  |         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-16 03:40:09 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     agent.enableExtraSocket = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Enable extra socket for GnuPG agent. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     agent.enableBrowserSocket = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Enable browser socket for GnuPG agent. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02:00
										 |  |  |     agent.pinentryFlavor = mkOption { | 
					
						
							|  |  |  |       type = types.nullOr (types.enum pkgs.pinentry.flavors); | 
					
						
							|  |  |  |       example = "gnome3"; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         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 16:03:13 +02:00
										 |  |  |         system configuration (qt flavor for lxqt and plasma5, gtk2 for xfce | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02:00
										 |  |  |         4.12, gnome3 on all other systems with X enabled, ncurses otherwise). | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 03:40:09 +10:00
										 |  |  |     dirmngr.enable = mkOption { | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Enables GnuPG network certificate management daemon with socket-activation for every user session. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.agent.enable { | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02:00
										 |  |  |     programs.gnupg.agent.pinentryFlavor = mkDefault defaultPinentryFlavor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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 16:02:16 +02:00
										 |  |  |           --pinentry-program ${pkgs.pinentry.${cfg.agent.pinentryFlavor}}/bin/pinentry | 
					
						
							|  |  |  |       '' ];
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |     systemd.user.sockets.gpg-agent = { | 
					
						
							|  |  |  |       wantedBy = [ "sockets.target" ]; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     systemd.user.sockets.gpg-agent-ssh = mkIf cfg.agent.enableSSHSupport { | 
					
						
							|  |  |  |       wantedBy = [ "sockets.target" ]; | 
					
						
							| 
									
										
										
										
											2017-06-16 03:40:09 +10: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-26 19:33:23 -04:00
										 |  |  |     systemd.user.sockets.dirmngr = mkIf cfg.dirmngr.enable { | 
					
						
							| 
									
										
										
										
											2017-06-16 03:40:09 +10:00
										 |  |  |       wantedBy = [ "sockets.target" ]; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-10-27 16:02:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 16:15:59 -04:00
										 |  |  |     services.dbus.packages = mkIf (cfg.agent.pinentryFlavor == "gnome3") [ pkgs.gcr ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-24 20:56:52 +02:00
										 |  |  |     environment.systemPackages = with pkgs; [ cfg.package ]; | 
					
						
							| 
									
										
										
										
											2019-03-17 09:49:50 +01:00
										 |  |  |     systemd.packages = [ cfg.package ]; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 10:19:04 +01:00
										 |  |  |     environment.interactiveShellInit = ''
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |       # Bind gpg-agent to this TTY if gpg commands are used. | 
					
						
							|  |  |  |       export GPG_TTY=$(tty) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     '' + (optionalString cfg.agent.enableSSHSupport '' | 
					
						
							|  |  |  |       # SSH agent protocol doesn't support changing TTYs, so bind the agent | 
					
						
							|  |  |  |       # to every new TTY. | 
					
						
							| 
									
										
										
										
											2019-03-17 09:49:50 +01:00
										 |  |  |       ${cfg.package}/bin/gpg-connect-agent --quiet updatestartuptty /bye > /dev/null | 
					
						
							| 
									
										
										
										
											2019-02-23 10:20:12 -08:00
										 |  |  |     '');
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 10:20:12 -08:00
										 |  |  |     environment.extraInit = mkIf cfg.agent.enableSSHSupport ''
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |       if [ -z "$SSH_AUTH_SOCK" ]; then | 
					
						
							| 
									
										
										
										
											2019-03-17 09:49:50 +01:00
										 |  |  |         export SSH_AUTH_SOCK=$(${cfg.package}/bin/gpgconf --list-dirs agent-ssh-socket) | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |       fi | 
					
						
							| 
									
										
										
										
											2019-02-23 10:20:12 -08:00
										 |  |  |     '';
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assertions = [ | 
					
						
							| 
									
										
										
										
											2017-07-11 01:24:13 +02:00
										 |  |  |       { assertion = cfg.agent.enableSSHSupport -> !config.programs.ssh.startAgent; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:14:39 +02:00
										 |  |  |         message = "You can't use ssh-agent and GnuPG agent with SSH support enabled at the same time!"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |