zsh: fix PATH manipulation on MacOS

My previous fix for /usr/libexec/path_helper messing with the PATH was
no longer working because zsh sources /etc/zprofile (which on MacOS
executes path_helper) AFTER $HOME/.zshenv, thus overwriting my changes.
This commit is contained in:
Fernando Schauenburg 2022-02-03 23:28:13 +01:00
parent 38f286411d
commit e994d8f0e3
4 changed files with 73 additions and 49 deletions

View file

@ -53,6 +53,7 @@ stow_packages() {
nvim \ nvim \
python \ python \
readline \ readline \
shell \
ssh \ ssh \
tmux \ tmux \
x11 \ x11 \

View file

@ -0,0 +1,58 @@
# This file is meant to be sourced from either $ZDOTDIR/.zshenv or
# $ZDOTDIR/.zshrc (see those files for explanation).
# Prevent path_helper from messing with the PATH when starting tmux.
#
# Clearing PATH before path_helper executes (from /etc/profile) will prevent it
# from prepending the default PATH to our (previously) chosen PATH, and will
# allow the rest of this file to set up PATH and MANPATH correctly.
#
# For details see: https://superuser.com/a/583502
#
[ "$(uname -s)" = "Darwin" ] && { PATH=""; source /etc/profile; }
# Add custom bin dirs to PATH if they exist and are not already in PATH.
while read -r dir; do
case ":${PATH:=$dir}:" in
*:"$dir":*) ;;
*) PATH="$dir:$PATH" ;;
esac
done <<EOL
$LOCAL_PREFIX/bin
$LOCAL_PREFIX/opt/findutils/libexec/gnubin
$HOME/.local/bin
EOL
export PATH
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
command -v manpath >/dev/null 2>&1 && MANPATH="$(unset MANPATH; manpath)"
while read -r dir; do
case ":${MANPATH:=$dir}:" in
*:"$dir":*) ;;
*) MANPATH="$dir:$MANPATH" ;;
esac
done <<EOL
$LOCAL_PREFIX/share/man
$LOCAL_PREFIX/opt/findutils/libexec/gnuman
$HOME/.local/share/man
EOL
export MANPATH
unset dir
# These checks habe to be done after PATH manipulation above so we can find
# installed programs if they are in the added paths.
if command -v nvim >/dev/null 2>&1; then
export EDITOR="nvim"
else
export EDITOR="vim"
fi
if command -v brew >/dev/null 2>&1; then
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
fi

View file

@ -1,6 +1,9 @@
#!/bin/zsh #!/bin/zsh
# Set up zsh for interactive use (options, prompt, aliases, etc.) # Set up zsh for interactive use (options, prompt, aliases, etc.)
# On MacOS, manipulate PATH and MANPATH here (see explanation in .zshenv).
[ "$(uname -s)" = "Darwin" ] && source "$XDG_CONFIG_HOME/shell/path.sh"
# Source additional configurations if available. # Source additional configurations if available.
while read -r f; do [ -f "$f" ] && source "$f"; done <<EOL while read -r f; do [ -f "$f" ] && source "$f"; done <<EOL
$ZDOTDIR/aliases $ZDOTDIR/aliases

View file

@ -36,53 +36,15 @@ export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
unset rst unset rst
} }
# Prevent path_helper from messing with the PATH when starting tmux. # Add custom directories to PATH and MANPATH.
# See: https://superuser.com/a/583502 #
[ "$(uname -s)" = "Darwin" ] && { PATH=""; source /etc/profile; } # On MacOS, we skip it here because these relevant files are sourced in order
# by zsh on startup ($ZDOTDIR defaults to $HOME if not set):
# Add custom bin dirs to PATH if they exist and are not already in PATH. #
while read -r dir; do # $ZDOTDIR/.zshenv <-- This file.
case ":${PATH:=$dir}:" in # /etc/zprofile <-- On MacOS this sources /usr/libexec/path_helper,
*:"$dir":*) ;; # which would undo what we do here.
*) PATH="$dir:$PATH" ;; # $ZDOTDIR/.zshrc <-- So we defer our PATH manipulation to this file.
esac #
done <<EOL [ "$(uname -s)" = "Darwin" ] || source "$XDG_CONFIG_HOME/shell/path.sh"
$LOCAL_PREFIX/opt/findutils/libexec/gnubin
$LOCAL_PREFIX/bin
$HOME/.local/bin
EOL
export PATH
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
command -v manpath >/dev/null 2>&1 && MANPATH="$(unset MANPATH; manpath)"
while read -r dir; do
case ":${MANPATH:=$dir}:" in
*:"$dir":*) ;;
*) MANPATH="$dir:$MANPATH" ;;
esac
done <<EOL
$LOCAL_PREFIX/opt/findutils/libexec/gnuman
$LOCAL_PREFIX/share/man
$HOME/.local/share/man
EOL
export MANPATH
unset dir
# These checks habe to be done after PATH manipulation above so we can find
# installed programs if they are in the added paths.
if command -v nvim >/dev/null 2>&1; then
export EDITOR="nvim"
else
export EDITOR="vim"
fi
if command -v brew >/dev/null 2>&1; then
LOCAL_PREFIX="$(brew --prefix)"
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
fi