Skip to content

fix(zsh): keep Nix profiles ahead of /usr/bin and Homebrew in PATH - #309

Merged
furedea merged 4 commits into
mainfrom
fix/path-order
Oct 23, 2026
Merged

furedea merged 4 commits into
mainfrom
fix/path-order

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

概要

シェルの PATH で /usr/bin や /opt/homebrew/bin が nix のプロファイルより前に来ていた原因を特定し,直した.

原因と証拠

  • env -i HOME=… zsh -lic 'print -l $path' では,先頭が ~/.local/bin,/Library/TeX/texbin,/opt/homebrew/bin,/usr/bin で,その後ろに ~/.nix-profile/bin,/etc/profiles/per-user/kaito/bin,/run/current-system/sw/bin が並んでいた.
  • zsh -lc(非対話)では nix のプロファイルが先頭側に来る.つまり nix-darwin の /etc/zshenv(set-environment)は正しい順序を作っており,その上から zsh/.zshrc が壊していた.
  • 犯人は zsh/.zshrc の export PATH="/usr/bin:$PATH",export PATH="/opt/homebrew/bin:$PATH",export PATH="/Library/TeX/texbin:$PATH",export PATH="$HOME/.local/bin:$PATH".
  • zsh/.zshenv(gem,cargo)と zsh/.zprofile(~/.local/bin)も先頭に足しており,同じ問題があった.
  • Homebrew の shellenv,macOS の path_helper,/etc/paths,/etc/paths.d は関係しない(nix-darwin が生成する /etc/zprofile は空で,path_helper を呼ばない).
  • Claude や Herdr のセッションは,この PATH を持つシェルから起動した環境を継承するため,同じ順序になる.

変更

  • 先頭への PATH 追加(root cause)を .zshrc,.zprofile,.zshenv から削除.
  • .zshenv は PATH の順序を強制しない.devShell や direnv 内の zsh でも,継承した順序(devShell のツールが先頭)を保つ.typeset -U path PATH で重複を除く.
  • 足りないディレクトリだけを,最初のシステムエントリ(/usr/local/bin,/usr/bin,/bin,/usr/sbin,/sbin のうち最初のもの)の直前に挿入する.順序は ~/.local/bin,~/.cargo/bin,~/.gem/bin(.zshenv),/opt/homebrew/bin,/Library/TeX/texbin(.zshrc).すでにあるエントリは動かさない.nix と devShell が先頭,これらが macOS 標準より前になる.
  • 既に壊れた順序を継承しているセッションは,switch 後にターミナルや Claude を再起動すれば直る.
  • tests/zsh/startup.bats:クリーンなログインシェルの順序(nix → 上記ディレクトリ → /usr/bin),devShell が先頭の PATH がそのまま保たれること(-c,-ic,-lic),挿入位置(システムエントリの直前,なければ末尾)を完全一致で検証.

前後の比較

前 後
PATH の順(クリーンなログイン) ~/.local/bin,texbin,/opt/homebrew/bin,/usr/bin,…,nix プロファイル,…,/usr/bin nix プロファイル,~/.local/bin,~/.cargo/bin,~/.gem/bin,/opt/homebrew/bin,texbin,/usr/bin,…
重複 あり(/usr/bin,~/.local/bin など) なし
command -v git /usr/bin/git nix の git

受け入れ条件

  • switch のあと,ターミナルと Claude を再起動し,新しいターミナルで print -l $path を実行して,nix のプロファイルが /usr/bin と /opt/homebrew/bin より前にあり,重複がない
  • 新しい Claude セッションでも同様(echo $PATH)
  • command -v git が nix の git を返す

Closes #308

🤖 Generated with Claude Code

furedea and others added 4 commits October 23, 2026 19:11
.zshrc prepended /usr/bin, /opt/homebrew/bin, /Library/TeX/texbin and
~/.local/bin, so they outranked the Nix profiles and git resolved to the
Xcode shim. Append them after the Nix entries and de-duplicate PATH.

Closes #308

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Move Nix profiles and user bin directories ahead of /usr/bin even when
the inherited PATH is already wrong, and keep Homebrew and TeX after it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
.zshenv runs inside devShell and direnv shells, so rebuilding PATH there
would push their tools behind the global profiles. Only append missing
user directories and keep inherited order.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Insert missing user and Homebrew directories just before the first
system PATH entry so they stay behind Nix and devShell entries but ahead
of the macOS tools.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@furedea
furedea merged commit ced69a0 into main Oct 23, 2026
11 checks passed
@furedea
furedea deleted the fix/path-order branch October 23, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

シェルの PATH で /usr/bin が nix のプロファイルより前に来る原因を調べて直す

1 participant