Introduction

One of the challenges I have had as an operations manager is managing configuration settings for the many tools and applications we use on a daily basis. I have been looking for a sane way of managing configuration settings that did not involve abusing my .zshrc/.bashrc files, or creating dozens of one-off aliases. I think I have found a pretty good solution in the form of direnv.

direnv

direnv is a tool for automatically setting and switching environment variables. It works by adding a special configuration file. .envrc, in each project directory containing the environment variables for that project. When you enter that directory in a shell like bash or zsh it automatically sets those values in the current session. direnv also unsets the environment variables when you leave the project directory, which is nice from a security/credentials stand point. It makes it more difficult to accidentally use the wrong settings because you have forgotten what was last set.

It also support cascading configuration files. This means you can set global settings in the project root directory, and have more specific settings in a sub-directory.

Git Configuration

Since I tend to add things like API keys and other secrets to my .envrc, I have a global ignore rule in git to prevent them from being accidentally added to a GitHub repository In my case, I have a .gitignore_global file in my home directory that contains .envrc as a pattern to ignore, and git is configured to use that as the global ignore file.

>\$ git config --global core.excludefiles '~/.gitignore_global'

Limitations

Near as I can tell, direnv only supports Unix-like systems like Linux or Mac, but it does not have support for Windows Powershell. It may work with the Windows Subsystem for Linux, but I have not had a chance to try that yet. I use it on Linux, and it works very well there.

If you primarily work in a Linux or Unix environment I highly recommend giving it a shot.