LinuxCommandLibrary

direnv

Load environment variables based on directory

TLDR

Grant direnv permission to load the .envrc present in the current directory

$ direnv allow [.]
copy

Revoke the authorization to load the .envrc present in the current directory
$ direnv deny [.]
copy

Edit the .envrc file in the default text editor and reload the environment on exit
$ direnv edit [.]
copy

Trigger a reload of the environment
$ direnv reload
copy

Print some debug status information
$ direnv status
copy

SYNOPSIS

direnv COMMAND [options] [args...]
Load/unload env vars via .envrc per directory.

PARAMETERS

allow
    Grant permission to load current .envrc

deny
    Revoke permission and unload .envrc

edit
    Open .envrc in $EDITOR safely

reload
    Reload environment from current .envrc

status
    Show if direnv is active and details

watch [file]...
    Watch files/dirs for auto-reload

exec dir cmd [args]...
    Load env from dir, run cmd

--help
    Show help for command

--version
    Print direnv version

--verbose
    Enable debug output

DESCRIPTION

Direnv is a battery-included shell extension that manages project-specific environment variables. It automatically loads and unloads environment variables when you cd into a directory containing a .envrc file.

Each .envrc contains shell commands (e.g., export PATH=./bin:$PATH, use python) that modify the environment safely. Direnv executes trusted .envrc files on directory entry and restores the previous state on exit, preventing global pollution.

Key features include a permission system (direnv allow/deny) to review code before execution, support for watchers to reload on file changes, and standards like layout functions for common tools (e.g., Ruby, Node, Python virtualenvs).

It's lightweight, secure (sandboxed eval), and hooks into shells like bash, zsh, fish. Ideal for developers switching projects without manual source commands. Widely used in open-source and enterprise for reproducible environments.

CAVEATS

Requires shell hook setup (e.g., eval "$(direnv hook bash)" in .bashrc). Untrusted .envrc prompts for review to avoid malicious code. May slow cd in deeply nested dirs with many watchers. Not active without hook.

SHELL INTEGRATION

Add to shell rc:
eval "$(direnv hook $SHELL)" for bash/zsh/fish.

EXAMPLE .ENVRC

export DATABASE_URL=postgresql://localhost/myapp
PATH_add ./bin
use flake

Review with direnv allow.

HISTORY

Created by Denis Knauf in 2012 as a successor to autoenv. Gained popularity via GitHub (direnv/direnv). Major v2.0 in 2019 added layouts and better security. Actively maintained, used by 100k+ devs for modern workflows.

SEE ALSO

env(1), bash(1), zsh(1)

Copied to clipboard