direnv -- Unclutter your .profile

Many of my projects need specific environment variables to run. Usually I create a export.sh script that I source when I cd into the directory, or add them into my docker compose file.

There’s a better way, which I highly recommend: direnv.

direnv is an environment switcher for the shell. It knows how to hook into bash, zsh, tcsh and fish shell to load or unload environment variables depending on the current directory. This allows project-specific environment variables without cluttering the ~/.profile file.

Before each prompt, direnv checks for the existence of a .envrc file in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.

It also has a robust standard library which even allows activating virtualenv’s automatically!

screencasts

All these screencasts were taken from the projects screencasts doc.

direnv installation on OS X with Homebrew

The direnv security model

Handling ruby versions with direnv