/ #cli #node #git 

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

  1. Put the above into an .envrc at the directory inside of which you want the configuration to be applied
  2. Replace ~/Documents/project-name with the path to the directory where you will store your configuration (essentially the “home/~” directory for the configuration you want to use).
  3. Create an .npmrc in the right location
  4. Replace “Hugo Di Francesco” with the name you want to commit under.
  5. Find your @users.noreply.github.com email and replace it
    1. the information in Setting your commit email address - GitHub Docs is useful but needs to be adapted
    2. ensure you select “Keep my email addresses private”
    3. take note of the @users.noreply.github.com
    4. put it into the .envrc for GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL
  6. Go into the directory containing the .envrc, and direnv allow it
  7. gh auth login to log into the GitHub CLI
    • can be checked with gh auth status
  8. Create and configure the .aws/config file
    • can be checked with aws sts get-caller-identity --no-cli-pager, see docs

Further reading

Author

Hugo Di Francesco

Co-author of "Professional JavaScript", "Front-End Development Projects with Vue.js" with Packt, "The Jest Handbook" (self-published). Hugo runs the Code with Hugo website helping over 100,000 developers every month and holds an MEng in Mathematical Computation from University College London (UCL). He has used JavaScript extensively to create scalable and performant platforms at companies such as Canon, Elsevier and (currently) Eurostar.

Get The Jest Handbook (100 pages)

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.