From e994d8f0e356cce3d14df4d1d87724114b92a5d3 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Thu, 3 Feb 2022 23:28:13 +0100 Subject: [PATCH] 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. --- install.sh | 1 + shell/.config/shell/path.sh | 58 +++++++++++++++++++++++++++++++++++ zsh/.config/zsh/.zshrc | 3 ++ zsh/.zshenv | 60 +++++++------------------------------ 4 files changed, 73 insertions(+), 49 deletions(-) create mode 100644 shell/.config/shell/path.sh diff --git a/install.sh b/install.sh index 38afd28..114fb10 100755 --- a/install.sh +++ b/install.sh @@ -53,6 +53,7 @@ stow_packages() { nvim \ python \ readline \ + shell \ ssh \ tmux \ x11 \ diff --git a/shell/.config/shell/path.sh b/shell/.config/shell/path.sh new file mode 100644 index 0000000..55082ea --- /dev/null +++ b/shell/.config/shell/path.sh @@ -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 </dev/null 2>&1 && MANPATH="$(unset MANPATH; manpath)" +while read -r dir; do + case ":${MANPATH:=$dir}:" in + *:"$dir":*) ;; + *) MANPATH="$dir:$MANPATH" ;; + esac +done </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 + diff --git a/zsh/.config/zsh/.zshrc b/zsh/.config/zsh/.zshrc index b232072..f1b151b 100644 --- a/zsh/.config/zsh/.zshrc +++ b/zsh/.config/zsh/.zshrc @@ -1,6 +1,9 @@ #!/bin/zsh # 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. while read -r f; do [ -f "$f" ] && source "$f"; done </dev/null 2>&1 && MANPATH="$(unset MANPATH; manpath)" -while read -r dir; do - case ":${MANPATH:=$dir}:" in - *:"$dir":*) ;; - *) MANPATH="$dir:$MANPATH" ;; - esac -done </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 +# Add custom directories to PATH and MANPATH. +# +# 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): +# +# $ZDOTDIR/.zshenv <-- This file. +# /etc/zprofile <-- On MacOS this sources /usr/libexec/path_helper, +# which would undo what we do here. +# $ZDOTDIR/.zshrc <-- So we defer our PATH manipulation to this file. +# +[ "$(uname -s)" = "Darwin" ] || source "$XDG_CONFIG_HOME/shell/path.sh"