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

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

Closed
drewm1980 opened this issue Mar 9, 2017 · 3 comments · Fixed by #3988
Closed

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

drewm1980 opened this issue Mar 9, 2017 · 3 comments · Fixed by #3988
Assignees

Comments

@drewm1980
Copy link

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...

@ben0x539
Copy link

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

@pjones
Copy link

pjones commented Dec 23, 2018

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

@brprice
Copy link

brprice commented Jul 27, 2020

Digging into this a little, I see that in Release 1.6 (2013-09-10):

<para><command>nix-shell</command> now sets the shell prompt
(<envar>PS1</envar>) to ensure that Nix shells are distinguishable
from your regular shells.</para>

and in Release 2.0 (2018-02-22)
<para><command>nix-shell</command> <link
xlink:href="https://github.com/NixOS/nix/commit/ea59f39326c8e9dc42dfed4bcbf597fbce58797c">now</link>
sets the <varname>IN_NIX_SHELL</varname> environment variable
during evaluation and in the shell itself. This can be used to
perform different actions depending on whether you’re in a Nix
shell or in a regular build. Nixpkgs provides
<varname>lib.inNixShell</varname> to check this variable during
evaluation.</para>

The relevant code appears to be (from inspection, I haven't understood or tested this yet)

R"s([ -n "$PS1" ] && PS1='\n\[\033[1;32m\][nix-shell:\w]\$\[\033[0m\] '; )s"

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.

domenkozar pushed a commit that referenced this issue Sep 11, 2020
Fixes #1268

`nix-shell` will now preserve `PS1` if the `NIX_SHELL_PRESERVE_PROMPT`
environment variable is set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants