Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emacs: allow wrapped emacs to execute itself again #361145

Merged

Conversation

binarin
Copy link
Contributor

@binarin binarin commented Dec 2, 2024

fixes #145302 #237855

emacsWithPackages wrapper script/site-start.el sanitizes 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 compilation, 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).

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

Copy link
Contributor

@jian-lin jian-lin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great at a first glance. I'll do a real review later.

Please target the staging branch.

@jian-lin jian-lin self-requested a review December 2, 2024 16:47
@binarin binarin force-pushed the emacs-with-packages-invocation branch from 8a478a1 to b344d66 Compare December 2, 2024 16:53
@binarin binarin changed the base branch from master to staging December 2, 2024 16:54
@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: haskell 6.topic: qt/kde 8.has: documentation This PR adds or changes documentation 8.has: changelog 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: rust 6.topic: golang 6.topic: testing Tooling for automated testing of packages and modules 6.topic: java Including JDK, tooling, other languages, other VMs 6.topic: lib The Nixpkgs function library 6.topic: php 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 6.topic: dotnet Language: .NET 6.topic: flutter and removed 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: haskell 6.topic: qt/kde labels Dec 2, 2024
@github-actions github-actions bot removed 8.has: documentation This PR adds or changes documentation 8.has: changelog 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: rust 6.topic: golang 6.topic: testing Tooling for automated testing of packages and modules 6.topic: java Including JDK, tooling, other languages, other VMs 6.topic: lib The Nixpkgs function library 6.topic: php 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 6.topic: dotnet Language: .NET 6.topic: flutter labels Dec 2, 2024
@binarin binarin removed the 6.topic: dotnet Language: .NET label Dec 2, 2024
@ofborg ofborg bot added 2.status: merge conflict This PR has merge conflicts with the target branch and removed 2.status: merge conflict This PR has merge conflicts with the target branch labels Dec 3, 2024
Copy link
Contributor

@jian-lin jian-lin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small problem: I cannot reproduce the restart-emacs issue.

My steps are as follows:

  1. nix build --include 'nixpkgs=flake:github:nixos/nixpkgs?rev=55d15ad12a74eb7d4646254e13638ad0c4128776' --impure --expr 'with (import <nixpkgs> { config = { }; overlays = [ ]; }); emacs.pkgs.withPackages (ps: [ ps.magit ])'
  2. export PATH=$PWD/result/bin:$PATH
  3. emacs --init-directory=$(mktemp -d)
  4. M-x restart-emacs RET
  5. M-x magit RET

There is no error in step 5, which means the restarted Emacs can find its packages.

pkgs/applications/editors/emacs/site-start.el Outdated Show resolved Hide resolved
fixes NixOS#145302 NixOS#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 NixOS#106486, this behaviour
stays unchanged. The reasoning was to be able to run different emacs
executables without polluting their EMACSLOADPATH (as described here
NixOS@23d4bfb).
The only change is that invoking itself is again feasible (and that's
what emacs actually expects).

Co-authored-by: Lin Jian <[email protected]>
@jian-lin jian-lin force-pushed the emacs-with-packages-invocation branch from b344d66 to 5f9cbf9 Compare December 6, 2024 05:15
@jian-lin jian-lin changed the base branch from staging to staging-next December 6, 2024 05:15
@jian-lin
Copy link
Contributor

jian-lin commented Dec 6, 2024

I took the liberty to dealt with the suffix / of invocation-directory and remove restart-emacs from commit message. If it is OK with you, I will merge this PR in 24 hours.

In addition, I re-targeted this PR to staging-next so that it can reach users sooner.

@binarin
Copy link
Contributor Author

binarin commented Dec 6, 2024

Yes, you are right - restart-emacs was actually not affected, only (shell-command (file-name-concat invocation-directory invocation-name)) (it's what was actually tested in #145302). I've just incorrectly assumed that restart-emacs is also broken.

I've looked a bit into why restart-emacs was working. Turns out (setenv ...) doesn't actually change OS environment, it only mutates process-environment elisp var. But restart-emacs doesn't use it, it just calls execvp in the original process env which is still pristine since after the start.

@jian-lin jian-lin merged commit 44c4eb8 into NixOS:staging-next Dec 6, 2024
25 of 26 checks passed
@binarin binarin deleted the emacs-with-packages-invocation branch December 9, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

emacsWithPackages runs emacs with invocation-directory of nested package
2 participants