Nix: UX issue with nix-shell: No clean way to preserve my $PS1

Created on 9 Mar 2017  路  3Comments  路  Source: NixOS/nix

I have customized $PS1 with my personal notion of ideal usability, but nix-shell replaces it with one that is hard coded.

Is there some clean way to tell nix-shell which environment variables it should preserve? If not maybe nix-shell could benetif from flags like:

nix-shell --preserve-variable PS1 --ignore-variable FOO --do-what-you-want-with-variable BAR

I think there is a way to preserve my customized PS1 variable with a --command 'export PS1=...' flag, but I'm already putting all of these nix-shell commands into a bash alias in my .bashrc, and I don't want to invest the time needed to figure out the proper escaping, and I don't want to have to edit and compile recompile nix-shell either.

This is only my second day with nix, not counting a few previous failed attempts to get into it over the years, so maybe this is something that is trivial to fix if you've already invested the time to learn the nix language...

Most helpful comment

I'm using PROMPT_COMMAND in my ~/.bashrc as a workaround:

function restore_prompt_after_nix_shell() {
  if [ "$PS1" != "$PROMPT" ]; then
    PS1=$PROMPT
    PROMPT_COMMAND=""
  fi
}

PROMPT_COMMAND=restore_prompt_after_nix_shell
PROMPT='$ '
export PS1=$PROMPT

All 3 comments

I think this isn't even intentional. But maybe alias nix-shell='nix-shell --command "$(declare -p PS1); return"' approximates a workaround.

I'm using PROMPT_COMMAND in my ~/.bashrc as a workaround:

function restore_prompt_after_nix_shell() {
  if [ "$PS1" != "$PROMPT" ]; then
    PS1=$PROMPT
    PROMPT_COMMAND=""
  fi
}

PROMPT_COMMAND=restore_prompt_after_nix_shell
PROMPT='$ '
export PS1=$PROMPT

Digging into this a little, I see that in Release 1.6 (2013-09-10):
https://github.com/NixOS/nix/blob/6924bdf2bf8f50f2e6ec5d490571594450aba13a/doc/manual/release-notes/rl-1.6.xml#L35-L37
and in Release 2.0 (2018-02-22)
https://github.com/NixOS/nix/blob/906afedd238e4d83ef9ea4cf5a3aca77e980d582/doc/manual/release-notes/rl-2.0.xml#L566-L573

The relevant code appears to be (from inspection, I haven't understood or tested this yet) https://github.com/NixOS/nix/blob/ea5bcfb59bdf34325d51aa70c8fcb7b399f99d09/src/nix-build/nix-build.cc#L449

Is there any appetite for changing this to not set PS1 now that there is another way to tell if you are in a nix-shell, or are we more concerned with backward compatability?
(I'm happy to (try to) make a patch if so!)
If we choose to not set PS1, we could potentially give a /etc/bashrc that gives different default prompts depending on if $IN_NIX_SHELL is set.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eqyiel picture eqyiel  路  3Comments

Infinisil picture Infinisil  路  3Comments

ericsagnes picture ericsagnes  路  4Comments

luc65r picture luc65r  路  3Comments

fare picture fare  路  4Comments