fix: hardcode pi binary path, set XDG env for npm

npm prefix discovery fails in systemd and activation contexts
because NPM_CONFIG_USERCONFIG and XDG_DATA_HOME are not set.
Hardcode the known path ~/.local/share/npm/bin/pi instead.
This commit is contained in:
Harivansh Rathi 2026-04-03 01:43:30 +00:00
parent 345c38bfc9
commit 4860769776
2 changed files with 19 additions and 16 deletions

View file

@ -1,9 +1,14 @@
{
config,
lib,
pkgs,
hostConfig,
...
}:
let
npmDir = "${config.xdg.dataHome}/npm";
piBin = "${npmDir}/bin/pi";
in
lib.mkIf hostConfig.isLinux {
# Install pi-coding-agent globally via npm at activation time.
home.activation.installPiAgent = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
@ -13,11 +18,11 @@ lib.mkIf hostConfig.isLinux {
pkgs.coreutils
]
}:$PATH"
export NPM_CONFIG_USERCONFIG="${config.xdg.configHome}/npm/npmrc"
export XDG_DATA_HOME="${config.xdg.dataHome}"
export XDG_CACHE_HOME="${config.xdg.cacheHome}"
npm_prefix="$(npm prefix -g 2>/dev/null)"
pkg_dir="$npm_prefix/lib/node_modules/@mariozechner/pi-coding-agent"
if [ ! -d "$pkg_dir" ]; then
if [ ! -d "${npmDir}/lib/node_modules/@mariozechner/pi-coding-agent" ]; then
npm install -g @mariozechner/pi-coding-agent 2>/dev/null || true
fi
'';
@ -34,13 +39,13 @@ lib.mkIf hostConfig.isLinux {
pkgs.git
]
}:$PATH"
export NPM_CONFIG_USERCONFIG="${config.xdg.configHome}/npm/npmrc"
export XDG_DATA_HOME="${config.xdg.dataHome}"
export XDG_CACHE_HOME="${config.xdg.cacheHome}"
npm_prefix="$(npm prefix -g 2>/dev/null)"
pi_bin="$npm_prefix/bin/pi"
if [ -x "$pi_bin" ]; then
if [ -x "${piBin}" ]; then
for pkg in "@e9n/pi-channels" "pi-schedule-prompt" "pi-subagents"; do
"$pi_bin" install "npm:$pkg" 2>/dev/null || true
"${piBin}" install "npm:$pkg" 2>/dev/null || true
done
fi
'';

View file

@ -9,23 +9,21 @@ let
[ -f "${piAgentEnvFile}" ]
'';
piBin = "/home/${username}/.local/share/npm/bin/pi";
# Wrapper that exec's pi inside tmux's foreground process so systemd
# tracks the actual PID. When pi dies, tmux exits, systemd sees it
# and triggers Restart=on-failure.
piAgentStart = pkgs.writeShellScript "start-pi-agent" ''
export PATH="${pkgs.nodejs_22}/bin:$PATH"
npm_prefix="$(npm prefix -g 2>/dev/null)"
pi_bin="$npm_prefix/bin/pi"
if [ ! -x "$pi_bin" ]; then
echo "pi binary not found at $pi_bin" >&2
if [ ! -x "${piBin}" ]; then
echo "pi binary not found at ${piBin}" >&2
exit 1
fi
# tmux runs in the foreground (-D) so systemd tracks this process.
# The inner shell exec's pi so the tmux pane PID *is* the pi PID.
exec ${pkgs.tmux}/bin/tmux new-session -D -s pi-agent \
"exec $pi_bin --chat-bridge"
"exec ${piBin} --chat-bridge"
'';
in
{