Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created September 17, 2024 14:37
Show Gist options
  • Save HariSekhon/009b2520b05c632e41c0d556b65a3fc0 to your computer and use it in GitHub Desktop.
Save HariSekhon/009b2520b05c632e41c0d556b65a3fc0 to your computer and use it in GitHub Desktop.
direnv.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowledge-Base

direnv - .envrc

direnv reads .envrc files and auto-loads useful environment variables upon switching directories that allow you to quickly switch between AWS profiles, EKS clusters, Terraform & Terragrunt versions or any number of other software versions or profiles.

Install direnv

On Mac just:

brew install direnv

This is automatically installed as part of core software by make in DevOps-Bash-tools repo.

For other platforms and more install details read:

https://direnv.net/docs/installation.html

Commands

Enable direnv in your shell

Add the direnv hook to your shell $HOME/.bashrc or similar:

eval "$(direnv hook bash)"

Approve .envrc files

When you switch to a directory containing an .envrc file for the first time it will print a warning that you need to approve the .envrc file.

cd to a dir with a .envrc and then run this allow it to automatically load this .envrc each time in future:

direnv allow

To edit, will auto-approve when you save and exit:

direnv edit

Advanced Examples

General - Ansible, Cloudflare, Docker, GitHub, Pre-Commit

.envrc

Python

Since recent versions of pip on Macs don't like to let you install packages to the system python any more, you really need to keep a virtualenv activated all the times for your personal tools and scripts.

Create your Python virtual:

virtualenv ~/venv

Then use this .envrc to keep it automatically activated:

.envrc-python

AWS

.envrc-aws

GCP

.envrc-gcp

Kubernetes

.envrc-kubernetes - Kubernetes-configs repo

or

.envrc-kubernetes - DevOps-Bash-tools repo

Terraform

.envrc-terraform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment