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

Avoid broken bind, complete, color_prompt in nested nix bash #181

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the readlink ... does not show anything in macos 🤔

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