Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions nix/home/zsh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,37 @@
text = ''
export NVM_DIR="$HOME/.nvm"

# nvm lazy load: node/npm/npx/nvm 初回実行時にロード (~700ms 短縮)
# デフォルト Node.js の PATH を即座に設定(claude 等の #!/usr/bin/env node 用)
# nvm 本体の初期化のみ遅延させる
if [ -d "$NVM_DIR/versions/node" ]; then
# 最新のインストール済みバージョンを使用
NODE_BIN=$(ls -d "$NVM_DIR/versions/node"/*/bin 2>/dev/null | sort -V | tail -1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor nvm default version when priming PATH

Selecting NODE_BIN via sort -V | tail -1 always chooses the highest installed Node, not the user's nvm alias default. In environments with multiple installed versions (for example default pinned to an LTS for project compatibility while a newer major is also installed), this makes node/npm and #!/usr/bin/env node tools run with the wrong runtime until nvm is explicitly invoked, which is a behavioral regression from the prior lazy-loader wrappers.

Useful? React with 👍 / 👎.

[ -d "$NODE_BIN" ] && export PATH="$NODE_BIN:$PATH"
fi

# nvm 本体の遅延読み込み(nvm コマンド初回実行時のみ)
_nvm_lazy_load() {
unset -f nvm node npm npx
unset -f nvm
local brew_prefix="''${HOMEBREW_PREFIX:-/opt/homebrew}"
local nvm_sh="$brew_prefix/opt/nvm/nvm.sh"
local nvm_comp="$brew_prefix/opt/nvm/etc/bash_completion.d/nvm"
if [ -s "$nvm_sh" ]; then
\. "$nvm_sh"
[ -s "$nvm_comp" ] && \. "$nvm_comp"
elif [ -s "''${NVM_DIR}/nvm.sh" ]; then
# フォールバック: 非Homebrew インストール ($NVM_DIR) を使用
\. "''${NVM_DIR}/nvm.sh"
else
echo "nvm: nvm.sh not found (checked: $nvm_sh, ''${NVM_DIR}/nvm.sh)" >&2
return 1
fi
}
nvm() { _nvm_lazy_load; nvm "$@"; }
node() { _nvm_lazy_load; node "$@"; }
npm() { _nvm_lazy_load; npm "$@"; }
npx() { _nvm_lazy_load; npx "$@"; }
nvm() { _nvm_lazy_load; nvm "$@"; }

export PNPM_HOME="$HOME/Library/pnpm"
case ":$PATH:" in
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac
'';
};

Expand Down
Loading