NixOS / nix

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

nix develop does not seem to re-evaluate ~/.bashrc or does so before setting $IN_NIX_SHELL #9174

Open jansol opened 9 months ago

jansol commented 9 months ago

Describe the bug

I have a .bashrc that sets the prompt based on $IN_NIX_SHELL but the shell given by nix develop does not display that prompt. env shows the variable as set, and manually sourcing the file changes the prompt to what I expect.

nix-env --version output

nix-env (Nix) 2.17.0

abathur commented 9 months ago

https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop#description

The prompt used by the bash shell can be customised by setting the bash-prompt, bash-prompt-prefix, and bash-prompt-suffix settings in nix.conf or in the flake's nixConfig attribute.

(Also CLI options: https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop#opt-bash-prompt etc.)

jansol commented 9 months ago

Thanks, that gets me sort of part of the way there although it still is a rather surprising behaviour.

bash-prompt = \[\033[34m\]\u\[\033[0m\]@\[\033[34m\]\h\[\033[0m\]:\[\033[35m\]\w\[\033[0m\]\[\033[34m\]$ \[\033[0m\]

This is nowhere as readable as the bashrc version which uses variables for the ansi escape codes, and there does not seem to be a way of having the prompt depend on whether or not the shell is behind an SSH connection, like the bashrc version does.

I also did not find any way of specifying that I want the prompt to end in a space (other than the workaround of moving the style reset ansi escape past said space). Putting the prompt in quotes just gave me... quotes around the prompt and any trailing whitespace before a comment mark seems to get trimmed off too.

abathur commented 9 months ago

I'm not a fan of how it works, either. You may have more luck using PROMPT_COMMAND. I don't think Nix touches it, so it should be able to set PS1 however you need.

I also did not find any way of specifying that I want the prompt to end in a space (other than the workaround of moving the style reset ansi escape past said space). Putting the prompt in quotes just gave me... quotes around the prompt and any trailing whitespace before a comment mark seems to get trimmed off too.

https://github.com/NixOS/nix/issues/8487#issuecomment-1585704206 has a workaround for this

svenssonaxel commented 3 months ago

For future reference: [ -n "$NIX_GCROOT" ] && set-prompt-for-nix-shell in .bashrc seems to work for me.