3

nix-shell changes PS1 to its own format.

 [nix-shell:~/snm]$ 

I tried adding check for "$IN_NIX_SHELL" inside ~/.bashrc, but it didn't work.

The only work around I found is to run

nix-shell --run bash
3
  • What is the PS1 that you want to keep, and where do you set it? Not knowing much about nix-shell, it seems obvious to me that if you run another shell, it may set its own prompt. You may want to look into how to configure the prompt for that shell.
    – Kusalananda
    Jan 14, 2022 at 14:26
  • my PS1 in set in ~/.bashrc, but nix-shell uses its own unless i explicitly tell it to run bash.
    – andreoss
    Jan 14, 2022 at 18:49
  • But having it run bash kinda defeats the purpose of running nix-shell, doesn't it? Why don't you just configure the prompt you want to use in the nix-shell? Or, alternatively, just use bash. Note that I don't quite know what nix-shell is, but am assuming it's a shell like any other.
    – Kusalananda
    Jan 14, 2022 at 19:21

3 Answers 3

3

Since Nix 2.4 [1] there is NIX_SHELL_PRESERVE_PROMPT environment variable which makes nix-shell preserve original PS1. It can be added by

  home.sessionVariables = {
    NIX_SHELL_PRESERVE_PROMPT=1;
  };

1: https://discourse.nixos.org/t/nix-2-4-released/15822

1

While NIX_SHELL_PRESERVE_PROMPT is the best option for Nix versions that support it, there exists a workaround for older versions which abuses the fact that while nix-shell sets its own PS1 it does not set its own PROMPT_COMMAND. This lets you change PS1 again right before the prompt is displayed (read: after nix-shell changes it) by making PROMPT_COMMAND set PS1 (and then unset itself afterwards). For example, if you want a prompt that just says > , you could go

PROMPT_COMMAND='export PS1="> "; unset PROMPT_COMMAND'

to make export PS1="> " run once at a point when nix-shell should be done with that variable

0

a wrapper script for nix-shell

based on the PROMPT_COMMAND solution by Sara J

#!/usr/bin/env bash

# run nix-shell with a custom prompt string

# based on /etc/bashrc
# Provide a nice prompt if the terminal supports it.
if [ "$TERM" != "dumb" ] || [ -n "$INSIDE_EMACS" ]; then
  PROMPT_COLOR="1;31m"
  ((UID)) && PROMPT_COLOR="1;32m"
  if [ -n "$INSIDE_EMACS" ]; then
    # Emacs term mode doesn't support xterm title escape sequence (\e]0;)
    PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
  else
    PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
  fi
  if test "$TERM" = "xterm"; then
    PS1="\[\033]2;\h:\u:\w\007\]$PS1"
  fi
fi

# https://unix.stackexchange.com/a/729431/295986
export PROMPT_COMMAND="export PS1=${PS1@Q}; unset PROMPT_COMMAND"

exec nix-shell "$@"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .