@aeondave/opencode-dotenv
v0.1.0
Published
Load a .env file next to your OpenCode config into the process and substitute {env:VAR} / ${VAR} placeholders across config files. Cross-platform, zero runtime dependencies.
Downloads
104
Maintainers
Readme
opencode-dot-env
Load a
.envnext to your OpenCode config and resolve{env:VAR}placeholders — no shell wrapper, no profile alias.
A plugin for OpenCode that reads a .env file from your config directory into the running process, so {env:VAR} placeholders in opencode.json, MCP definitions, provider options, and other plugins resolve automatically. Cross-platform (Linux, macOS, Windows) with zero runtime dependencies.
Repository: AeonDave/opencode-dot-env
Why This Exists
OpenCode resolves {env:VAR} from the process environment at startup. Without help, that means exporting every secret beforehand — a shell wrapper script, a profile alias, or system-wide environment variables. That is fragile and easy to forget.
This plugin removes the wrapper: drop a .env next to opencode.json and the variables are loaded for you, on every OS, every launch.
- One
.env, everywhere — Keep values likeAPI_KEY,ACCESS_TOKEN, and other secrets in a single gitignored file beside your config. - Placeholders just work —
{env:VAR}and${VAR}in config files resolve from the loaded.env. - Shells inherit it too — Loaded variables are injected into the
bashtool and terminal sessions.
How It Works
The plugin runs at three points, earliest first:
- Plugin init — before MCP servers spawn and providers initialize, it parses the
.envand populatesprocess.env. This is what makes OpenCode's own{env:VAR}resolution succeed. confighook — rewrites any{env:VAR}/${VAR}literals that survived into the merged config object (coversopencode.json,dcp.jsonc, and other plugins' config).shell.envhook — injects the loaded variables into shell executions (bash tool + user terminals).
Unknown variables are left untouched, so a typo never silently blanks a value.
.env Resolution
The plugin looks for a .env in your OpenCode config directory, resolved in this order:
OPENCODE_DOTENV_DIR— explicit directory to search.OPENCODE_CONFIG— explicit config file; its directory is used.XDG_CONFIG_HOME/opencode.~/.config/opencode(OpenCode's default on every OS, including Windows).
An optional OPENCODE_DOTENV_PATH points at a specific .env file, which is loaded last and wins.
Supported .env syntax
# comments and blank lines are ignored
export FOO=bar
API_KEY="example-key"
QUOTED='single quoted'
MULTILINE="line1\nline2" # \n \t \r \\ \" unescaped inside double quotesInstallation
From npm (recommended)
Add the package to the plugin array in your OpenCode config at ~/.config/opencode/opencode.json:
{
"plugin": ["@aeondave/opencode-dotenv@latest"]
}OpenCode installs the plugin automatically on the next start. To pin a version, replace @latest with a specific version (e.g. @0.1.0).
From a local clone (shim)
Run from a local checkout — useful before publishing or while hacking on the plugin:
Clone the repository and install dependencies:
git clone https://github.com/AeonDave/opencode-dot-env.git cd opencode-dot-env npm installCreate a shim file in your global plugin directory that re-exports the checkout's entry point. The directory is
plugin(singular):- Path:
~/.config/opencode/plugin/dotenv.ts - Content — a single line pointing at the absolute path of the cloned entry point:
export { default } from "/absolute/path/to/opencode-dot-env/src/plugin/dotenv.ts"On Windows, use forward slashes and include the drive letter, for example:
export { default } from "C:/path/to/opencode-dot-env/src/plugin/dotenv.ts"- Path:
Restart OpenCode. The plugin loads from your working tree, so edits to
src/take effect on the next restart. Delete the shim file to uninstall.
Use one method at a time. If you add the npm entry, remove the local shim (and vice versa) so the plugin is not loaded twice.
Usage
Put a
.envnext to your config:# ~/.config/opencode/.env API_KEY=your-api-key ACCESS_TOKEN=your-access-tokenReference the variables in your config as usual:
// Example excerpt { "somePlugin": { "apiKey": "{env:API_KEY}", "accessToken": "${ACCESS_TOKEN}" } }Start OpenCode normally — no wrapper script, no alias.
Add
.envto your.gitignore. Never commit real secrets.
Configuration
| Environment variable | Default | Effect |
|----------------------|---------|--------|
| OPENCODE_DOTENV_DIR | unset | Directory to search for .env (overrides config-dir detection). |
| OPENCODE_DOTENV_PATH | unset | Path to an extra .env file, loaded last (highest priority). |
| OPENCODE_DOTENV_OVERRIDE | 0 | When truthy (1/true/yes/on), .env values replace existing process variables. By default existing variables are kept. |
| OPENCODE_DOTENV_SILENT | 0 | When truthy, suppress info/debug logs (warnings still shown). |
Development
npm install # install dev dependencies
npm run typecheckDisclaimer
This project is not built by the OpenCode team and is not affiliated with OpenCode.
License
MIT
