Craftsman at work

I'm Artur Karbone, coding software architect and independent IT consultant and this is my blog about craftsmanship, architecture, distributive systems, management and much more.

Master environment variables per project with Direnv

I just faced an amazing tool, called Direnv, which I immediately put in my toolbox. The name reveals the intention - it manages environment variables per folder. It knows how to hook into different shells and load variables from a configuration file just per that particular folder.

Let's install it:

brew install direnv  

Then attach to your shell of choice:

eval "$(direnv hook bash)"  

Now we are good to create a configuration file, where all the variables per directory/project will be stored:

echo export SENDGRID_KEY=SOME_KEY > .envrc  

The format of the file is quite simple. You just export key/value pair. That's it:

export KEY1=<VALUE>  
export KEY2=<VALUE>  

To load the variables run (usually when you edit or add new):

direnv allow  

To summarize there is the following workflow:

  • Create .envrc file per project/directory
  • Fill .envrc with variable
  • Use the variables in the scope of the directory
comments powered by Disqus