fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , options , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . gpg-agent ; 8gpgPkg = config . programs . gpg . package ; 9 10homedir = config . programs . gpg . homedir ; 11 12gpgSshSupportStr = '' 13${ gpgPkg } /bin/gpg-connect-agent updatestartuptty /bye > /dev/null 14'' ; 15 16gpgInitStr = '' 17GPG_TTY="$(tty)" 18export GPG_TTY 19'' + optionalString cfg . enableSshSupport gpgSshSupportStr ; 20 21gpgFishInitStr = '' 22set -gx GPG_TTY (tty) 23'' + optionalString cfg . enableSshSupport gpgSshSupportStr ; 24 25# mimic `gpgconf` output for use in `systemd` unit definitions. 26# we cannot use `gpgconf` directly because it heavily depends on system 27# state, but we need the values at build time. original: 28# https://github.com/gpg/gnupg/blob/c6702d77d936b3e9d91b34d8fdee9599ab94ee1b/common/homedir.c#L672-L681 29gpgconf = dir : 30let 31hash = 32substring 0 24 ( hexStringToBase32 ( builtins . hashString "sha1" homedir )); 33in if homedir == options . programs . gpg . homedir . default then 34"%t/gnupg/ ${ dir } " 35else 36"%t/gnupg/d. ${ hash } / ${ dir } " ; 37 38# Act like `xxd -r -p | base32` but with z-base-32 alphabet and no trailing padding. 39# Written in Nix for purity. 40hexStringToBase32 = let 41mod = a :b :a - a / b * b ; 42pow2 = elemAt [ 1 2 4 8 16 32 64 128 256 ]; 43splitChars = s :init ( tail ( splitString "" s )); 44 45base32Alphabet = splitChars "ybndrfg8ejkmcpqxot1uwisza345h769" ; 46hexToIntTable = listToAttrs ( genList ( x :{ 47name = toLower ( toHexString x ); 48value = x ; 49}) 16 ); 50 51initState = { 52ret = "" ; 53buf = 0 ; 54bufBits = 0 ; 55}; 56go = { ret , buf , bufBits } : 57hex : 58let 59buf' = buf * pow2 4 + hexToIntTable .${ hex }; 60bufBits' = bufBits + 4 ; 61extraBits = bufBits' - 5 ; 62in if bufBits >= 5 then { 63ret = ret + elemAt base32Alphabet ( buf' / pow2 extraBits ); 64buf = mod buf' ( pow2 extraBits ); 65bufBits = bufBits' - 5 ; 66} else { 67ret = ret ; 68buf = buf' ; 69bufBits = bufBits' ; 70}; 71in hexString :( foldl' go initState ( splitChars hexString )). ret ; 72 73in { 74meta . maintainers = [ maintainers . rycee ]; 75 76options = { 77services . gpg-agent = { 78enable = mkEnableOption "GnuPG private key agent" ; 79 80defaultCacheTtl = mkOption { 81type = types . nullOr types . int ; 82default = null ; 83description = '' 84Set the time a cache entry is valid to the given number of 85seconds. 86'' ; 87}; 88 89defaultCacheTtlSsh = mkOption { 90type = types . nullOr types . int ; 91default = null ; 92description = '' 93Set the time a cache entry used for SSH keys is valid to the 94given number of seconds. 95'' ; 96}; 97 98maxCacheTtl = mkOption { 99type = types . nullOr types . int ; 100default = null ; 101description = '' 102Set the maximum time a cache entry is valid to n seconds. After this 103time a cache entry will be expired even if it has been accessed 104recently or has been set using gpg-preset-passphrase. The default is 1052 hours (7200 seconds). 106'' ; 107}; 108 109maxCacheTtlSsh = mkOption { 110type = types . nullOr types . int ; 111default = null ; 112description = '' 113Set the maximum time a cache entry used for SSH keys is valid to n 114seconds. After this time a cache entry will be expired even if it has 115been accessed recently or has been set using gpg-preset-passphrase. 116The default is 2 hours (7200 seconds). 117'' ; 118}; 119 120enableSshSupport = mkOption { 121type = types . bool ; 122default = false ; 123description = '' 124Whether to use the GnuPG key agent for SSH keys. 125'' ; 126}; 127 128sshKeys = mkOption { 129type = types . nullOr ( types . listOf types . str ); 130default = null ; 131description = '' 132Which GPG keys (by keygrip) to expose as SSH keys. 133'' ; 134}; 135 136enableExtraSocket = mkOption { 137type = types . bool ; 138default = false ; 139description = '' 140Whether to enable extra socket of the GnuPG key agent (useful for GPG 141Agent forwarding). 142'' ; 143}; 144 145verbose = mkOption { 146type = types . bool ; 147default = false ; 148description = '' 149Whether to produce verbose output. 150'' ; 151}; 152 153grabKeyboardAndMouse = mkOption { 154type = types . bool ; 155default = true ; 156description = '' 157Tell the pinentry to grab the keyboard and mouse. This 158option should in general be used to avoid X-sniffing 159attacks. When disabled, this option passes 160{option}`no-grab` setting to gpg-agent. 161'' ; 162}; 163 164enableScDaemon = mkOption { 165type = types . bool ; 166default = true ; 167description = '' 168Make use of the scdaemon tool. This option has the effect of 169enabling the ability to do smartcard operations. When 170disabled, this option passes 171{option}`disable-scdaemon` setting to gpg-agent. 172'' ; 173}; 174 175extraConfig = mkOption { 176type = types . lines ; 177default = "" ; 178example = '' 179allow-emacs-pinentry 180allow-loopback-pinentry 181'' ; 182description = '' 183Extra configuration lines to append to the gpg-agent 184configuration file. 185'' ; 186}; 187 188pinentryFlavor = mkOption { 189type = types . nullOr ( types . enum pkgs . pinentry . flavors ); 190example = "gnome3" ; 191default = "gtk2" ; 192description = '' 193Which pinentry interface to use. If not 194`null`, it sets 195{option}`pinentry-program` in 196{file}`gpg-agent.conf`. Beware that 197`pinentry-gnome3` may not work on non-Gnome 198systems. You can fix it by adding the following to your 199system configuration: 200```nix 201services.dbus.packages = [ pkgs.gcr ]; 202``` 203For this reason, the default is `gtk2` for 204now. 205'' ; 206}; 207 208enableBashIntegration = mkEnableOption "Bash integration" // { 209default = true ; 210}; 211 212enableZshIntegration = mkEnableOption "Zsh integration" // { 213default = true ; 214}; 215 216enableFishIntegration = mkEnableOption "Fish integration" // { 217default = true ; 218}; 219}; 220}; 221 222config = mkIf cfg . enable ( mkMerge [ 223{ 224home . file . " ${ homedir } /gpg-agent.conf" . text = concatStringsSep "\n" 225( optional ( cfg . enableSshSupport ) "enable-ssh-support" 226++ optional cfg . grabKeyboardAndMouse "grab" 227++ optional ( ! cfg . enableScDaemon ) "disable-scdaemon" 228++ optional ( cfg . defaultCacheTtl != null ) 229"default-cache-ttl ${ toString cfg . defaultCacheTtl } " 230++ optional ( cfg . defaultCacheTtlSsh != null ) 231"default-cache-ttl-ssh ${ toString cfg . defaultCacheTtlSsh } " 232++ optional ( cfg . maxCacheTtl != null ) 233"max-cache-ttl ${ toString cfg . maxCacheTtl } " 234++ optional ( cfg . maxCacheTtlSsh != null ) 235"max-cache-ttl-ssh ${ toString cfg . maxCacheTtlSsh } " 236++ optional ( cfg . pinentryFlavor != null ) 237"pinentry-program ${ pkgs . pinentry .${ cfg . pinentryFlavor }} /bin/pinentry" 238++ [ cfg . extraConfig ]); 239 240home . sessionVariablesExtra = optionalString cfg . enableSshSupport '' 241if [[ -z "$SSH_AUTH_SOCK" ]]; then 242export SSH_AUTH_SOCK="$( ${ gpgPkg } /bin/gpgconf --list-dirs agent-ssh-socket)" 243fi 244'' ; 245 246programs . bash . initExtra = mkIf cfg . enableBashIntegration gpgInitStr ; 247programs . zsh . initExtra = mkIf cfg . enableZshIntegration gpgInitStr ; 248programs . fish . interactiveShellInit = 249mkIf cfg . enableFishIntegration gpgFishInitStr ; 250} 251 252( mkIf ( cfg . sshKeys != null ) { 253# Trailing newlines are important 254home . file . " ${ homedir } /sshcontrol" . text = concatMapStrings ( s :'' 255${ s } 256'' ) cfg . sshKeys ; 257}) 258 259# The systemd units below are direct translations of the 260# descriptions in the 261# 262# ${gpgPkg}/share/doc/gnupg/examples/systemd-user 263# 264# directory. 265{ 266assertions = [ 267( hm . assertions . assertPlatform "services.gpg-agent" pkgs platforms . linux ) 268]; 269 270systemd . user . services . gpg-agent = { 271Unit = { 272Description = "GnuPG cryptographic agent and passphrase cache" ; 273Documentation = "man:gpg-agent(1)" ; 274Requires = "gpg-agent.socket" ; 275After = "gpg-agent.socket" ; 276# This is a socket-activated service: 277RefuseManualStart = true ; 278}; 279 280Service = { 281ExecStart = " ${ gpgPkg } /bin/gpg-agent --supervised" 282+ optionalString cfg . verbose " --verbose" ; 283ExecReload = " ${ gpgPkg } /bin/gpgconf --reload gpg-agent" ; 284Environment = [ "GNUPGHOME= ${ homedir } " ]; 285}; 286}; 287 288systemd . user . sockets . gpg-agent = { 289Unit = { 290Description = "GnuPG cryptographic agent and passphrase cache" ; 291Documentation = "man:gpg-agent(1)" ; 292}; 293 294Socket = { 295ListenStream = gpgconf "S.gpg-agent" ; 296FileDescriptorName = "std" ; 297SocketMode = "0600" ; 298DirectoryMode = "0700" ; 299}; 300 301Install = { WantedBy = [ "sockets.target" ]; }; 302}; 303} 304 305( mkIf cfg . enableSshSupport { 306systemd . user . sockets . gpg-agent-ssh = { 307Unit = { 308Description = "GnuPG cryptographic agent (ssh-agent emulation)" ; 309Documentation = 310"man:gpg-agent(1) man:ssh-add(1) man:ssh-agent(1) man:ssh(1)" ; 311}; 312 313Socket = { 314ListenStream = gpgconf "S.gpg-agent.ssh" ; 315FileDescriptorName = "ssh" ; 316Service = "gpg-agent.service" ; 317SocketMode = "0600" ; 318DirectoryMode = "0700" ; 319}; 320 321Install = { WantedBy = [ "sockets.target" ]; }; 322}; 323}) 324 325( mkIf cfg . enableExtraSocket { 326systemd . user . sockets . gpg-agent-extra = { 327Unit = { 328Description = 329"GnuPG cryptographic agent and passphrase cache (restricted)" ; 330Documentation = "man:gpg-agent(1) man:ssh(1)" ; 331}; 332 333Socket = { 334ListenStream = gpgconf "S.gpg-agent.extra" ; 335FileDescriptorName = "extra" ; 336Service = "gpg-agent.service" ; 337SocketMode = "0600" ; 338DirectoryMode = "0700" ; 339}; 340 341Install = { WantedBy = [ "sockets.target" ]; }; 342}; 343}) 344]); 345}