NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
11.48k stars 1.44k forks source link

`nix shell` use `bash-prompt-prefix` from nix configuration #8177

Open GuillaumeDesforges opened 1 year ago

GuillaumeDesforges commented 1 year ago

Is your feature request related to a problem? Please describe. It is hard to track "how many nix shells deep" I am. I want to know through the prompt.

Describe the solution you'd like When I enter a Nix shell with nix shell ..., I want the prompt to be updated using the bash-prompt-prefix configuration from nix.conf, just like nix develop does. Every new nix shell should concatenate with the previous.

Describe alternatives you've considered There seems to be a --bash-prompt-prefix flag according to the doc of nix shell (2.13.3) but it does not do anything.

Priorities

Add :+1: to issues you find important.

J-Swift commented 9 months ago

You and I seem to think very similarly @GuillaumeDesforges : D. Each time I come looking for info on something I want, you have already opened an issue for it.

Did some research on this one and heres a dump of my findings:

$ cat < $tmpfile [[ -f ~/.bash_profile ]] && . ~/.bash_profile

export PS1="[new-prefix]\040$PS1" EOF

$ nix shell nixpkgs#jq --command bash --rcfile $tmpfile


- This has issues because
  - you now have to pass an additional shell-specific argument
  - you have to specify the shell command explicitly so the shell-specific argument is parsed/passed properly
  - it seems really flimsy to me

I was able to at least verify this does work (even with multiple layers of `nix shell`) but it was hardcoding assumptions based on my local environment in terms of profile sourcing and shell.
xav-ie commented 6 months ago

By reading the source here: https://github.com/NixOS/nix/blob/ea2dd166235e049699cf7f70c243c2b83089f824/src/nix/run.cc#L105-L140

I was able to find that the shell command does not do much that you can measure other than that it changes the PATH. However, this could be helpful. Maybe you can figure out your default/expected path before shells. And then you can only print the things that get added onto the path.

Example:

~
❯ echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/Users/xavierruiz/.nix-profile/bin:/etc/profiles/per-user/xavierruiz/bin:/run/current-system/sw/bin:/nix/var/nix/profiles
/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
~
❯ nix shell nixpkgs#nodejs nixpkgs#yarn
~
❯ echo $PATH
/nix/store/rr171p6xks9xf2f0ds9wyi4mc36xm5c1-nodejs-20.10.0/bin:/nix/store/45vwimqzlg79phkshmci9rw2r94vns9y-yarn-1.22.19/bin:/opt/homebrew/bin:/opt/homebrew/sb
in:/Users/xavierruiz/.nix-profile/bin:/etc/profiles/per-user/xavierruiz/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/b
in:/usr/sbin:/bin:/sbin
~
❯ # ideally, new shell would look like:
/nix/store/rr171p6xks9xf2f0ds9wyi4mc36xm5c1-nodejs-20.10.0/bin:/nix/store/45vwimqzlg79phkshmci9rw2r94vns9y-yarn-1.22.19/bin:
~
❯  # or you can do something like 
❄️
~
❯  # each shell resets the expected "default/expected" path. so another shell adds another flake

I think this functionality can be accomplished with some clever bash scripting, which I don't have time for right now.