refactor: move GPG defaults to :config default

Assuming GPG is present and set up by default can be surprising for
beginners, so this commit makes GPG integration opt-in, behind a new
+gnupg flag in the :config default module. There'll be more added to
this later.
This commit is contained in:
Henrik Lissner
2024-12-03 18:01:46 -05:00
parent ea616ebd5b
commit c788769469
3 changed files with 33 additions and 29 deletions

View File

@ -20,6 +20,9 @@ This module provides a set of reasonable defaults, including:
** Module flags
- +bindings :: ...
- +gnupg ::
Enable GnuPG integration and defaults, allowing Emacs to pick up on your
default GPG keys, including interop with pinentry-emacs.
- +smartparens :: ...
** Packages

View File

@ -31,29 +31,36 @@
avy-single-candidate-jump nil)
(after! epa
;; With GPG 2.1+, this forces gpg-agent to use the Emacs minibuffer to prompt
;; for the key passphrase.
(set 'epg-pinentry-mode 'loopback)
;; Default to the first enabled and non-expired key in your keyring.
(setq-default
epa-file-encrypt-to
(or (default-value 'epa-file-encrypt-to)
(unless (string-empty-p user-full-name)
(when-let (context (ignore-errors (epg-make-context)))
(cl-loop for key in (epg-list-keys context user-full-name 'public)
for subkey = (car (epg-key-sub-key-list key))
if (not (memq 'disabled (epg-sub-key-capability subkey)))
if (< (or (epg-sub-key-expiration-time subkey) 0)
(time-to-seconds))
collect (epg-sub-key-fingerprint subkey))))
user-mail-address))
;; And suppress prompts if epa-file-encrypt-to has a default value (without
;; overwriting file-local values).
(defadvice! +default--dont-prompt-for-keys-a (&rest _)
:before #'epa-file-write-region
(unless (local-variable-p 'epa-file-encrypt-to)
(setq-local epa-file-encrypt-to (default-value 'epa-file-encrypt-to)))))
(when (modulep! +gnupg)
;; By default, Emacs stores `authinfo' in $HOME and in plain-text. Let's not
;; do that, mkay? This file stores usernames, passwords, and other treasures
;; for the aspiring malicious third party. You'll need a GPG setup though.
(setq auth-sources (list (file-name-concat doom-profile-state-dir "authinfo.gpg")
"~/.authinfo.gpg"))
(after! epa
;; With GPG 2.1+, this forces gpg-agent to use the Emacs minibuffer to
;; prompt for the key passphrase.
(set 'epg-pinentry-mode 'loopback)
;; Default to the first enabled and non-expired key in your keyring.
(setq-default
epa-file-encrypt-to
(or (default-value 'epa-file-encrypt-to)
(unless (string-empty-p user-full-name)
(when-let (context (ignore-errors (epg-make-context)))
(cl-loop for key in (epg-list-keys context user-full-name 'public)
for subkey = (car (epg-key-sub-key-list key))
if (not (memq 'disabled (epg-sub-key-capability subkey)))
if (< (or (epg-sub-key-expiration-time subkey) 0)
(time-to-seconds))
collect (epg-sub-key-fingerprint subkey))))
user-mail-address))
;; And suppress prompts if epa-file-encrypt-to has a default value (without
;; overwriting file-local values).
(defadvice! +default--dont-prompt-for-keys-a (&rest _)
:before #'epa-file-write-region
(unless (local-variable-p 'epa-file-encrypt-to)
(setq-local epa-file-encrypt-to (default-value 'epa-file-encrypt-to))))))
(after! woman