DIRENV(1)
π DIRENV(1) β the direnv manual, bestie π
manual name: direnv Β· section: DIRENV(1) Β· energy: unclutter your whole profile era β¨
β¨ NAME β¨
direnv β unclutter your .profile πΈ (thatβs literally the name, no cap).
π SYNOPSIS π
direnv command ...
π DESCRIPTION π
direnv is an environment variable manager for your shell, bestie. It knows how to hook into bash, zsh and fish shell to load or unload environment variables depending on your current directory. This allows you to have project-specific environment variables and not clutter the "~/.profile" file.
Before each prompt it checks for the existence of an .envrc file in the current and parent directories. If the file exists, it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to your current shell, while unset variables are removed.
Because direnv is compiled into a single static executable it is fast enough to be unnoticeable on each prompt. It is also language agnostic and can be used to build solutions similar to rbenv, pyenv, phpenv, ... π
π EXAMPLE π
$ cd ~/my_project
$ echo ${FOO-nope}
nope
$ echo export FOO=foo > .envrc
\.envrc is not allowed
$ direnv allow .
direnv: reloading
direnv: loading .envrc
direnv export: +FOO
$ echo ${FOO-nope}
foo
$ cd ..
direnv: unloading
direnv export: ~PATH
$ echo ${FOO-nope}
nope
π οΈ SETUP π οΈ
For direnv to work properly it needs to be hooked into the shell. Each shell has its own extension mechanism:
π BASH
Add the following line at the end of the ~/.bashrc file:
eval "$(direnv hook bash)"
Make sure it appears even after rvm, git-prompt and other shell extensions that manipulate the prompt. π
π§ ZSH
Add the following line at the end of the ~/.zshrc file:
eval "$(direnv hook zsh)"
π FISH
Add the following line at the end of the $XDG_CONFIG_HOME/fish/config.fish file:
direnv hook fish | source
Fish supports 3 modes you can set with the global environment variable direnv_fish_mode:
set -g direnv_fish_mode eval_on_arrow # trigger direnv at prompt, and on every arrow-based directory change (default)
set -g direnv_fish_mode eval_after_arrow # trigger direnv at prompt, and only after arrow-based directory changes before executing command
set -g direnv_fish_mode disable_arrow # trigger direnv at prompt only, this is similar functionality to the original behavior
π TCSH
Add the following line at the end of the ~/.cshrc file:
eval `direnv hook tcsh`
π§ Elvish
Run:
~> mkdir -p ~/.config/elvish/lib
~> direnv hook elvish > ~/.config/elvish/lib/direnv.elv
and add the following line to your ~/.config/elvish/rc.elv file:
use direnv
π» PowerShell
Add the following line to your $PROFILE:
Invoke-Expression "$(direnv hook pwsh)"
π Murex
Add the following line to your ~/.murex_profile:
direnv hook murex -> source
π COMMANDS π
direnv allow [PATH_TO_RC]β grants direnv permission to load the given.envrcor.envfile.direnv deny [PATH_TO_RC]β revokes the authorization of a given.envrcor.envfile.direnv edit [PATH_TO_RC]β opensPATH_TO_RCor the current.envrcor.envinto an$EDITORand allow the file to be loaded afterwards.direnv exec DIR COMMAND [...ARGS]β executes a command after loading the first.envrcor.envfound inDIR.direnv export SHELLβ loads an.envrcor.envand prints the diff in terms of exports. Supported shells:bash,zsh,fish,tcsh,elvish,pwsh,murex,json,vim,gha(GitHub Actions),gzenv,systemd.direnv fetchurl <url> [<integrity-hash>]β fetches a given URL into direnvβs CAS.direnv helpβ shows this help.direnv hook SHELLβ used to setup the shell hook.direnv pruneβ removes old allowed files.direnv reloadβ triggers an env reload.direnv statusβ prints some debug status information.direnv stdlibβ displays the stdlib available in the.envrcexecution context.direnv versionβ prints the version or checks that direnv is no older thanVERSION_AT_LEAST.
π§ USAGE π§
In some target folder, create an .envrc file and add some export(1) and unset(1) directives in it.
On the next prompt you will notice that direnv complains about the .envrc being blocked. This is the security mechanism to avoid loading new files automatically. Otherwise any git repo that you pull, or tar archive that you unpack, would be able to wipe your hard drive once you cd into it. π
bestie, that block is not being mean β itβs literally the security mechanism doing its job.
So here we are pretty sure that it wonβt do anything bad. Type direnv allow . and watch direnv loading your new environment. Note that direnv edit . is a handy shortcut that opens the file in your $EDITOR and automatically reloads it if the fileβs modification time has changed.
Now that the environment is loaded you can notice that once you cd out of the directory it automatically gets unloaded. If you cd back into it itβs loaded again. Thatβs the base of the mechanism that allows you to build cool things. π₯
Exporting variables by hand is a bit repetitive so direnv provides a set of utility functions that are made available in the context of the .envrc file. Check the direnv-stdlib(1) man page for more details. You can also define your own extensions inside $XDG_CONFIG_HOME/direnv/direnvrc or $XDG_CONFIG_HOME/direnv/lib/*.sh files.
Hopefully this is enough to get you started. π«Ά
πΈ ENVIRONMENT πΈ
DIRENV_DISABLEβ set to1ortrueto stop direnv from changing the environment. direnv does not revert the changes it already made. Unset it to let direnv continue.XDG_CONFIG_HOMEβ defaults to$HOME/.config.XDG_DATA_HOMEβ defaults to$HOME/.local/share.
π FILES π
$XDG_CONFIG_HOME/direnv/direnv.tomlβ direnv configuration. Seedirenv.toml(1).$XDG_CONFIG_HOME/direnv/direnvrcβ bash code loaded before every.envrc. Good for personal extensions.$XDG_CONFIG_HOME/direnv/lib/*.shβ bash code loaded before every.envrc. Good for third-party extensions.$XDG_DATA_HOME/direnv/allowβ records which.envrcfiles have been direnv allowed.
π«Ά CONTRIBUTE π«Ά
Bug reports, contributions and forks are welcome.
All bugs or other forms of discussion happen on
β¨http://github.com/direnv/direnv/issuesβ©
There is also a wiki available where you can share your usage patterns or other tips and tricks β¨https://github.com/direnv/direnv/wikiβ©
Or drop by on the #direnv channel on FreeNode β¨irc://#direnv@FreeNodeβ© to have a chat. π¬
Β©οΈ COPYRIGHT Β©οΈ
MIT licence - Copyright (C) 2019 @zimbatm and contributors
π SEE ALSO π
direnv-stdlib(1), direnv.toml(1), direnv-fetchurl(1)
π okay bye bestie β this manual is source/version/date: direnv, 2019, DIRENV(1); mwah π
π¦ where this page comes from
- arch rolling: direnv 2.38.2-1 Β· MIT
- nixpkgs unstable: direnv 2.38.2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms