direnv has been the most useful tool in my software development repository in the last two years. It’s a simple tool: it loads and unloads environment variables when you move into or out of a directory. It’s fast, extensible, and never screws up, which makes it a rarity.
You set direnv up by dropping an
.envrc file wherever you want to use it. Let’s look at one:
That first line is killer. It tells direnv to create a virtualenv linked to the Python executable at
/usr/local/bin/python3 and then set up the environment variables to activate that virtualenv. When I leave the directory, the environment variables are unset. Similar commands exist for Go, Node, Ruby, and Perl, and it’s not hard to write your own.
The second line uses a command
PATH_add instead of a simple
export statement. This prepends that directory to my
PATH. The reasoning for a special command is to avoid overwriting the
export commands set environment variables I need for this application, which is a Django app. I use django-environ with all my Django applications to make it easy to configure via environment variables.
The best thing about direnv is that it’s extensible. You can add bash functions in a
~/.direnvrc file and they are available to use in
.envrc files. I have this one I took from a comment on GitHub:
I’m not going to explain every line of bash code here, and if you do read and understand it, you’ll see how it is an ugly hack, but it does allow me to do this in an
This has been great for working with Docker. Typing those same commands over and over was not fun.
I hope you give direnv a try! It has replaced a lot of other tools like pyenv and virtualenv for me.