fork/home-manager
git clone https://git.julia.blue/fork/home-manager
8d243f7d
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6cfg = config . programs . gpg ; 7 8mkKeyValue = key :value : 9if isString value then " ${ key } ${ value } " else optionalString value key ; 10 11cfgText = generators . toKeyValue { 12inherit mkKeyValue ; 13listsAsDuplicateKeys = true ; 14} cfg . settings ; 15 16scdaemonCfgText = generators . toKeyValue { 17inherit mkKeyValue ; 18listsAsDuplicateKeys = true ; 19} cfg . scdaemonSettings ; 20 21primitiveType = types . oneOf [ types . str types . bool ]; 22 23publicKeyOpts = { config , ...} :{ 24options = { 25text = mkOption { 26type = types . nullOr types . str ; 27default = null ; 28description = '' 29Text of an OpenPGP public key. 30'' ; 31}; 32 33source = mkOption { 34type = types . path ; 35description = '' 36Path of an OpenPGP public key file. 37'' ; 38}; 39 40trust = mkOption { 41type = types . nullOr ( types . enum [ 42"unknown" 431 44"never" 452 46"marginal" 473 48"full" 494 50"ultimate" 515 52]); 53default = null ; 54apply = v : 55if isString v then 56{ 57unknown = 1 ; 58never = 2 ; 59marginal = 3 ; 60full = 4 ; 61ultimate = 5 ; 62}.${ v } 63else 64v ; 65description = '' 66The amount of trust you have in the key ownership and the care the 67owner puts into signing other keys. The available levels are 68 69`unknown` or `1` 70: I don't know or won't say. 71 72`never` or `2` 73: I do **not** trust. 74 75`marginal` or `3` 76: I trust marginally. 77 78`full` or `4` 79: I trust fully. 80 81`ultimate` or `5` 82: I trust ultimately. 83 84See the [Key Management chapter](https://www.gnupg.org/gph/en/manual/x334.html) 85of the GNU Privacy Handbook for more. 86'' ; 87}; 88}; 89 90config = { 91source = 92mkIf ( config . text != null ) ( pkgs . writeText "gpg-pubkey" config . text ); 93}; 94}; 95 96importTrustBashFunctions = let gpg = " ${ cfg . package } /bin/gpg" ; 97in '' 98function gpgKeyId() { 99${ gpg } --show-key --with-colons "$1" \ 100| grep ^pub: \ 101| cut -d: -f5 102} 103 104function importTrust() { 105local keyIds trust 106IFS='\n' read -ra keyIds <<< "$(gpgKeyId "$1")" 107trust="$2" 108for id in "''${keyIds[@]}" ; do 109{ echo trust; echo "$trust"; (( trust == 5 )) && echo y; echo quit; } \ 110| ${ gpg } --no-tty --command-fd 0 --edit-key "$id" 111done 112} 113 114'' ; 115 116keyringFiles = let 117gpg = " ${ cfg . package } /bin/gpg" ; 118 119importKey = { source , trust , ...} :'' 120${ gpg } --import ${ source } 121${ optionalString ( trust != null ) 122''importTrust " ${ source } " ${ toString trust } '' } 123'' ; 124 125importKeys = concatMapStringsSep "\n" importKey cfg . publicKeys ; 126in pkgs . runCommand "gpg-pubring" { buildInputs = [ cfg . package ]; } '' 127export GNUPGHOME 128GNUPGHOME = $( mktemp -d ) 129 130${ importTrustBashFunctions } 131${ importKeys } 132 133mkdir $ out 134cp $ GNUPGHOME /pubring.kbx $ out /pubring.kbx 135if [[ -e $ GNUPGHOME /trustdb.gpg ]] ; then 136cp $ GNUPGHOME /trustdb.gpg $ out /trustdb.gpg 137fi 138'' ; 139 140in { 141options . programs . gpg = { 142enable = mkEnableOption "GnuPG" ; 143 144package = mkOption { 145type = types . package ; 146default = pkgs . gnupg ; 147defaultText = literalExpression "pkgs.gnupg" ; 148example = literalExpression "pkgs.gnupg23" ; 149description = 150"The Gnupg package to use (also used by the gpg-agent service)." ; 151}; 152 153settings = mkOption { 154type = 155types . attrsOf ( types . either primitiveType ( types . listOf types . str )); 156example = literalExpression '' 157{ 158no-comments = false; 159s2k-cipher-algo = "AES128"; 160} 161'' ; 162description = '' 163GnuPG configuration options. Available options are described 164in 165[ 166{manpage}`gpg(1)` 167](https://gnupg.org/documentation/manpage.html). 168 169Note that lists are converted to duplicate keys. 170'' ; 171}; 172 173scdaemonSettings = mkOption { 174type = 175types . attrsOf ( types . either primitiveType ( types . listOf types . str )); 176example = literalExpression '' 177{ 178disable-ccid = true; 179} 180'' ; 181description = '' 182SCdaemon configuration options. Available options are described 183in 184[ 185{manpage}`scdaemon(1)` 186](https://www.gnupg.org/documentation/manuals/gnupg/Scdaemon-Options.html). 187'' ; 188}; 189 190homedir = mkOption { 191type = types . path ; 192example = literalExpression ''"''${config.xdg.dataHome}/gnupg"'' ; 193default = " ${ config . home . homeDirectory } /.gnupg" ; 194defaultText = 195literalExpression ''"''${config.home.homeDirectory}/.gnupg"'' ; 196description = "Directory to store keychains and configuration." ; 197}; 198 199mutableKeys = mkOption { 200type = types . bool ; 201default = true ; 202description = '' 203If set to `true`, you may manage your keyring as a user 204using the `gpg` command. Upon activation, the keyring 205will have managed keys added without overwriting unmanaged keys. 206 207If set to `false`, the path 208{file}`$GNUPGHOME/pubring.kbx` will become an immutable 209link to the Nix store, denying modifications. 210'' ; 211}; 212 213mutableTrust = mkOption { 214type = types . bool ; 215default = true ; 216description = '' 217If set to `true`, you may manage trust as a user using 218the {command}`gpg` command. Upon activation, trusted keys have 219their trust set without overwriting unmanaged keys. 220 221If set to `false`, the path 222{file}`$GNUPGHOME/trustdb.gpg` will be 223*overwritten* on each activation, removing trust for 224any unmanaged keys. Be careful to make a backup of your old 225{file}`trustdb.gpg` before switching to immutable trust! 226'' ; 227}; 228 229publicKeys = mkOption { 230type = types . listOf ( types . submodule publicKeyOpts ); 231example = literalExpression '' 232[ { source = ./pubkeys.txt; } ] 233'' ; 234default = [ ]; 235description = '' 236A list of public keys to be imported into GnuPG. Note, these key files 237will be copied into the world-readable Nix store. 238'' ; 239}; 240}; 241 242config = mkIf cfg . enable { 243programs . gpg . settings = { 244personal-cipher-preferences = mkDefault "AES256 AES192 AES" ; 245personal-digest-preferences = mkDefault "SHA512 SHA384 SHA256" ; 246personal-compress-preferences = mkDefault "ZLIB BZIP2 ZIP Uncompressed" ; 247default-preference-list = mkDefault 248"SHA512 SHA384 SHA256 AES256 AES192 AES ZLIB BZIP2 ZIP Uncompressed" ; 249cert-digest-algo = mkDefault "SHA512" ; 250s2k-digest-algo = mkDefault "SHA512" ; 251s2k-cipher-algo = mkDefault "AES256" ; 252charset = mkDefault "utf-8" ; 253fixed-list-mode = mkDefault true ; 254no-comments = mkDefault true ; 255no-emit-version = mkDefault true ; 256keyid-format = mkDefault "0xlong" ; 257list-options = mkDefault "show-uid-validity" ; 258verify-options = mkDefault "show-uid-validity" ; 259with-fingerprint = mkDefault true ; 260require-cross-certification = mkDefault true ; 261no-symkey-cache = mkDefault true ; 262use-agent = mkDefault true ; 263}; 264 265programs . gpg . scdaemonSettings = { 266# no defaults for scdaemon 267}; 268 269home . packages = [ cfg . package ]; 270home . sessionVariables = { GNUPGHOME = cfg . homedir ; }; 271 272home . file . " ${ cfg . homedir } /gpg.conf" . text = cfgText ; 273 274home . file . " ${ cfg . homedir } /scdaemon.conf" . text = scdaemonCfgText ; 275 276# Link keyring if keys are not mutable 277home . file . " ${ cfg . homedir } /pubring.kbx" = 278mkIf ( ! cfg . mutableKeys && cfg . publicKeys != [ ]) { 279source = " ${ keyringFiles } /pubring.kbx" ; 280}; 281 282home . activation = { 283createGpgHomedir = 284hm . dag . entryBetween [ "linkGeneration" ] [ "writeBoundary" ] '' 285$DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${ escapeShellArg cfg . homedir } 286'' ; 287 288importGpgKeys = let 289gpg = " ${ cfg . package } /bin/gpg" ; 290 291importKey = { source , trust , ...} : 292# Import mutable keys 293optional cfg . mutableKeys 294"$DRY_RUN_CMD ${ gpg } $QUIET_ARG --import ${ source } " 295 296# Import mutable trust 297++ optional ( trust != null && cfg . mutableTrust ) 298''$DRY_RUN_CMD importTrust " ${ source } " ${ toString trust } '' ; 299 300anyTrust = any ( k :k . trust != null ) cfg . publicKeys ; 301 302importKeys = concatStringsSep "\n" ( concatMap importKey cfg . publicKeys ); 303 304# If any key/trust should be imported then create the block. Otherwise 305# leave it empty. 306block = concatStringsSep "\n" ( optional ( importKeys != "" ) '' 307export GNUPGHOME= ${ escapeShellArg cfg . homedir } 308if [[ ! -v VERBOSE ]]; then 309QUIET_ARG="--quiet" 310else 311QUIET_ARG="" 312fi 313${ importTrustBashFunctions } 314${ importKeys } 315unset GNUPGHOME QUIET_ARG keyId importTrust 316'' ++ optional ( ! cfg . mutableTrust && anyTrust ) '' 317install -m 0700 ${ keyringFiles } /trustdb.gpg " ${ cfg . homedir } /trustdb.gpg"'' ); 318in mkIf ( cfg . publicKeys != [ ]) 319( lib . hm . dag . entryAfter [ "linkGeneration" ] block ); 320}; 321}; 322}