Make bashrc_forward truly self-replicating, fix /nix/store self-referential problem, remove aliases, add comment about bash --rcfile

This commit is contained in:
cubernetes 2026-10-20 12:20:38 +02:00
parent 57d1a9f0e2
commit 3dd251f192

View file

@ -17,11 +17,15 @@ bashrc_get () {
echo "set +v" echo "set +v"
} }
_strip_comments () {
grep -v '^[[:space:]]*#\+-#'
}
bashrc_load () { bashrc_load () {
if [ "$_s" ]; then if [ "$_s" ]; then
#-# Relay .bashrc if it has already been forwarded #-# Relay .bashrc if it has already been forwarded
: "${_s#*[B]ASHRC_FORWARDED=1[[:space:]]}" : "${_s#*[B]ASHRC_FORWARDED=1[[:space:]]}"
printf %s "${_%[[:space:]][s]et +v*}" printf '%s\n%s\n%s\n' "BASHRC_FORWARDED=1" "${_%[[:space:]][s]et +v*}" "set +v"
else else
#-# Warning: Your `.bashrc` MUST NOT print any error messages at all! #-# Warning: Your `.bashrc` MUST NOT print any error messages at all!
#-# Additionally, be aware that every time you use bashrc_forward, #-# Additionally, be aware that every time you use bashrc_forward,
@ -41,9 +45,10 @@ bashrc_load () {
fi | fi |
#-# Removing comments (if marked as such by using #-#) because final `.bashrc` must be #-# Removing comments (if marked as such by using #-#) because final `.bashrc` must be
#-# below 131k bytes because of resource limits (`MAX_ARG_STRLEN` on Linux) #-# below 131k bytes because of resource limits (`MAX_ARG_STRLEN` on Linux)
grep -v -e '^[[:space:]]*#\+-#' -e ' # *bashrc_forward: .*\<ignore\>[ a-z]*$' | _strip_comments |
grep -v -e ' # *bashrc_forward: .*\<ignore\>[ a-z]*$' |
sed ' sed '
s,/nix/store/[^/]\+,, s,/[n]ix/store/[^/]\+,,
s,'\'','\''\\'\'''\''\\'\''\\'\'''\'''\''\\'\'''\'',g s,'\'','\''\\'\'''\''\\'\''\\'\'''\'''\''\\'\'''\'',g
' '
} }
@ -55,6 +60,8 @@ bashrc_load () {
#-# *unless you relay the `.bashrc`, in that case the remote system needs sed, grep, cat and bash. #-# *unless you relay the `.bashrc`, in that case the remote system needs sed, grep, cat and bash.
bashrc_forward () { bashrc_forward () {
printf %s ' printf %s '
#-# If bashrc was forwarded using environment variable, unset it, since we do not need it anymore and to keep the env clean. Relaying will set it anyways to something fresh.
unset FORWARDED_BASHRC
_argv_wrap () { _argv_wrap () {
if >/dev/null 2>&1 command -v bash; then if >/dev/null 2>&1 command -v bash; then
bash -c '\'' bash -c '\''
@ -80,12 +87,6 @@ bashrc_forward () {
#-# no circumstance will the last line be printed, no matter what amount of trailing backslashes you add. #-# no circumstance will the last line be printed, no matter what amount of trailing backslashes you add.
'"$(bashrc_load)"' '"$(bashrc_load)"'
alias bash="-(exec -a sh bash {CURSOR})"
#-# tmux has a tight (16384) argument length limit. To bypass that limit, we pass the serialized `.bashrc`
#-# via an environment variable and deserialize it using eval. Unfortunately, it does not work perfectly.
alias tmuxb="FORWARDED_BASHRC=\$(bashrc_forward) tmux -2 new-session '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''eval \"\$FORWARDED_BASHRC\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''"
#-# Using PROMPT_COMMAND instead of DEBUG trap because of speed tradeoffs (DEBUG trap runs way too often) #-# Using PROMPT_COMMAND instead of DEBUG trap because of speed tradeoffs (DEBUG trap runs way too often)
#-# Also explicitly removing the export flag as to not append needlessly, in case the remote system exported it accidentally. #-# Also explicitly removing the export flag as to not append needlessly, in case the remote system exported it accidentally.
declare +x PROMPT_COMMAND=$PROMPT_COMMAND'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' declare +x PROMPT_COMMAND=$PROMPT_COMMAND'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''
@ -155,8 +156,9 @@ bashrc_forward () {
fi fi
fi fi
} }
#-# Allow accepting shell arguments
_argv_wrap \ _argv_wrap \
' ' | _strip_comments
} }
#-# HARD TO FIX BUG: shopt -u expand_aliases must be run in ENV so bash-abbr works, but ENV is run before the shell switches to interactive #-# HARD TO FIX BUG: shopt -u expand_aliases must be run in ENV so bash-abbr works, but ENV is run before the shell switches to interactive
@ -179,3 +181,5 @@ bashrc_forward () {
alias brc='*"$(bashrc_forward)"' alias brc='*"$(bashrc_forward)"'
alias bbrc='*sh -c "$(bashrc_forward)"' alias bbrc='*sh -c "$(bashrc_forward)"'
alias tbrc='*-t "$(bashrc_forward)"' alias tbrc='*-t "$(bashrc_forward)"'
#-# TODO consider bash --rcfile <() approach, maybe it works better than ENV approach. Would also solve the "HARD TO FIX BUG" from above.