Direnv
direnv lets you modify your environment as you change directories.
Documentation
github https://github.com/direnv/direnv stdlib docs https://github.com/direnv/direnv/blob/master/man/direnv-stdlib.1.md stdlib src (more complete) https://github.com/direnv/direnv/blob/master/stdlib.sh
Locations
(.|..)/.envrc
instructions to load on cd ~/.config/direnv/lib/*.sh
define your own functions for .envrc
Overview
When directory is changed, current dir, and all parent dirs are checked for a trusted
.envrc
file.
If present, it is loaded.Integrate into your shell by adding
eval "$(direnv hook zsh)"
to your shell's rcfile.After making changes to
.envrc
, rundirenv trust
to allow the code to execute.
Install
WARNING:
if installing direnv with the intention of using it alongside nix, use nix to install it
pacaur -S direnv # archlinux nix-env -i direnv # nix/nixos
Usage
direnv allow # trust directory direnv deny # untrust directory
Configuration
For direnv to work, you need to add a hook to your shell.
I've encountered recursion issues with direnv+nix, so I omit the hook if I'm already in a direnv resolved environment
Here is my zshrc/bashrc config
# skip loading direnv if it is already loaded if test -z $DIRENV_DIR; then if whereis direnv 2&>1 1> /dev/null ; then case "$(basename $SHELL)" in zsh) eval "$(direnv hook zsh)" ;; bash) eval "$(direnv hook bash)" ;; *) echo "skip" /dev/null ;; esac fi fi
Syntax
direnv is just shellscript.
You can use it's builtin functions, or simply run shellscript.For example.
# /home/you/dev/foo export FOO=barcd ~/dev/foo direnv allow # FOO=bar is set in environment cd .. # FOO is not set in environment