Skip to content

Commit

Permalink
emacs: allow wrapped emacs to execute itself again
Browse files Browse the repository at this point in the history
fixes #145302 #237855

emacsWithPackages wrapper script/`site-start.el` sanitize
EMACSLOADPATH, to make nested emacs invocations independent of the
package set specified in emacsWithPackages.

But there are valid use cases when one needs to call nested emacs with
the same package set. This includes built-in emacs functionality such
as `restart-emacs` and async native compilations, and also external
packages like `emacs-async` and `esup`. In all of these cases
`invocation-directory`/`invocation-name` variables are being used to
launch nested emacs.

With this patch these variables will be populated to point to the
emacsWithPackages wrapper executable, so that executing
`(file-name-concat invocation-directory invocation-name)` will give a
fully functional emacs again.

`EMACSLOADPATH` sanitization was introduced by #106486, this behaviour
stays unchanged. The reasoning was to be able to run different emacs
executables without polluting their EMACSLOADPATH (as described here
23d4bfb).
The only change is that invoking itself is again feasible (and that's
what emacs actually expects).
  • Loading branch information
binarin committed Dec 2, 2024
1 parent b598495 commit 8a478a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkgs/applications/editors/emacs/build-support/wrapper.nix
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ runCommand
--subst-var-by bash ${emacs.stdenv.shell} \
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
--subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \
--subst-var-by wrapperInvocationDirectory "$out/bin" \
--subst-var-by wrapperInvocationName "$progname" \
--subst-var prog
chmod +x $out/bin/$progname
# Create a “NOP” binary wrapper for the pure sake of it becoming a
Expand All @@ -222,6 +224,8 @@ runCommand
--subst-var-by bash ${emacs.stdenv.shell} \
--subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \
--subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \
--subst-var-by wrapperInvocationDirectory "$out/Applications/Emacs.app/Contents/MacOS" \
--subst-var-by wrapperInvocationName "Emacs" \
--subst-var-by prog "$emacs/Applications/Emacs.app/Contents/MacOS/Emacs"
chmod +x $out/Applications/Emacs.app/Contents/MacOS/Emacs
wrapProgramBinary $out/Applications/Emacs.app/Contents/MacOS/Emacs
Expand Down
3 changes: 3 additions & 0 deletions pkgs/applications/editors/emacs/build-support/wrapper.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ export emacsWithPackages_siteLisp=@wrapperSiteLisp@
export EMACSNATIVELOADPATH="${newNativeLoadPath[*]}"
export emacsWithPackages_siteLispNative=@wrapperSiteLispNative@

export emacsWithPackages_invocationDirectory=@wrapperInvocationDirectory@
export emacsWithPackages_invocationName=@wrapperInvocationName@

exec @prog@ "$@"
10 changes: 10 additions & 0 deletions pkgs/applications/editors/emacs/site-start.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ least specific (the system profile)"
(setenv "EMACSNATIVELOADPATH" (when new-env-list
(mapconcat 'identity new-env-list ":"))))))

(let ((wrapper-invocation-directory (getenv "emacsWithPackages_invocationDirectory")))
(when wrapper-invocation-directory
(setq invocation-directory wrapper-invocation-directory)
(setenv "emacsWithPackages_invocationDirectory" nil)))

(let ((wrapper-invocation-name (getenv "emacsWithPackages_invocationName")))
(when wrapper-invocation-name
(setq invocation-name wrapper-invocation-name)
(setenv "emacsWithPackages_invocationName" nil)))

;;; Set up native-comp load path.
(when (featurep 'native-compile)
;; Append native-comp subdirectories from `NIX_PROFILES'.
Expand Down

0 comments on commit 8a478a1

Please sign in to comment.