direnv
direnv loads and unloads environment variables automatically when you enter and leave directories. Each project gets its own isolated environment.
Basics
Section titled “Basics”Create .envrc in your project root:
# Set env varsexport DATABASE_URL="postgresql://localhost/mydb"export API_KEY="development-key"
# Load .env if it existsdotenv_if_exists .env
# Add project bin to PATHPATH_add ./bin
# Use language versions from miseuse miseYou need to approve it the first time (and after any change):
direnv allowmise integration
Section titled “mise integration”Combine with mise for automatic language runtime switching:
use miseSpecify versions in .mise.toml:
[tools]node = "20.10.0"python = "3.12"go = "1.21"Just cd into the directory and the right versions activate.
Common patterns
Section titled “Common patterns”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”- Always gitignore
.envrc.localfor machine-specific secrets - Never put real secrets in
.envrc— use placeholders direnv allowis an explicit trust mechanism- Review
.envrcchanges carefully in PRs
direnv + wtp workflow
Section titled “direnv + wtp workflow”Give each worktree its own isolated environment:
# Create a feature worktreewtp add feature/new-apicd ../project-feature-new-api
# Set up environment for this worktreecat > .envrc << EOFexport FEATURE_FLAG_NEW_API=trueexport API_VERSION=v2use miseEOF
direnv allow
# Main worktree stays unaffected