Skip to content

Commit

Permalink
Avoid broken bind, complete, color_prompt in nested nix bash (#181
Browse files Browse the repository at this point in the history
)

* Workaround to avoid Nix shell with complete/bind not found

* Make stable behavior in nested bash on nix-shell

* Add note

* Rename var
  • Loading branch information
kachick committed Jul 27, 2023
1 parent decae2d commit d3ca665
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions home/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ shopt -s histappend
HISTSIZE=100000
HISTFILESIZE=4200000

# See https://github.com/kachick/times_kachick/issues/237
#
# Avoid nix provided bash on nix-shell makes broken bind, complete, color_prompt
# Following part of ps... did referer to https://github.com/NixOS/nix/issues/3862#issuecomment-1611837272,
# because non nested bash does not make this problem.
# e.g
# cd ../
# rm ./the_repo/.envrc # Avoid direnv injection
# cd ./the_repo
# nix develop # the executed nix-shell does NOT have problems, so you can wrap as starship
# bash # this nested bash HAS problems without following ps... condition.
if [[ -n "$IN_NIX_SHELL" && "$(ps -o uid= $PPID)" -eq "$UID" ]]; then
in_nested_nix_bash="true"
fi

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
Expand Down Expand Up @@ -71,21 +86,23 @@ if [ -n "$force_color_prompt" ]; then
fi
fi

if [ "$color_prompt" = yes ]; then
if [ "$color_prompt" = yes ] && [ -z "$in_nested_nix_bash" ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm* | rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*) ;;
if [ -z "$in_nested_nix_bash" ]; then
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm* | rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*) ;;

esac
esac
fi

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
Expand Down Expand Up @@ -123,7 +140,7 @@ fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if ! shopt -oq posix && [ -z "$in_nested_nix_bash" ]; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
Expand All @@ -139,15 +156,20 @@ if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then
bind '"\e[B":history-substring-search-forward'
fi

if command -v fzf-share >/dev/null; then
if command -v fzf-share >/dev/null && [ -z "$in_nested_nix_bash" ]; then
source "$(fzf-share)/key-bindings.bash"
source "$(fzf-share)/completion.bash"
fi

# # Delegate history search with "Up arrow key" to fzf
# bind '"\C-\e[A":"\C-r"'

eval "$(starship init bash)"
if [ -n "$in_nested_nix_bash" ]; then
# readlink - https://iww.hateblo.jp/entry/20131108/dash
PS1="${IN_NIX_SHELL} - $(readlink /proc/$$/exe)\n$PS1"
else
eval "$(starship init bash)"
fi

# https://github.com/starship/starship/blob/0d98c4c0b7999f5a8bd6e7db68fd27b0696b3bef/docs/uk-UA/advanced-config/README.md#change-window-title
function set_win_title() {
Expand Down

0 comments on commit d3ca665

Please sign in to comment.