☼
⌃
☰

Direnv

Table of Contents

Shell extension that automatically loads and unloads environment variables when you change into or out of a directory. Keeps .bashrc and .zshrc clean, enables per-project environment isolation, and works with any language or toolchain. The .envrc file in a project directory replaces manual export commands run before starting work.

Related: Shell Bash Scripting

Installation

SHELL
# Nix (recommended)
nix-env -i direnv

# macOS
brew install direnv

# Ubuntu / Debian
sudo apt install direnv

# Hook into shell — add to the END of shell rc file
# Bash (~/.bashrc)
eval "$(direnv hook bash)"

# Zsh (~/.zshrc)
eval "$(direnv hook zsh)"

# Fish (~/.config/fish/config.fish)
direnv hook fish | source

Core Workflow

SHELL
# Create an .envrc in your project
echo 'export FOO=bar' > .envrc

# First time — must explicitly allow the file (security model)
direnv allow

# Now entering the directory loads the vars automatically
cd /path/to/project     # → direnv: loading .envrc
cd ..                   # → direnv: unloading

# Allow again after any edit to .envrc
direnv allow

# Block a previously allowed directory
direnv deny

# Show current env diff
direnv status

.envrc Reference

Standard shell script — any valid shell command works.

Environment Variables

SHELL
export DATABASE_URL="postgres://localhost:5432/mydb"
export API_KEY="dev-key-not-for-production"
export DEBUG=true

Standard Library Functions

Direnv ships with helper functions that are safer than raw exports:

SHELL
# Add a directory to PATH (prepend)
PATH_add bin
PATH_add .venv/bin

# Load a .env file (key=value format, no export needed)
dotenv .env
dotenv_if_exists .env.local    # only loads if file exists

# Source another .envrc file
source_env ../shared/.envrc

# Load parent .envrc files up the directory tree
source_up

# Set a variable only if not already set
export PORT="${PORT:-8080}"

Python Virtual Environments

SHELL
# Auto-activate venv on cd
layout python3

# Or point to an existing venv
source .venv/bin/activate

Node.js

SHELL
# Add local node_modules/.bin to PATH
PATH_add node_modules/.bin

# Use a specific Node version (with nvm)
use node 20

Inheritance

TEXT
monorepo/
  .envrc          ← shared config (DB URLs, shared secrets)
  service-a/
    .envrc        ← source_up + service-specific vars
  service-b/
    .envrc        ← source_up + service-specific vars
SHELL
# In service-a/.envrc
source_up               # loads monorepo/.envrc first
export SERVICE=service-a

Secrets Management

Do not commit secrets directly in .envrc — add .envrc to .gitignore if it contains sensitive values, or keep a committed .envrc.example with placeholder values.

SHELL
# .envrc — gitignored, contains real secrets
export API_KEY="real-secret"

# .envrc.example — committed, documents required vars
export API_KEY="your-api-key-here"

For team environments, integrate with a secrets manager:

SHELL
# 1Password CLI
export SECRET=$(op read "op://vault/item/field")

# Doppler
eval "$(doppler secrets download --no-file --format docker)"

Tips

  • Always run direnv allow after editing .envrc — direnv blocks the file after any change until re-authorised
  • Use dotenv_if_exists .env.local rather than dotenv .env when the file may not always be present — prevents errors on fresh checkouts
  • direnv status shows what is currently loaded and what would change — useful for debugging missing variables
  • The security model (explicit allow per-file) means you are safe to cd into untrusted directories without loading their .envrc — direnv will warn you it blocked it
  • Put the hook at the very end of your shell rc file — initialisation order matters and placing it early can cause subtle issues