direnv
direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.
https://direnv.net/
So what does it do?
Once entering a folder in a shell it can change your environment variables, load programs, etc.
It is quite popular in the Nix ecosystem, but applies to every other distribution as well. See the direnv website on how to install.
For me it is a simple pacman -S direnv to install and add the hook to my fish shell.
From there it depends what you want to use it for.
Hugo & direnv#
I use direnv to have hugo available without installing it to my system.
# change to my-project, download and extract to .env folder
cd ~/my-project
mkdir .env
cd .env
wget https://github.com/gohugoio/hugo/releases/download/v0.155.1/hugo_0.155.1_Linux-64bit.tar.gz
tar xzf hugo_0.155.1_Linux-64bit.tar.gz
Now basically the hugo binary is available within the project. All I need to do is add the .env folder to my $PATH to have access to it within the project.
cd ~/my-project
echo "PATH_add .env" > .envrc
As soon as direnv registers that there’s a .envrc it asks for permission to render the file (which is good, since this could contain malicious code).
Allow with direnv allow
Once the project directory is entered direnv will notify:
direnv: loading ~/my-project/.envrc
direnv: export ~PATH
Leaving the directory will print a simple direnv: unloading
it’s a good idea to add the .env directory to the .gitignore
python venv#
Another example is python venv. Simply put: create a virtual environment with python in a project and activate on entering.
# create venv
python -m venv .venv
# .envrc content
export VIRTUAL_ENV="venv"
layout python
Enable with direnv allow and use the venv without having to activate it manually.
Proceed with pip-installs without cluttering the system.