direnv
direnv は directory に入ると環境変数を自動で load し、離れると unload する。project ごとに独立した環境を持てる。
project root に .envrc を作成:
# 環境変数を設定export DATABASE_URL="postgresql://localhost/mydb"export API_KEY="development-key"
# .env があれば loaddotenv_if_exists .env
# project の bin を PATH に追加PATH_add ./bin
# mise の言語 version を使うuse mise初回 (と変更後) は approve が必要:
direnv allowmise integration
Section titled “mise integration”mise と組み合わせて言語 runtime を自動で切り替える:
use miseversion は .mise.toml で指定:
[tools]node = "20.10.0"python = "3.12"go = "1.21"cd するだけで正しい version が activate される。
よくある pattern
Section titled “よくある pattern”Database credentials
Section titled “Database credentials”export DATABASE_URL="postgresql://localhost/dev_db"export REDIS_URL="redis://localhost:6379"AWS profile
Section titled “AWS profile”export AWS_PROFILE="development"export AWS_REGION="us-west-2"Project PATH
Section titled “Project PATH”PATH_add ./scriptsPATH_add ./node_modules/.binSecrets
Section titled “Secrets”# .envrc (committed)dotenv_if_exists .env.local
# .env.local (never committed)OPENAI_API_KEY="sk-..."STRIPE_SECRET_KEY="sk_test_..."Security
Section titled “Security”.envrc.localは machine 固有の secret 用。必ず gitignore する.envrcに本物の secret を置かない。placeholder を使うdirenv allowは明示的な trust mechanism- PR で
.envrcの変更は慎重に review する
direnv + wtp workflow
Section titled “direnv + wtp workflow”worktree ごとに独立した環境を持たせる:
# feature worktree を作成wtp add feature/new-apicd ../project-feature-new-api
# この worktree 用の環境を setupcat > .envrc << EOFexport FEATURE_FLAG_NEW_API=trueexport API_VERSION=v2use miseEOF
direnv allow
# main worktree は影響を受けない