From efc827f5a9efc072bd6cd248631e9647d048756f Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Wed, 24 May 2023 16:36:32 -0500 Subject: fix: prevent PATH from being overridden Also, mimic Arch's /etc/profile more accurately, switching to append-path API function that is called from auxiliary profile.d scripts. --- alacritty/.config/alacritty/shell.yml | 1 - cargo/.config/profile.d/20-cargo.sh | 5 +++- sh/.config/profile.d/20-dotnet.sh | 5 +++- sh/.config/profile.d/20-go.sh | 8 +++--- sh/.config/profile.d/20-perl.sh | 5 +++- sh/.config/profile.d/20-ruby.sh | 3 ++- sh/.profile | 48 +++++++++++++++++++++++++---------- xdg/.config/profile.d/10-xdg.sh | 3 --- 8 files changed, 54 insertions(+), 24 deletions(-) delete mode 100644 xdg/.config/profile.d/10-xdg.sh diff --git a/alacritty/.config/alacritty/shell.yml b/alacritty/.config/alacritty/shell.yml index 363c6c9..cde304b 100644 --- a/alacritty/.config/alacritty/shell.yml +++ b/alacritty/.config/alacritty/shell.yml @@ -1,6 +1,5 @@ shell: program: /bin/zsh args: - - -l - -c - tmux new -A diff --git a/cargo/.config/profile.d/20-cargo.sh b/cargo/.config/profile.d/20-cargo.sh index 153ea8c..f0c4dbd 100644 --- a/cargo/.config/profile.d/20-cargo.sh +++ b/cargo/.config/profile.d/20-cargo.sh @@ -2,4 +2,7 @@ export RUSTUP_HOME="$XDG_DATA_HOME/rustup" export CARGO_HOME="$XDG_DATA_HOME/cargo" -export PATH="$PATH:$CARGO_HOME/bin" + +append_path "$CARGO_HOME/bin" + +export PATH diff --git a/sh/.config/profile.d/20-dotnet.sh b/sh/.config/profile.d/20-dotnet.sh index 9e89365..f7ac5a6 100644 --- a/sh/.config/profile.d/20-dotnet.sh +++ b/sh/.config/profile.d/20-dotnet.sh @@ -2,4 +2,7 @@ export OMNISHARPHOME="$XDG_CONFIG_HOME/omnisharp" export DOTNET_CLI_HOME="$XDG_DATA_HOME/dotnet" -export PATH="$PATH:$DOTNET_CLI_HOME/tools" + +append_path "$DOTNET_CLI_HOME/tools" + +export PATH diff --git a/sh/.config/profile.d/20-go.sh b/sh/.config/profile.d/20-go.sh index 58d8115..252ba47 100644 --- a/sh/.config/profile.d/20-go.sh +++ b/sh/.config/profile.d/20-go.sh @@ -1,7 +1,9 @@ #!/bin/sh -export GOPATH="$XDG_DATA_HOME/go" -export PATH="$PATH:$GOPATH/bin" - # See: https://drewdevault.com/2022/05/25/Google-has-been-DDoSing-sourcehut.html export GOPRIVATE=git.sr.ht +export GOPATH="$XDG_DATA_HOME/go" + +append_path "$GOPATH/bin" + +export PATH diff --git a/sh/.config/profile.d/20-perl.sh b/sh/.config/profile.d/20-perl.sh index 8ee3b93..13d5e0b 100644 --- a/sh/.config/profile.d/20-perl.sh +++ b/sh/.config/profile.d/20-perl.sh @@ -5,4 +5,7 @@ export PERL_LOCAL_LIB_ROOT="$XDG_DATA_HOME/perl" export PERL5LIB="$PERL_LOCAL_LIB_ROOT/lib/perl5" export PERL_MB_OPT="--install_base '$PERL_LOCAL_LIB_ROOT'" export PERL_MM_OPT="INSTALL_BASE=$PERL_LOCAL_LIB_ROOT" -export PATH="$PATH:$PERL_LOCAL_LIB_ROOT/bin" + +append_path "$PERL_LOCAL_LIB_ROOT/bin" + +export PATH diff --git a/sh/.config/profile.d/20-ruby.sh b/sh/.config/profile.d/20-ruby.sh index 1336853..757bd95 100644 --- a/sh/.config/profile.d/20-ruby.sh +++ b/sh/.config/profile.d/20-ruby.sh @@ -2,5 +2,6 @@ if command -v ruby >/dev/null && command -v gem >/dev/null; then GEM_USER_DIR="$(ruby -r rubygems -e 'puts Gem.user_dir')" - export PATH="$PATH:$GEM_USER_DIR" + [ -d "$GEM_USER_DIR" ] && append_path "$GEM_USER_DIR" + export PATH fi diff --git a/sh/.profile b/sh/.profile index 7095e1a..658c65b 100644 --- a/sh/.profile +++ b/sh/.profile @@ -1,29 +1,51 @@ #!/bin/sh -# shellcheck disable=2046 +# shellcheck disable=2046,1090 + +# Most of this script is a user scoped version of /etc/profile + +# Append "$1" to $PATH when not already in. +# This function API is accessible to scripts in $XDG_CONFIG_HOME/profile.d +append_path() { + case ":$PATH:" in + *:"$1":*) ;; + *) + PATH="${PATH:+$PATH:}$1" + ;; + esac +} # Use systemd-environment-d-generator(8) to generate environment, and export those variables +# NOTE: To avoid overriding PATH, we rename it and append it separately # # See: https://wiki.archlinux.org/title/Environment_variables#Per_Wayland_session for gen in /usr/lib/systemd/user-environment-generators/*; do if [ -e "$gen" ]; then - export $($gen | xargs) + export $($gen | sed 's/^PATH=/GEN_PATH=/' | xargs) + append_path "$GEN_PATH" + unset GEN_PATH fi done +append_path "$HOME/.local/bin" + +# Force PATH to be environment +export PATH + +# Load profiles from $XDG_CONFIG_HOME/profile.d +if test -d "$XDG_CONFIG_HOME"/profile.d/; then + for profile in "$XDG_CONFIG_HOME"/profile.d/*.sh; do + test -r "$profile" && . "$profile" + done + unset profile +fi + +# Unload our profile API functions +unset -f append_path + # Manually parse and export XDG user directories. xdg-user-dirs-update is disabled in -# $XDG_CONFIG_HOME/user-dirs.conf due to how it handles non-existant directories +# $XDG_CONFIG_HOME/user-dirs.conf due to how it handles non-existent directories # # See: https://wiki.archlinux.org/title/XDG_user_directories if [ -e "$HOME/.config/user-dirs.dirs" ]; then export $(xargs <"$HOME/.config/user-dirs.dirs") fi - -# Adopt the behavior of the system wide configuration for application specific settings -# -# See: https://wiki.archlinux.org/title/Command-line_shell#/etc/profile -for script in "$XDG_CONFIG_HOME"/profile.d/*.sh; do - if [ -r "$script" ]; then - # shellcheck disable=1090 - . "$script" - fi -done diff --git a/xdg/.config/profile.d/10-xdg.sh b/xdg/.config/profile.d/10-xdg.sh deleted file mode 100644 index 3edf889..0000000 --- a/xdg/.config/profile.d/10-xdg.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -export PATH="$PATH:$HOME/.local/bin" -- cgit v1.2.3-70-g09d2