direnv — Automatic Environment Switcher for Your Shell
direnv loads and unloads environment variables when you cd into a directory. Per-project PATH, API keys, and language versions — applied automatically when you enter, cleaned up when you leave. The foundation of reproducible dev environments.
What it is
direnv is a shell extension that loads and unloads environment variables when you cd into a directory. You create an .envrc file in your project root, and direnv automatically sets PATH, API keys, language versions, and any other environment variables when you enter that directory. When you leave, it cleans everything up.
direnv targets developers working on multiple projects that each need different environment configurations. Instead of remembering to source scripts or switch version managers, direnv handles it transparently.
Why it saves time or tokens
Manually managing environment variables across projects leads to 'works on my machine' bugs and forgotten configuration steps. direnv makes environments self-documenting and automatic. The .envrc file serves as both documentation and execution. For onboarding new developers or AI agents to a project, a well-configured .envrc means the environment is correct the moment you enter the directory.
How to use
- Install direnv:
brew install direnv(macOS) or your OS package manager - Hook direnv into your shell: add
eval "$(direnv hook zsh)"to your.zshrc - Create an
.envrcfile in your project and rundirenv allowto trust it
Example
# .envrc
export DATABASE_URL='postgres://localhost:5432/myapp'
export API_KEY='dev-key-12345'
export NODE_ENV='development'
# Use a specific Node version via nvm
use node 20
# Add local bin to PATH
PATH_add bin
# Load from .env file
dotenv
| Feature | Description |
|---|---|
| Auto-load | Variables set on directory entry |
| Auto-unload | Variables removed on directory exit |
| Security | Requires explicit direnv allow |
| .env support | Load existing .env files |
| Language support | bash, zsh, fish, tcsh, elvish |
Related on TokRepo
- AI tools for coding — developer environment tools on TokRepo
- AI tools for devops — environment and configuration management
Common pitfalls
- Forgetting to run
direnv allowafter editing.envrcmeans changes do not take effect; direnv blocks untrusted changes as a security measure - Committing
.envrcwith real secrets exposes them in version control; use.envrcfor non-sensitive config and reference a.envfile (gitignored) for secrets - Some shell integrations conflict with other version managers (nvm, pyenv); check the direnv wiki for compatibility notes
Frequently Asked Questions
direnv requires you to explicitly run direnv allow for each .envrc file before it executes. If the file changes, you must re-allow it. This prevents malicious .envrc files from executing automatically. The allow list is stored in a local database, not in the repository.
Yes. Use the dotenv command in your .envrc to load variables from a .env file. This lets you keep secrets in a gitignored .env file while using direnv for automatic loading. The .envrc itself can be committed safely because it just references the .env file.
Yes. direnv supports use commands for several language version managers. 'use node 20' activates Node 20 via nvm, 'use python 3.11' activates Python via pyenv, and similar commands exist for Go, Ruby, and other languages. This replaces manual version switching.
direnv can work in CI, but most CI systems use their own environment variable mechanisms. If your CI runs shell scripts that cd into directories, direnv hooks work normally. However, many teams prefer to set CI environment variables through the CI platform's native configuration.
dotenv libraries load .env files at application runtime within a specific language (Node.js, Python). direnv loads environment variables at the shell level before your application starts, making them available to any tool or language. direnv is language-agnostic and works for all commands you run in that directory.
Citations (3)
- direnv GitHub— direnv loads and unloads environment variables based on directory
- direnv Docs— direnv configuration and shell hooks
- 12 Factor App— Environment variable best practices for development
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.