Direnv setup for multiple git/GitHub, npm and AWS accounts/credentials
In this post, we’ll look at how to configure direnv to manage multiple git, GitHub, npm and AWS accounts and credentials on a single machine.
Table of Contents
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.
We can use direnv to apply credentials and configuration to a subtree of our file system. This is useful for example for freelancers who might have separate clients who give them access to their systems. In this scenario, the configurations shouldn’t affect each and we also wouldn’t want to reset the “global” configuration when changing projects.
The following is an .envrc file which overrides npm, gh, git and aws CLI credentials via updating configuration files.
# npm
export NPM_CONFIG_USERCONFIG="~/Documents/project-name/.npmrc"
# GH CLI
export GH_CONFIG_DIR="~/Documents/project-name/.gh-config"
# GH_TOKEN is only needed if you opt to `gh auth login` via token
# export GH_TOKEN="<GH_TOKEN>"
# Git CLI
export GIT_AUTHOR_EMAIL="<find-in-github>@users.noreply.github.com"
export GIT_AUTHOR_NAME="Hugo Di Francesco"
export GIT_COMMITTER_EMAIL="<find-in-github>@users.noreply.github.com"
export GIT_COMMITTER_NAME="Hugo Di Francesco"
# AWS CLI
export AWS_CONFIG_FILE="~/Documents/project-name/.aws/config"
# override the profile to use if not using `default` AWS profile
# from the config using the following
# export AWS_PROFILE="<PROFILE_NAME>"
How to use the .envrc
- Put the above into an
.envrcat the directory inside of which you want the configuration to be applied - Replace
~/Documents/project-namewith the path to the directory where you will store your configuration (essentially the “home/~” directory for the configuration you want to use). - Create an
.npmrcin the right location - Replace “Hugo Di Francesco” with the name you want to commit under.
- Find your
@users.noreply.github.comemail and replace it- the information in Setting your commit email address - GitHub Docs is useful but needs to be adapted
- ensure you select “Keep my email addresses private”
- take note of the
@users.noreply.github.com - put it into the
.envrcforGIT_AUTHOR_EMAILandGIT_COMMITTER_EMAIL
- Go into the directory containing the
.envrc, anddirenv allowit gh auth loginto log into the GitHub CLI- can be checked with
gh auth status
- can be checked with
- Create and configure the
.aws/configfile- can be checked with
aws sts get-caller-identity --no-cli-pager, see docs
- can be checked with
Further reading
ghCLI environment variables reference: https://cli.github.com/manual/gh_help_environmentawsCLI environment variables reference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-listnpmenvironment variables guide: https://docs.npmjs.com/cli/v10/using-npm/config#environment-variablesgitenvironment variables reference: https://git-scm.com/docs/git#_environment_variablesgitenvironment variables reference, “Git Commits” section: https://git-scm.com/docs/git#_git_commits
Interested in Alpine.js?
Power up your debugging with the Alpine.js Devtools Extension for Chrome and Firefox. Trusted by over 15,000 developers (rated 4.5 ⭐️).
orJoin 1000s of developers learning about Enterprise-grade Node.js & JavaScript