From 5f9cbf9898b86c8151dde657c2023f3afdb714bf Mon Sep 17 00:00:00 2001 From: Alexey Lebedeff Date: Mon, 2 Dec 2024 17:04:15 +0100 Subject: [PATCH] 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 --- .../editors/emacs/build-support/wrapper.nix | 4 ++++ .../editors/emacs/build-support/wrapper.sh | 3 +++ pkgs/applications/editors/emacs/site-start.el | 10 ++++++++++ 3 files changed, 17 insertions(+) mode change 100644 => 100755 pkgs/applications/editors/emacs/build-support/wrapper.sh diff --git a/pkgs/applications/editors/emacs/build-support/wrapper.nix b/pkgs/applications/editors/emacs/build-support/wrapper.nix index 5b325f5e0a9e..81329808716b 100644 --- a/pkgs/applications/editors/emacs/build-support/wrapper.nix +++ b/pkgs/applications/editors/emacs/build-support/wrapper.nix @@ -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 @@ -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 diff --git a/pkgs/applications/editors/emacs/build-support/wrapper.sh b/pkgs/applications/editors/emacs/build-support/wrapper.sh old mode 100644 new mode 100755 index 44762bd4582b..e83308bf5b32 --- a/pkgs/applications/editors/emacs/build-support/wrapper.sh +++ b/pkgs/applications/editors/emacs/build-support/wrapper.sh @@ -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@ "$@" diff --git a/pkgs/applications/editors/emacs/site-start.el b/pkgs/applications/editors/emacs/site-start.el index c844e703e385..8c7ea3dfc913 100644 --- a/pkgs/applications/editors/emacs/site-start.el +++ b/pkgs/applications/editors/emacs/site-start.el @@ -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 (file-name-as-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'.