Skip to content

Instantly share code, notes, and snippets.

@cdegroot
Created April 11, 2021 17:29
Show Gist options
  • Save cdegroot/148733a6d468a0a2c863fb38aedeaaed to your computer and use it in GitHub Desktop.
Save cdegroot/148733a6d468a0a2c863fb38aedeaaed to your computer and use it in GitHub Desktop.
#!/bin/sh
destdir=$(pwd -P)
destfile=$destdir/.envrc
linksdir=~/.config/direnv/links
rcfile=$linksdir/$(echo $destdir | sed 's,/,_+_,g')
if [ -f "$rcfile" ]
then
echo "File exists, maybe you meant direnv-install-links?"
exit 1
fi
if [ -f "$destfile" ]
then
cat <<EOF
.envrc exists. Execute:
mv .envrc $rcfile
and then run direnv-install-links. Hit Enter to do that now, Ctrl-C otherwise.
EOF
read dummy
mv .envrc $rcfile
direnv-install-links
exit 0
fi
echo '# -*- mode: sh; -*-' >$rcfile
echo '' >>$rcfile
echo Installing $rcfile in $destfile
ln -s $rcfile $destfile
echo "Edit .envrc now"
@cdegroot
Copy link
Author

Direnv is pretty neat and I use it all the time. But in repos that are not exclusively mine, you cannot add .envrc files and I kept losing them. direnv-new, above, will create a file under ~/.config/direnv and symlink it, and direnv-install-links will install all these links in case you cleaned a repo or something. I combine it with dotfiles in git so I can version control my .envrc files and share them between machines.

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