fix(lib): package!: add :env property

Allows the association of arbitrary envvars or variables with the build
artifacts of a package. If they change, the package is rebuilt on the
next 'doom sync'. This is a temporary measure, which is why this is not
touted as a new feature. It will be replaced in v3.
This commit is contained in:
Henrik Lissner
2025-09-22 22:30:30 -04:00
parent 1bc2af6ce5
commit 87a7efcea6
3 changed files with 41 additions and 3 deletions

View File

@@ -1601,7 +1601,7 @@ For more about modules and flags, see `doom!'."
;;; `doom-package' ;;; `doom-package'
(cl-defmacro package! (cl-defmacro package!
(name &rest plist &key built-in recipe ignore _type _pin _disable) (name &rest plist &key built-in recipe ignore _type _pin _disable _env)
"Declares a package and how to install it (if applicable). "Declares a package and how to install it (if applicable).
This macro is declarative and does not load nor install packages. It is used to This macro is declarative and does not load nor install packages. It is used to
@@ -1638,6 +1638,9 @@ Accepts the following properties:
inform help commands like `doom/help-packages' that this is a built-in inform help commands like `doom/help-packages' that this is a built-in
package. If set to 'prefer, the package will not be installed if it is package. If set to 'prefer, the package will not be installed if it is
already provided by Emacs. already provided by Emacs.
:env ALIST
Parameters and envvars to set while the package is building. If these values
change, the package will be rebuilt on next 'doom sync'.
Returns t if package is successfully registered, and nil if it was disabled Returns t if package is successfully registered, and nil if it was disabled
elsewhere." elsewhere."

View File

@@ -965,8 +965,14 @@ Must be run from a magit diff buffer."
(if-let* ((built (if-let* ((built
(doom-packages--with-recipes recipes (package local-repo recipe) (doom-packages--with-recipes recipes (package local-repo recipe)
(let ((repo-dir (straight--repos-dir (or local-repo package))) (let ((repo-dir (straight--repos-dir (or local-repo package)))
(build-dir (straight--build-dir package))) (build-dir (straight--build-dir package))
(build-file ".doompackage"))
(unless force-p (unless force-p
;; Ensure packages w/ a changed :env are rebuilt
(when-let* ((plist (alist-get (intern package) doom-packages)))
(unless (equal (plist-get plist :env)
(doom-file-read (doom-path build-dir build-file) :by 'read :noerror t))
(puthash package t straight--packages-to-rebuild)))
;; Ensure packages w/ outdated files/bytecode are rebuilt ;; Ensure packages w/ outdated files/bytecode are rebuilt
(let* ((build (if (plist-member recipe :build) (let* ((build (if (plist-member recipe :build)
(plist-get recipe :build) (plist-get recipe :build)
@@ -1005,7 +1011,25 @@ Must be run from a magit diff buffer."
(print! (item "%s: pinned to %s") package pin) (print! (item "%s: pinned to %s") package pin)
(when commit (when commit
(print! (item "%s: checked out %s") package commit))))) (print! (item "%s: checked out %s") package commit)))))
straight-vc-git-post-clone-hook))) straight-vc-git-post-clone-hook))
(straight-use-package-prepare-functions
(cons (lambda (package &rest _)
(when-let* ((plist (alist-get (intern package) doom-packages))
(env (plist-get plist :env)))
(cl-loop for (var . val) in env
if (and (symbolp var)
(string-prefix-p "_" (symbol-name var)))
do (set-default var val)
else if (and (stringp var) val)
do (setenv var val))))
straight-use-package-prepare-functions))
(straight-use-package-post-build-functions
(cons (lambda (package &rest _)
(when-let* ((plist (alist-get (intern package) doom-packages))
(env (plist-get plist :env)))
(with-temp-file (straight--build-file package build-file)
(prin1 env (current-buffer)))))
straight-use-package-post-build-functions)))
(straight-use-package (intern package))) (straight-use-package (intern package)))
(error (error
(signal 'doom-package-error (list package e)))))))) (signal 'doom-package-error (list package e))))))))

View File

@@ -61,6 +61,7 @@ Can be changed externally by setting $DOOMPROFILELOADFILE.")
;;; Profile storage variables ;;; Profile storage variables
(defvar doom-profile-generators (defvar doom-profile-generators
'(("05-vars.auto.el" doom-profile--generate-vars doom--startup-vars) '(("05-vars.auto.el" doom-profile--generate-vars doom--startup-vars)
("20-package-envs.auto.el" doom-profile--generate-package-envs)
("80-loaddefs.auto.el" doom-profile--generate-loaddefs-doom doom--startup-loaddefs-doom) ("80-loaddefs.auto.el" doom-profile--generate-loaddefs-doom doom--startup-loaddefs-doom)
("90-loaddefs-packages.auto.el" doom-profile--generate-loaddefs-packages doom--startup-loaddefs-packages) ("90-loaddefs-packages.auto.el" doom-profile--generate-loaddefs-packages doom--startup-loaddefs-packages)
("95-modules.auto.el" doom-profile--generate-load-modules doom--startup-modules)) ("95-modules.auto.el" doom-profile--generate-load-modules doom--startup-modules))
@@ -422,6 +423,16 @@ caches them in `doom--profiles'. If RELOAD? is non-nil, refresh the cache."
(mapcan #'doom-glob doom-autoloads-files)) (mapcan #'doom-glob doom-autoloads-files))
nil))))) nil)))))
(defun doom-profile--generate-package-envs ()
(cl-loop for (_ . plist) in doom-packages
if (plist-get plist :env)
append (cl-loop for (var . val) in it
if (and (stringp var) val)
collect `(setenv ,var ,val)
else if (and (symbolp var)
(string-prefix-p "_" (symbol-name var)))
collect `(setq-default ,var ,val))))
(defun doom-profile--generate-loaddefs-packages () (defun doom-profile--generate-loaddefs-packages ()
`((defun doom--startup-loaddefs-packages () `((defun doom--startup-loaddefs-packages ()
(let ((load-in-progress t)) (let ((load-in-progress t))