NixOS / nix

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

Shell prompt and arrow key misbihave when entering sub-shells inside `nix develop` #6982

Open ShamrockLee opened 2 years ago

ShamrockLee commented 2 years ago

Describe the bug

When entering sub-shells inside nix develop, PS1 environment variable escape sequence got interpreted wrongly, and arrow keys doesn't work but print out ^[[C^[[D^[[A^[[B instead.

Screenshot_20220901_161939

This used to happen to nix-shell, but was fixed one or two years ago.

Steps To Reproduce

  1. Run

    bash
    export PS1='\n\[\033[1;32m\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\$\[\033[0m\] '
  2. See the correct shell prompt

    [shamrock@nixos-202104:~]$ your-command
  3. Run

    nix develop github:NixOS/nixpkgs#hello
    bash
  4. See the erroneous shell prompt

    \[\][\[\]shamrock@nixos-202104:~]$\[\] your-command
  5. Press , , and , and see

    \[\][\[\]shamrock@nixos-202104:~]$\[\] ^[[A^[[B^[[C^[[D
  6. Press ⌃ Control + C to prevent the above stuff from being executed and show a new input prompt

  7. Type ⌃ Control + D ⌃ Control + D or run

    exit
    exit

    to get back.

Expected behavior

The shell prompt and arrow keys always work correctly.

nix-env --version output nix-env (Nix) 2.9.1

Additional context

Nix info:

Text inside the above screenshot:

[shamrock@nixos-202104:~]$ echo "begin${PS1}end"
begin\n\[\033[1;32m\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\$\[\033[0m\] end

[shamrock@nixos-202104:~]$ nix develop github:NixOS/nixpkgs#hello
error (ignored): error: cannot find flake 'flake:nixpkgs' in the flake registries

[shamrock@nixos-202104:~]$ echo "begin${PS1}end"
begin\n\[\033[1;32m\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\$\[\033[0m\] end

[shamrock@nixos-202104:~]$ bash

\[\][\[\]shamrock@nixos-202104:~]$\[\] echo "begin${PS1}end"
begin\n\[\033[1;32m\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\$\[\033[0m\] end

\[\][\[\]shamrock@nixos-202104:~]$\[\] ^[[A^[[B^[[C^[[D^C

\[\][\[\]shamrock@nixos-202104:~]$\[\] exit

[shamrock@nixos-202104:~]$
tomberek commented 2 years ago

Similar: https://github.com/NixOS/nix/issues/6091 The specific problem in your step three is described here: https://github.com/NixOS/nix/issues/6091#issuecomment-1038247010

When you ran "bash", did you intend that to be the normal bash for building, or bashInteractive?

ShamrockLee commented 2 years ago

When you ran \"bash\", did you intend that to be the normal bash for building, or bashInteractive?

I didn't even know there's a difference.

I would expect bashInteractive, since the motivation for me to file this issue is that it breaks the VSCode terminal.

tomberek commented 2 years ago

Technically, the bash provided is the one used for building the package. But for a bit of usability the shell started is bashInteractive (it turns on things that are nice when used interactively). So when you start the new shell, it’s not as usable.

This is known to happen, but not quite clear what should be done about improving the situation. Should we overwrite bash in particular? What about other shells? What about readline-wrapped variants of other tools like SQLite? Should we unset the SHELL or warn if you are descending into a potentially unusable sub shell? But then again, you may want to run build scripts or build phase that require it.

ShamrockLee commented 2 years ago

@tomberek IMO, the least-change improvement would be documenting this behavior alongside the nativeBuildInputs = [ bashInteractive ]; workaround for people to fix their VSCode terminal. Another possible inprovement would be to add flags to nix develop (say --interactive or --non-interactive) to specify which kinds of Bash to use.

knedlsepp commented 2 years ago

Looks like https://github.com/NixOS/nix/pull/7010 might be about to solve this.

ilyagr commented 1 year ago

I'm still running into this in spite of #6091 and #7010 being closed. In addition to problems with PS1 and arrow keys, I also get a lot of errors from my .bashrc and related config. The superficial cause seems to be the same: bash pretends to be sh and refuses to understand bash-specific commands. The errors are along the lines of:

bash: complete: command not found
bash: bind: command not found

My workaround: I put alias bash=/usr/bin/bash in my shell config next to nix initialization. I think this is pretty ugly, but it works.

I'm running into this on Nix 2.15.1 on Debian trixie; I have no idea how well my workaround would work in other environments. I just started trying out Nix, so I have no idea how to do something like that alias in NixOS or elsewhere.