Tired of manually exporting API keys, database URLs, or feature flags each time you switch projects? By using direnv .envrc to automatically load project‑specific environment variables, you keep secrets out of your shell history and let your dev environment ready itself the moment you cd into a folder.
Why project‑specific environment variables matter
Environment variables are the de‑facto way to configure applications without hard‑coding values. When each project needs its own set of vars—different database credentials, third‑party API keys, or feature flags—manually exporting them leads to mistakes, duplicated effort, and potential leaks. direnv solves this by loading the right set automatically whenever you enter a project directory.
How direnv .envrc automatically loads environment variables
Direnv is a shell extension that watches for a file named .envrc in the current directory. When you change directories, direnv evaluates the .envrc file, exports any variables it defines, and removes them when you leave. This creates a clean, per‑project environment without cluttering your global shell.
Understanding the shell hook
After installation, direnv adds a hook to your shell’s PROMPT_COMMAND (bash) or precmd (zsh/fish). The hook runs direnv export each time the prompt is displayed, checking for a new or modified .envrc. If the file exists and is allowed, direnv loads its contents into the environment.
Installing direnv
- On macOS with Homebrew:
brew install direnv - On Ubuntu/Debian:
sudo apt-get install direnv - On Fedora:
sudo dnf install direnv - Verify installation:
direnv version - Hook direnv into your shell:
- Bash: add
eval "$(direnv hook bash)"to~/.bashrc - Zsh: add
eval "$(direnv hook zsh)"to~/.zshrc - Fish: add
direnv hook fish | sourceto~/.config/fish/config.fish
- Bash: add
- Reload your shell or open a new terminal.
Creating and using a .envrc file
- Navigate to your project root:
cd my-project - Create the file:
touch .envrc - Add variable exports, for example:
export DATABASE_URL='postgres://localhost/mydb' export API_KEY='s3cr3t' export NODE_ENV='development'
- Save the file.
- The first time you enter the directory, direnv will warn that the .envrc is blocked. Allow it with:
direnv allow - Direnv now automatically loads the variables on each
cdinto the folder and unloads them when you leave.
Security considerations and best practices
- Never commit
.envrcto version control; add it to.gitignore. - Use direnv’s built‑in
dotenvstdlib to load a separate.envfile that you do keep ignored. - Restrict
direnv allowto trusted projects; review any .envrc before allowing. - Consider using
direnv editto open the file in your editor safely. - Rotate secrets regularly and audit which projects have active envrcs.
Integrating direnv with your shell (bash, zsh, fish)
Once the hook is added (see installation step), direnv works transparently. However, a few tips improve the experience:
- Enable lazy loading for large projects by adding
export DIRENV_LOG_FORMAT=to suppress verbose logs. - Use
direnv statusto see which variables are currently loaded. - If you use a version manager like
asdfornvm, place the direnv hook after those initializations so PATH modifications apply correctly. - For CI environments, you can source
$(direnv export bash)in your CI script to replicate the local environment.
Advanced tips: loading .env files, using stdlib, and more
- Load a
.envfile with the stdlib: addsource_upordotenvto your.envrc:
# .envrc
dotenv
export DATABASE_URL='postgres://localhost/mydb'
- Use
source_upto inherit variables from a parent directory’s .envrc. - Combine with
layoutfunctions (e.g.,layout node) to automatically runnpm installorpip install -r requirements.txtwhen entering a project. - Create project‑specific aliases or functions inside .envrc to streamline repetitive tasks.
- Direnv also supports
exportandunsetfor temporary overrides.
Conclusion
By leveraging direnv .envrc to automatically load project‑specific environment variables, you eliminate manual export steps, reduce the risk of leaking secrets, and keep each project’s configuration isolated and reproducible. Start today: install direnv, add a simple .envrc, run direnv allow, and watch your workflow become seamless.