nix-mode icon indicating copy to clipboard operation
nix-mode copied to clipboard

Provide an eshell-aware `nix` command

Open Patryk27 opened this issue 10 months ago • 11 comments

This commit introduces an eshell-aware nix command that allows to run nix develop and nix shell that extend the current eshell session instead of spawning an external process.

Closes #164.

Patryk27 avatar Feb 10 '25 12:02 Patryk27

Sweet, definitely trying this out.

nvimtor avatar Feb 11 '25 00:02 nvimtor

lovely, just what I needed. Will definitely try it out later today.

update: it works fine for me. I did not notice any obvious issues. I tried it on a couple of repositories, both medium sized (~500k loc each) and nix develop, nix shell both worked as expected.

I love it!

ayys avatar Feb 11 '25 09:02 ayys

Hi! I have an issue where the first nix shell command works, but every subsequent nix shell (or develop) command doesn't.

I am not really good at Elisp but I added a few debugs to try and figure out what is going on:

https://gist.github.com/nvimtor/2c9ef0a8fabd669385589956bb9ef2db

then I got these logs:

DEBUG: eshell/nix called with args: ("shell" "nixpkgs#just")
DEBUG: Running nix-command-eshell with args: ("shell" "nixpkgs#just")
DEBUG: Entering nix-command-eshell with args: ("shell" "nixpkgs#just")
DEBUG: Spawning nix process with args: ("shell" "nixpkgs#just")
DEBUG: Temporary file created: /var/folders/9b/xxz26xk12pj432cnfkvmh6_h0000gn/T/nixOeYWs3
DEBUG: Executing command: nix shell nixpkgs#just --command sh -c export > /var/folders/9b/xxz26xk12pj432cnfkvmh6_h0000gn/T/nixOeYWs3
DEBUG: Process sentinel triggered for process: nix with status: finished

DEBUG: Process nix has command: nil
DEBUG: Process nix is no longer live. Deleting temporary file: /var/folders/9b/xxz26xk12pj432cnfkvmh6_h0000gn/T/nixOeYWs3

it seems the cmd I get from (cmd (car (eshell-commands-for-process proc)) is nil and that causes the file not to be parsed.

Removing the check for cmd here

(when (and cmd (buffer-live-p buffer) (eq 0 (process-exit-status proc)))

makes it work flawlessly.

nvimtor avatar Feb 11 '25 22:02 nvimtor

but every subsequent nix shell (or develop) command doesn't.

Ah, you mean like running nix shell twice in a row?

Patryk27 avatar Feb 12 '25 08:02 Patryk27

@Patryk27

not necessarily; if I exit the first shell and then try to spawn a new one, the same happens. I also realized that some eshell buffers work consistently, some don't work at all! But the "issue" is always the same: cmd being nil.

Curious, are you using direnv as well?

nvimtor avatar Feb 12 '25 12:02 nvimtor

I am, but I think I didn't actually test it in this case - I'll take a look, thanks for reporting 🙂

Patryk27 avatar Feb 12 '25 13:02 Patryk27

Having this + eat will be great. It's the main thing that prevents me from using eshell, because we rely on Nix at my job to build projects, and nix develop is what I use to start an interactive session for a particular repo.

knazarov avatar Feb 14 '25 16:02 knazarov

@knazarov sure you know this but envrc-mode + use flake works amazingly well

nvimtor avatar Feb 14 '25 18:02 nvimtor

Status: I think I've found the culprit, should have the fixed version today; as an extra, I'm implementing support for nested shells (e.g. running nix shell twice and bringing both applications into scope, not just the last one).

Patryk27 avatar Feb 16 '25 09:02 Patryk27

Okie, ready! Changes:

  • Added support for envrc,
  • Added support for eshell-syntax-highlighting (it will now correctly mark commands within Nix shell using a green font),
  • Added support for nested shells (nix shell 'nixpkgs#foo' followed by nix shell 'nixpkgs#bar' will bring both packages into scope),
  • exit will now cd into the directory where nix was run, simulating a nested shell better (can be customized via nix-command-eshell-exit-behavior).

(btw, this package is compatible with eat as well -- as in no eat-specific changes are needed, it just works.)

Patryk27 avatar Feb 16 '25 17:02 Patryk27

@knazarov sure you know this but envrc-mode + use flake works amazingly well

From what I understand, envrc-related things don't properly work with shell aliases, but mostly with env variables. In projects I'm working on this leads to subtle bugs. The only solution that will work universally is to actually run the commands in the proper nix subshell (which I hope this pull request is about).

knazarov avatar Feb 17 '25 18:02 knazarov