Skip to content

Direnv Template



IMPORTANT: Project Archived Development of this project has been stop in favor of project My Dotfiles / Direnv to setup my `direnv` configuration globally and not per project

Project to manage Directory Environment (either manually or using direnv) uniformly for all my projects.


IMPORTANT !
Main repo is on Framagit - My Programs / Direnv Template.
On other online git platforms, they are just mirrors of the main repo.
Any issues, pull/merge requests, etc., might not be considered on those other platforms.

Introduction

This repo aims to help managing directory environment uniformly accross multiple project. Management of directory environment (i.e. activation and deactivation) can be done manually or using direnv.

What is a directory environment ?

What we call a directory environment is a set of environment variables, binary, scripts, etc., that should only be configured when working on a specific project (i.e. in a specific folder and its subfolder).

For instance, if you use python virtualenv and OpenStack. Usually, when starting to work, you often may enter the following command:

# Load OpenStack project variable
source openrc.sh
# Activate python virtual environment
source .venv/bin/activate

This will setup OpenStack related variable and Python Virtual environment related variable as well as the method deactivate to deactivate the python virtual environment.

Both of these are directory related process, usually you do not want these values to be set when on another directory.

So to conclude this description, basically this repo will help you to setup your directory to:

  • Automate these command to setup environment variable and methods when entering directories in which you want these varaible set
  • Automate unsetting these variables and methods when leaving the directory.

This can be achieve manually (by sourcing the right file) or by using 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.

In other terms, if a script .envrc is present in a folder and allowed for direnv, it will automatically be executed when entering the folder. When leaving the folder any exported variables will be automatically unloaded.

Why this repo since there is direnv ?

Since some times now, I use direnv to manage my directory environment. Nevertheless, I was tired to always rewrite or copy/paste the same base scripts, then adapt them for each of my working directory. This repo is here to help managing the directory environment script in a homogeneous manner. Now, I do not need to rewrite or copy/paste the base scripts. All of my repos have the same scripts (i.e. modules) and I configure them using a .envrc.ini file.

Quickstart

In this section, we will see succinctly how to use this repo to setup directory environment for your current folder.

First, install scripts setting up directory environment:

curl -sSL https://framagit.org/rdeville.public/my_programs/direnv_template/-/raw/master/setup.sh | bash -

This will create file .envrc and folder .direnv.

Then copy .direnv/templates/envrc.template.ini next to .envrc.

cp .direnv/templates/envrc.template.ini .envrc.ini

Now, see modules documentation to choose which module you want as well as their configuration. Then, update .envrc.ini configuration file accordingly with your favorite editor (vim here as example).

vim .envrc.ini

Finally, activate the directory environment.

# If you are using `direnv`, you can allow it for the current directory
direnv allow

# If you are not using `direnv`, you can activate it manually.
source .direnv/activate_direnv

This will parse your .envrc.ini and load modules accordingly.

Once you have finish working, you may want to deactivate directory environment:

  • If you are using direnv, you have nothing to do, the directory environment will automatically be deactivated when leaving the directory.

  • If you activate it manually, a new command should be defined: deactivate_direnv. You simply need to call it wherever you are.

See Setup directory environment for a more complete usage description.


Last update: January 13, 2022
Back to top