Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

emacs: allow wrapped emacs to execute itself again

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 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
https://github.com/NixOS/nixpkgs/pull/106486/commits/23d4bfb6661ca57a9e331a2cf4184232d38ac38b).
The only change is that invoking itself is again feasible (and that's
what emacs actually expects).

Co-authored-by: Lin Jian <me@linj.tech>

authored by Alexey Lebedeff Lin Jian and committed by Lin Jian 5f9cbf98 47b7878e

+17
+4
pkgs/applications/editors/emacs/build-support/wrapper.nix
··· 198 --subst-var-by bash ${emacs.stdenv.shell} \ 199 --subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \ 200 --subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \ 201 --subst-var prog 202 chmod +x $out/bin/$progname 203 # Create a “NOP” binary wrapper for the pure sake of it becoming a ··· 222 --subst-var-by bash ${emacs.stdenv.shell} \ 223 --subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \ 224 --subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \ 225 --subst-var-by prog "$emacs/Applications/Emacs.app/Contents/MacOS/Emacs" 226 chmod +x $out/Applications/Emacs.app/Contents/MacOS/Emacs 227 wrapProgramBinary $out/Applications/Emacs.app/Contents/MacOS/Emacs
··· 198 --subst-var-by bash ${emacs.stdenv.shell} \ 199 --subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \ 200 --subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \ 201 + --subst-var-by wrapperInvocationDirectory "$out/bin/" \ 202 + --subst-var-by wrapperInvocationName "$progname" \ 203 --subst-var prog 204 chmod +x $out/bin/$progname 205 # Create a “NOP” binary wrapper for the pure sake of it becoming a ··· 224 --subst-var-by bash ${emacs.stdenv.shell} \ 225 --subst-var-by wrapperSiteLisp "$deps/share/emacs/site-lisp" \ 226 --subst-var-by wrapperSiteLispNative "$deps/share/emacs/native-lisp" \ 227 + --subst-var-by wrapperInvocationDirectory "$out/Applications/Emacs.app/Contents/MacOS/" \ 228 + --subst-var-by wrapperInvocationName "Emacs" \ 229 --subst-var-by prog "$emacs/Applications/Emacs.app/Contents/MacOS/Emacs" 230 chmod +x $out/Applications/Emacs.app/Contents/MacOS/Emacs 231 wrapProgramBinary $out/Applications/Emacs.app/Contents/MacOS/Emacs
+3
pkgs/applications/editors/emacs/build-support/wrapper.sh
··· 50 export EMACSNATIVELOADPATH="${newNativeLoadPath[*]}" 51 export emacsWithPackages_siteLispNative=@wrapperSiteLispNative@ 52 53 exec @prog@ "$@"
··· 50 export EMACSNATIVELOADPATH="${newNativeLoadPath[*]}" 51 export emacsWithPackages_siteLispNative=@wrapperSiteLispNative@ 52 53 + export emacsWithPackages_invocationDirectory=@wrapperInvocationDirectory@ 54 + export emacsWithPackages_invocationName=@wrapperInvocationName@ 55 + 56 exec @prog@ "$@"
+10
pkgs/applications/editors/emacs/site-start.el
··· 39 (setenv "EMACSNATIVELOADPATH" (when new-env-list 40 (mapconcat 'identity new-env-list ":")))))) 41 42 ;;; Set up native-comp load path. 43 (when (featurep 'native-compile) 44 ;; Append native-comp subdirectories from `NIX_PROFILES'.
··· 39 (setenv "EMACSNATIVELOADPATH" (when new-env-list 40 (mapconcat 'identity new-env-list ":")))))) 41 42 + (let ((wrapper-invocation-directory (getenv "emacsWithPackages_invocationDirectory"))) 43 + (when wrapper-invocation-directory 44 + (setq invocation-directory (file-name-as-directory wrapper-invocation-directory)) 45 + (setenv "emacsWithPackages_invocationDirectory" nil))) 46 + 47 + (let ((wrapper-invocation-name (getenv "emacsWithPackages_invocationName"))) 48 + (when wrapper-invocation-name 49 + (setq invocation-name wrapper-invocation-name) 50 + (setenv "emacsWithPackages_invocationName" nil))) 51 + 52 ;;; Set up native-comp load path. 53 (when (featurep 'native-compile) 54 ;; Append native-comp subdirectories from `NIX_PROFILES'.