LinuxCommandLibrary

direnv

per-directory environment variable manager

TLDR

Enable direnv in your shell (add to shell config)

$ eval "$(direnv hook bash)"
copy
Create a new .envrc file
$ direnv edit .
copy
Allow the current directory's .envrc
$ direnv allow
copy
Deny/revoke an .envrc
$ direnv deny
copy
Reload the current environment
$ direnv reload
copy
Export current environment as shell commands
$ direnv export bash
copy
Show environment diff when changing directories
$ direnv status
copy
Print the current .envrc being used
$ direnv show
copy

SYNOPSIS

direnv [command] [arguments]

DESCRIPTION

direnv is an environment switcher that loads and unloads environment variables based on the current directory. When you enter a directory containing an .envrc file, direnv automatically exports its variables into your shell. When you leave, it unloads them.
The .envrc file is a bash script that typically sets environment variables using export. Direnv provides a standard library of functions for common tasks like setting PATH, loading .env files, using nix-shell, or activating Python virtual environments.
For security, .envrc files must be explicitly allowed before they're executed. This prevents malicious code from running when entering untrusted directories. The allow command whitelists a file based on its content hash; any change requires re-approval.
Integration requires adding a shell hook to your configuration (e.g., ~/.bashrc). The hook modifies the prompt to show when direnv is active and handles automatic loading/unloading. Direnv supports bash, zsh, fish, tcsh, elvish, and other shells.
Common use cases include project-specific environment variables, automatic virtual environment activation, and development environment configuration.

PARAMETERS

allow [path]

Approve an .envrc file to be loaded. Security measure to prevent executing arbitrary code.
deny [path]
Revoke approval for an .envrc file.
edit [path]
Open .envrc in $EDITOR and allow it after editing.
reload
Manually reload the current directory's environment.
status
Show direnv's current status and active .envrc.
export shell
Export the current environment for the specified shell (bash, zsh, fish, etc.).
hook shell
Print the shell hook code to enable direnv.
show
Display the currently loaded .envrc file.
version
Print direnv version.
prune
Remove old allowed file entries.
fetchurl url [integrity]
Fetch a URL with caching and optional integrity check.
stdlib
Print the stdlib functions available in .envrc.

CONFIGURATION

.envrc

Per-directory environment configuration script loaded when entering the directory.
~/.config/direnv/direnv.toml
Global direnv configuration for whitelist and settings.
~/.config/direnv/direnvrc
Global shared functions and settings available to all .envrc files.

CAVEATS

Shell hook must be added to config for automatic operation. .envrc files must be explicitly allowed (security feature). Changes to .envrc require re-allowing. Some shell features may not work identically in .envrc. Large environments may cause slight delay when changing directories.

HISTORY

direnv was created by Jonas Pfenniger (zimbatm) around 2013 to solve the problem of managing per-project environment configurations. It was inspired by the need to switch between different development environments without manually sourcing files. The tool has become popular in the DevOps and development communities, especially with nix integration.

SEE ALSO

source(1), env(1), export(1), nix-shell(1), virtualenv(1)

> TERMINAL_GEAR

Curated for the Linux community

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

$1,589.00
New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

$54.00
KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

$16.99
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

$239.99
KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

$16.99
nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

$129.95