@aeondave/opencode-dotenvLoad 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.
2
83
近 7 天 34
32.5
生态多维模型
2 个月前
2026-06-17
快速安装与配置
opencode.json写入当前项目的 opencode.json,只对这个仓库生效。
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["@aeondave/opencode-dotenv@0.1.0"]
}写入 ~/.config/opencode/opencode.json,对所有项目生效。
~/.config/opencode/opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["@aeondave/opencode-dotenv@0.1.0"]
}若你要在本地改造这个插件,先装到项目里再从本地路径引用。
shell
pnpm add -D @aeondave/opencode-dotenvopencode 启动时会通过内嵌运行时自动加载 npm 依赖并缓存至本地目录,无需手动在全局环境执行安装。
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 quotes
Installation
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 plugin directory is
plugins(plural):- Path:
~/.config/opencode/plugins/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.
Directory name: current OpenCode scans
plugins(plural) —~/.config/opencode/plugins/and.opencode/plugins/. Some older builds used the singularplugin/. If the shim doesn't load, check your version (opencode --version) and try the other spelling.
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 typecheck
Disclaimer
This project is not built by the OpenCode team and is not affiliated with OpenCode.
License
MIT