direnv lets you load and unload environment variables depending on the current directory. This is incredibly helpful for per-project isolated development environments or in DevOps for loading secrets for deployment of software builds reliably.
TL:DR – This is a shell extension, for Unix-like operating systems with a supported shell such as bash, zsh, and is easy to install and widely supported. Highly recommended.
Contents
direnv
direnv lets you load and unload environment variables depending on the current directory. This is incredibly helpful for per-project isolated development environments or in DevOps for loading secrets for deployment of software builds reliably.
Installing
The installation has two parts.
- Install the package or binary
- hook into your shell.
See https://direnv.net/docs/installation.html
.envrc
This file is bash code that is going to be loaded by direnv. It contains the variables you want while you are in this folder. For example for my Joomla Plugin build script I need to tell it the name of the plugin via a variable to keep it our of the script - then I can use the script on multiple plugins and set all the secrets in environment variables. This is really nice as it means I won't accidentally check secrets in to my repositories!
export PLUGIN_NAME="Content - EZONE Automatic Meta Description"
export PLUGIN_ELEMENT="autometa"
To apply the .envrc
type direnv allow . and you'll see confirmation that your environment variables are available now. And you can run your script with them.
direnv: loading .envrc
direnv: export +PLUGIN_ELEMENT +PLUGIN_NAME
Highly recommended
direnv saved hours of time and isolated my keys and paths and whatnot from the script.
I used it in Automating publication of a Joomla Plugin using an update server to keep the plugin details out of the script itself.