feat(shell): add .zshenv Homebrew init and Trash-backed rm

Move brew shellenv into .zshenv so PATH is available outside
interactive shells, and make install guarantee a Trash backend
for safer rm aliases on both macOS and Linux.
This commit is contained in:
mio
2026-07-26 16:28:09 +08:00
parent 17ecc5f67c
commit 9377023e28
4 changed files with 62 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
if [ -x /opt/homebrew/bin/brew ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -x /usr/local/bin/brew ]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
+11 -5
View File
@@ -1,8 +1,3 @@
# Homebrew initialization
if [ -d '/opt/homebrew' ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc. # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n] # Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below. # confirmations, etc.) must go above this block; everything else may go below.
@@ -222,6 +217,17 @@ source "$ZSH/oh-my-zsh.sh"
# Configs that can only work after "source $ZSH/oh-my-zsh.sh", such as Aliases that depend oh-my-zsh plugins # Configs that can only work after "source $ZSH/oh-my-zsh.sh", such as Aliases that depend oh-my-zsh plugins
# ponytail: Trash aliases accept paths, not every rm flag; add a wrapper only if flag compatibility is required.
if [[ "$OSTYPE" == darwin* && -x /usr/bin/trash ]]; then
alias rm='/usr/bin/trash'
elif [[ "$OSTYPE" == darwin* ]] && command -v rmtrash &> /dev/null; then
alias rm='rmtrash'
elif [[ "$OSTYPE" != darwin* ]] && command -v gio &> /dev/null; then
alias rm='gio trash'
elif [[ "$OSTYPE" != darwin* ]] && command -v trash-put &> /dev/null; then
alias rm='trash-put'
fi
# Now source fzf.zsh , otherwise Ctr+r is overwritten by ohmyzsh # Now source fzf.zsh , otherwise Ctr+r is overwritten by ohmyzsh
if [[ -x "$HOME/.fzf/bin/fzf" ]] && ! fzf --zsh >/dev/null 2>&1; then if [[ -x "$HOME/.fzf/bin/fzf" ]] && ! fzf --zsh >/dev/null 2>&1; then
path=("$HOME/.fzf/bin" ${path:#$HOME/.fzf/bin}) path=("$HOME/.fzf/bin" ${path:#$HOME/.fzf/bin})
+30 -8
View File
@@ -56,6 +56,14 @@ run_as_root() {
fi fi
} }
has_trash_command() {
if [[ "$OSTYPE" == darwin* ]]; then
command -v trash &> /dev/null || command -v rmtrash &> /dev/null
else
command -v gio &> /dev/null || command -v trash-put &> /dev/null
fi
}
install_eza_binary() { install_eza_binary() {
local archive local archive
local binary local binary
@@ -177,19 +185,20 @@ install_packages() {
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi fi
brew install "${base_packages[@]}" brew install "${base_packages[@]}"
command -v trash &> /dev/null || brew install rmtrash
;; ;;
ubuntu|debian) ubuntu|debian)
run_as_root apt-get update run_as_root apt-get update
run_as_root apt-get install -y "${base_packages[@]}" curl tar run_as_root apt-get install -y "${base_packages[@]}" curl tar libglib2.0-bin
;; ;;
fedora) fedora)
run_as_root dnf install -y "${base_packages[@]}" curl tar run_as_root dnf install -y "${base_packages[@]}" curl tar glib2
;; ;;
centos|rhel) centos|rhel)
run_as_root yum install -y "${base_packages[@]}" curl tar run_as_root yum install -y "${base_packages[@]}" curl tar glib2
;; ;;
arch|manjaro) arch|manjaro)
run_as_root pacman -Syu --noconfirm "${base_packages[@]}" curl tar run_as_root pacman -Syu --noconfirm "${base_packages[@]}" curl tar glib2
;; ;;
*) *)
echo "Unsupported operating system. Please install ${base_packages[*]} manually." echo "Unsupported operating system. Please install ${base_packages[*]} manually."
@@ -207,15 +216,20 @@ install_packages() {
return 1 return 1
fi fi
done done
if ! has_trash_command; then
echo "Error: no command is available for moving files to Trash" >&2
return 1
fi
} }
# Check if required packages are installed, if not, install them # Check if required packages are installed, if not, install them
if command -v zsh &> /dev/null && command -v git &> /dev/null && command -v wget &> /dev/null && command -v curl &> /dev/null && command -v tar &> /dev/null && command -v fc-cache &> /dev/null && command -v eza &> /dev/null && command -v zoxide &> /dev/null && command -v autoconf &> /dev/null; then if command -v zsh &> /dev/null && command -v git &> /dev/null && command -v wget &> /dev/null && command -v curl &> /dev/null && command -v tar &> /dev/null && command -v fc-cache &> /dev/null && command -v eza &> /dev/null && command -v zoxide &> /dev/null && command -v autoconf &> /dev/null && has_trash_command; then
echo -e "ZSH, Git, wget, curl, tar, fontconfig, eza, zoxide, and autoconf are already installed\n" echo -e "ZSH, Git, wget, curl, tar, fontconfig, eza, zoxide, autoconf, and Trash support are already installed\n"
else else
echo "Installing required packages..." echo "Installing required packages..."
install_packages install_packages
echo -e "zsh, wget, curl, tar, git, fontconfig, eza, zoxide, and autoconf installed\n" echo -e "zsh, wget, curl, tar, git, fontconfig, eza, zoxide, autoconf, and Trash support installed\n"
fi fi
if [ -f ~/.gitconfig.backup ]; then if [ -f ~/.gitconfig.backup ]; then
@@ -229,8 +243,10 @@ fi
ZSH="$HOME/.oh-my-zsh" ZSH="$HOME/.oh-my-zsh"
zshrc_download=$(mktemp "$HOME/.zshrc.download.XXXXXX") zshrc_download=$(mktemp "$HOME/.zshrc.download.XXXXXX")
trap 'rm -f "$zshrc_download"' EXIT zshenv_download=$(mktemp "$HOME/.zshenv.download.XXXXXX")
trap 'rm -f "$zshrc_download" "$zshenv_download"' EXIT
wget -q "https://git.miomio.moe/mio/easyzsh/raw/branch/master/.zshrc" -O "$zshrc_download" wget -q "https://git.miomio.moe/mio/easyzsh/raw/branch/master/.zshrc" -O "$zshrc_download"
wget -q "https://git.miomio.moe/mio/easyzsh/raw/branch/master/.zshenv" -O "$zshenv_download"
# echo -e "The setup will be installed in '~/.config/ezsh'\n" # echo -e "The setup will be installed in '~/.config/ezsh'\n"
# echo -e "Place your personal zshrc config files under '~/.config/ezsh/zshrc/'\n" # echo -e "Place your personal zshrc config files under '~/.config/ezsh/zshrc/'\n"
@@ -258,7 +274,13 @@ if [ -e "$HOME/.zshrc" ] || [ -L "$HOME/.zshrc" ]; then
cp -Pp "$HOME/.zshrc" "$zshrc_backup" cp -Pp "$HOME/.zshrc" "$zshrc_backup"
echo -e "Backed up the current .zshrc to $zshrc_backup\n" echo -e "Backed up the current .zshrc to $zshrc_backup\n"
fi fi
if [ -e "$HOME/.zshenv" ] || [ -L "$HOME/.zshenv" ]; then
zshenv_backup=$(mktemp "$HOME/.zshenv.backup.$(date +"%Y%m%d-%H%M%S").XXXXXX")
cp -Pp "$HOME/.zshenv" "$zshenv_backup"
echo -e "Backed up the current .zshenv to $zshenv_backup\n"
fi
mv "$zshrc_download" "$HOME/.zshrc" mv "$zshrc_download" "$HOME/.zshrc"
mv "$zshenv_download" "$HOME/.zshenv"
trap - EXIT trap - EXIT
# cp -f ezshrc.zsh ~/.config/ezsh/ # cp -f ezshrc.zsh ~/.config/ezsh/
# cp -f p10k.zsh ~/.config/ezsh/ # cp -f p10k.zsh ~/.config/ezsh/
+16 -2
View File
@@ -44,16 +44,19 @@ fi
main_bin="$TEMP_DIR/main-bin" main_bin="$TEMP_DIR/main-bin"
main_home="$TEMP_DIR/main-home" main_home="$TEMP_DIR/main-home"
mkdir -p "$main_bin" "$main_home" mkdir -p "$main_bin" "$main_home"
for command_name in zsh git curl tar fc-cache eza zoxide autoconf; do for command_name in zsh git curl tar fc-cache eza zoxide autoconf trash; do
ln -s /usr/bin/true "$main_bin/$command_name" ln -s /usr/bin/true "$main_bin/$command_name"
done done
ln -s /usr/bin/false "$main_bin/wget" ln -s /usr/bin/false "$main_bin/wget"
cp "$ROOT/README.md" "$main_home/.zshrc" cp "$ROOT/README.md" "$main_home/.zshrc"
printf '%s\n' 'existing-zshenv' >"$main_home/.zshenv"
if HOME="$main_home" PATH="$main_bin:/usr/bin:/bin" /bin/bash "$ROOT/install.sh" --non-interactive >/dev/null 2>&1; then if HOME="$main_home" PATH="$main_bin:/usr/bin:/bin" /bin/bash "$ROOT/install.sh" --non-interactive >/dev/null 2>&1; then
fail "install.sh returned success after the zshrc download failed" fail "install.sh returned success after the zshrc download failed"
fi fi
cmp -s "$ROOT/README.md" "$main_home/.zshrc" || cmp -s "$ROOT/README.md" "$main_home/.zshrc" ||
fail "install.sh replaced .zshrc after its download failed" fail "install.sh replaced .zshrc after its download failed"
printf '%s\n' 'existing-zshenv' | cmp -s - "$main_home/.zshenv" ||
fail "install.sh replaced .zshenv after a download failed"
fnm_home="$TEMP_DIR/fnm-home" fnm_home="$TEMP_DIR/fnm-home"
mkdir -p "$fnm_home/.local/share/fnm" mkdir -p "$fnm_home/.local/share/fnm"
@@ -65,7 +68,7 @@ for script in "$ROOT"/*.sh; do
bash -n "$script" bash -n "$script"
done done
for config in "$ROOT/.zshrc" "$ROOT"/zshrc/*.zsh; do for config in "$ROOT/.zshenv" "$ROOT/.zshrc" "$ROOT"/zshrc/*.zsh; do
zsh -n "$config" zsh -n "$config"
done done
@@ -73,10 +76,21 @@ grep -q 'NVM_VERSION="v0.40.4"' "$ROOT/install_nvm.sh" || fail "nvm version is s
grep -q 'apt-get install -y unzip' "$ROOT/install_fnm.sh" || fail "Ubuntu unzip installation is missing" grep -q 'apt-get install -y unzip' "$ROOT/install_fnm.sh" || fail "Ubuntu unzip installation is missing"
grep -q 'tail -1 || true' "$ROOT/install_merlin_worker.sh" || fail "a missing PYTHONPATH remains fatal" grep -q 'tail -1 || true' "$ROOT/install_merlin_worker.sh" || fail "a missing PYTHONPATH remains fatal"
grep -Fq 'mktemp "$HOME/.zshrc.download.' "$ROOT/install.sh" || fail ".zshrc is not downloaded on its target filesystem" grep -Fq 'mktemp "$HOME/.zshrc.download.' "$ROOT/install.sh" || fail ".zshrc is not downloaded on its target filesystem"
grep -Fq 'mktemp "$HOME/.zshenv.download.' "$ROOT/install.sh" || fail ".zshenv is not downloaded on its target filesystem"
grep -Fq 'mktemp "$HOME/.config/.easyzsh-' "$ROOT/patch.sh" || fail "patch downloads can be sourced while incomplete" grep -Fq 'mktemp "$HOME/.config/.easyzsh-' "$ROOT/patch.sh" || fail "patch downloads can be sourced while incomplete"
grep -Fq '"$ZSH_CONFIGS_DIR"/*(DN)' "$ROOT/.zshrc" || fail "existing extensionless Zsh configs are ignored" grep -Fq '"$ZSH_CONFIGS_DIR"/*(DN)' "$ROOT/.zshrc" || fail "existing extensionless Zsh configs are ignored"
grep -Fq "alias rm='/usr/bin/trash'" "$ROOT/.zshrc" || fail "macOS rm does not use the native Trash"
grep -Fq "alias rm='gio trash'" "$ROOT/.zshrc" || fail "Linux rm does not use the desktop Trash"
grep -Fq 'libglib2.0-bin' "$ROOT/install.sh" || fail "Ubuntu Trash support is not installed"
grep -Fq 'cp -Pp "$HOME/.zshrc"' "$ROOT/install.sh" || fail ".zshrc symlinks are not preserved in backups" grep -Fq 'cp -Pp "$HOME/.zshrc"' "$ROOT/install.sh" || fail ".zshrc symlinks are not preserved in backups"
grep -Fq '[ -L "$HOME/.zshrc" ]' "$ROOT/install.sh" || fail "broken .zshrc symlinks are not backed up" grep -Fq '[ -L "$HOME/.zshrc" ]' "$ROOT/install.sh" || fail "broken .zshrc symlinks are not backed up"
grep -Fq 'cp -Pp "$HOME/.zshenv"' "$ROOT/install.sh" || fail ".zshenv symlinks are not preserved in backups"
grep -Fq '[ -L "$HOME/.zshenv" ]' "$ROOT/install.sh" || fail "broken .zshenv symlinks are not backed up"
grep -Fq 'brew shellenv' "$ROOT/.zshenv" || fail "Homebrew is not initialized in .zshenv"
grep -Fq '/usr/local/bin/brew' "$ROOT/.zshenv" || fail "Intel Homebrew path is missing from .zshenv"
if grep -Fq 'brew shellenv' "$ROOT/.zshrc"; then
fail "Homebrew still initializes from interactive-only .zshrc"
fi
[[ ! -e "$ROOT/install/fnm.sh" ]] || fail "vendored fnm installer still exists" [[ ! -e "$ROOT/install/fnm.sh" ]] || fail "vendored fnm installer still exists"
if grep -Eq '^[[:space:]]*export MLX_USER_TOKEN=' "$ROOT"/zshrc/*.zsh; then if grep -Eq '^[[:space:]]*export MLX_USER_TOKEN=' "$ROOT"/zshrc/*.zsh; then
fail "a static MLX user token remains" fail "a static MLX user token remains"