Skip to content

fix(env): stop claiming ~/.env; read from ~/.config/PAI/.env instead#1066

Open
MHoroszowski wants to merge 1 commit intodanielmiessler:mainfrom
MHoroszowski:fix/env-file-xdg-location
Open

fix(env): stop claiming ~/.env; read from ~/.config/PAI/.env instead#1066
MHoroszowski wants to merge 1 commit intodanielmiessler:mainfrom
MHoroszowski:fix/env-file-xdg-location

Conversation

@MHoroszowski
Copy link
Copy Markdown

Summary

PAI currently writes its managed secrets to `/.config/PAI/.env` (the XDG-compliant canonical location — correct choice) but then creates a symlink at `/.env` pointing at it. That symlink is a home-directory namespace grab: `/.env` is a conventional, user-owned file that many shells and tools look for. If a user installs any other tool that expects `/.env`, it either collides with PAI's secrets or silently overwrites PAI on the next install. The user loses control of a file they should own.

The cause is that `VoiceServer/server.ts` hardcodes `join(homedir(), '.env')` as the only place it looks for `ELEVENLABS_API_KEY`. The installer created the `~/.env` symlink purely to satisfy that hardcoded read — not as a considered design decision.

This PR removes the `/.env` symlink and teaches VoiceServer to read from the canonical XDG path directly, with `/.env` preserved as an optional user overlay for anyone who wants to put PAI-relevant keys there.

Changes

`VoiceServer/server.ts`

Refactored the env-loading block:

Before:
```ts
// Load .env from user home directory
const envPath = join(homedir(), '.env');
if (existsSync(envPath)) {
const envContent = await Bun.file(envPath).text();
envContent.split('\n').forEach(line => { /* parse */ });
}
```

After:
```ts
async function loadEnvFile(path: string): Promise { /* parse if exists */ }

const xdgEnvPath = join(homedir(), '.config', 'PAI', '.env');
const homeEnvPath = join(homedir(), '.env');

await loadEnvFile(xdgEnvPath); // PAI's managed secrets (canonical)
await loadEnvFile(homeEnvPath); // optional user overlay — wins on collisions
```

The error message when `ELEVENLABS_API_KEY` is missing now points at the canonical path rather than hardcoding `~/.env`.

`PAI-Install/engine/actions.ts`

Removed the creation of the `/.env` symlink. The `/.claude/.env` symlink is preserved — that path is PAI's own namespace, so the symlink is safe and hooks that already read it continue working unchanged.

Backward compatibility

This is fully backward-compatible with existing installs:

  1. Existing installs with the `~/.env` symlink already in place: Both paths resolve to the same real file. `loadEnvFile()` calls it twice; the second load overwrites with the same values. No-op, no breakage.
  2. Existing installs where a user previously replaced `~/.env` with a real file containing PAI values: `loadEnvFile()` reads both `/.config/PAI/.env` (if it exists) and `/.env`, so any of the three configurations (XDG only, home only, or both) keeps working.
  3. New installs after this lands: The installer writes secrets to `/.config/PAI/.env` and creates only the `/.claude/.env` symlink. `~/.env` is left untouched — the user owns it.
  4. Install re-runs (upgrades): The installer's existing safeguard at line 742 (`continue; // Don't overwrite a real file`) already refuses to clobber a non-symlink `/.env`, so users who reclaimed their `/.env` between installs are already safe. This PR simply extends that safety by not creating the symlink at all.

Test plan

  • Local reclaim test. Removed the `~/.env` symlink on my WSL2 install, replaced with an empty real file (mode 0600), restarted VoiceServer via systemd. VoiceServer started clean, `/health` reports `api_key_configured: true`, a live voice notification request succeeds end-to-end. This confirms the XDG-path load works.
  • Overlay test. Added a throwaway key to `~/.env` (e.g. `FOO=bar`) and confirmed `process.env.FOO` is visible inside VoiceServer's startup logs (via `--debug`), without breaking the XDG load.
  • Error message test. Temporarily renamed `~/.config/PAI/.env` to simulate a fresh install with no secrets yet. Confirmed the new error message correctly points at the XDG path.
  • Shell-escape / install.sh re-run test. Ran the installer path locally against a scratch directory; confirmed the new behavior: only `/.claude/.env` symlink is created, `/.env` is untouched.
  • macOS sanity check — would appreciate a maintainer running `bun run VoiceServer/server.ts` on a Mac to confirm the XDG path resolves correctly there too. macOS honors `$HOME/.config` the same way Linux does, so I'm confident but don't have a Mac to verify.

Why this matters

`/.env` is a conventional, user-owned file. A well-behaved application writes to its own scoped location (`/.config/PAI/` ✓) and teaches its dependencies to read from the right place. It should not reach into the home directory and colonize a generic file — especially not as an implicit side effect of one hardcoded line in one component.

PAI's installer already knows the right pattern (the XDG file is already the canonical location). This PR just finishes the job: makes the runtime read from the right place so the symlink shim isn't needed.

Related

PAI currently owns the user's ~/.env file via a symlink created during
install. This is a home-directory namespace grab: ~/.env is a conventional,
user-owned file that many shells and tools look for. If the user installs
any other tool that expects to read ~/.env, it either collides with PAI's
secrets or is silently overwritten on the next PAI install. This is the
wrong shape.

The cause is that VoiceServer hardcodes `join(homedir(), '.env')` as the
only place it looks for ELEVENLABS_API_KEY. The installer created the
~/.env symlink to make that hardcoded read resolve to the real secrets
file at ~/.config/PAI/.env (which is already the XDG-compliant, correct
canonical location).

This change:

- VoiceServer/server.ts: load ~/.config/PAI/.env first (XDG canonical
  location), then optionally overlay from ~/.env if the user has chosen
  to put PAI-relevant keys there. Values in ~/.env win on key collisions,
  preserving the "explicit user override" mental model. The error message
  when ELEVENLABS_API_KEY is missing now points at the canonical path.

- PAI-Install/engine/actions.ts: stop creating the ~/.env symlink.
  ~/.claude/.env stays symlinked (that path is PAI's own namespace, so
  the symlink is safe and the hooks' existing reads continue working
  unchanged). The new comment explains why we no longer touch ~/.env.

Backward compatibility:

- Existing installs that already have the ~/.env symlink continue to
  work — both paths point at the same real file, so loading both is a
  no-op.
- Existing installs that already have a real ~/.env with PAI values in
  it continue to work — loadEnvFile() reads both paths and either one
  (or both) can contain the keys.
- New installs after this change will have a real file at
  ~/.config/PAI/.env and will NOT touch ~/.env. Users own ~/.env.

Related to the VoiceServer cross-platform audio work in danielmiessler#1061 and the
Pushcut notification channel in danielmiessler#1062.
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.

1 participant