nix-shell을 사용할 때 PS1을 보존하는 방법은 무엇입니까?

nix-shell을 사용할 때 PS1을 보존하는 방법은 무엇입니까?

nix-shellPS1을 자체 형식으로 변경하십시오.

 [nix-shell:~/snm]$ 

~/.bashrc에 "$IN_NIX_SHELL"에 대한 검사를 추가하려고 시도했지만 작동하지 않았습니다.

내가 찾은 유일한 해결 방법은 다음을 실행하는 것입니다.

nix-shell --run bash

답변1

Nix 2.4[1]부터는 nix-shell원본을 보존 할 수 있는 NIX_SHELL_PRESERVE_PROMPT 환경 변수를 PS1추가하여 추가할 수 있습니다.

  home.sessionVariables = {
    NIX_SHELL_PRESERVE_PROMPT=1;
  };

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

답변2

이를 지원하는 Nix 버전에 가장 적합한 옵션 이지만 자체 설정을 지정하는 동안 다음 과 NIX_SHELL_PRESERVE_PROMPT같은 사실을 남용하는 이전 버전에 대한 해결 방법이 있습니다.nix-shellPS1아니요직접 설정하면 다시 변경할 PROMPT_COMMAND수 있습니다.PS1직전프롬프트 표시(읽기:뒤쪽에 nix-shellPROMPT_COMMAND설정을 통해 변경합니다 PS1(그런 다음 자체 설정을 해제합니다). 예를 들어 표시 전용 프롬프트를 원하는 경우 >다음으로 이동하세요.

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

export PS1="> "변수가 nix-shell완료되어야 하는 시점에 한 번만 실행 되도록 합니다.

답변3

래퍼 스크립트nix-shell

PROMPT_COMMAND솔루션 기반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 "$@"

관련 정보