direnv
$ direnv 社区开源 中风险在 Shell 进入或离开目录时自动加载与卸载项目级 .envrc 环境变量。提供 direnv status --json、direnv export json 与适用于无交互 Shell 的 direnv exec,同时默认阻止未经显式授权的 .envrc 执行。
brew install direnv 纯本地 Shell 环境钩子工具;Agent 应直接在工作区调用 direnv exec 或 direnv export json。
Agent 友好度体检单
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
提供 direnv status --json、direnv export json 与 direnv exec,适配未挂载钩子的子 Agent Shell。
direnv status --jsonanddirenv export jsonreturn JSONdirenv exec <dir> <cmd>works without shell hook setup
显式 SHA-256 授权白名单默认拦截未信任的 .envrc,但一旦授权,.envrc 可执行任意 bash 脚本。
- Untrusted
.envrcchanges are blocked untildirenv allow
单 Go 二进制,可通过 Homebrew 与系统包管理器在 macOS 和 Linux 安装(无原生 Windows 支持)。
- macOS and Linux only; no native Windows shell support
优雅统一各仓库的 PATH、Nix use flake 与 .env 注入。
- Integrates cleanly with Nix, Python venvs, and
.envfiles
长期稳定维护的 MIT 开源社区项目。
- Active
direnv/direnvrepository
具备完善的联机手册页(direnv.1、direnv-stdlib.1)与官网文档。
- Complete manual pages at direnv.net
安全边界、鉴权与放权指南
direnv 本身不存储远程凭证;.envrc 信任哈希记录在 ~/.local/share/direnv/allow/。
MIT 开源免费 Go 二进制。
# direnv Agent Rules - Never run `direnv allow` blindly on an uninspected `.envrc` file (`.envrc` executes arbitrary bash). - Use `direnv exec <dir> <cmd>` in non-interactive shells where `direnv hook` is not active.
Agent 高频安全命令配方
只读查询(安全推荐)
direnv status --json
以 JSON 报告当前生效的 .envrc 路径、信任状态与加载情况。
direnv export json
计算并以 JSON 打印当前目录新增或修改的环境变量。
受控写入与预演
direnv exec . env
无需依赖交互式 Shell 钩子,单次应用已授权的 .envrc 运行命令。
核心优势
- +
direnv status --json与direnv export json让环境状态可被 Agent 精确检查。 - +
direnv exec允许子 Agent 在未加载 Shell 钩子时依然使用.envrc环境。 - + 内置
allow哈希校验阻止未经审查的.envrc自动执行。
注意事项与踩坑点
- ! 除 WSL 外不支持原生 Windows。
- !
.envrc本质上是可执行 bash 脚本,Agent 执行direnv allow前必须先审查其内容。