.nh .TH DIRENV 1 "2019" direnv "User Manuals" .SH NAME .PP direnv - unclutter your .profile .SH SYNOPSIS .PP \fB\fCdirenv\fR \fIcommand\fP ... .SH DESCRIPTION .PP \fB\fCdirenv\fR is an environment variable manager for your shell. It knows how to hook into bash, zsh and fish shell to load or unload environment variables depending on your current directory. This allows you to have project-specific environment variables and not clutter the "~/.profile" file. .PP Before each prompt it checks for the existence of an \fB\fC\&.envrc\fR file in the current and parent directories. If the file exists, it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to your current shell, while unset variables are removed. .PP Because direnv is compiled into a single static executable it is fast enough to be unnoticeable on each prompt. It is also language agnostic and can be used to build solutions similar to rbenv, pyenv, phpenv, ... .SH EXAMPLE .PP .RS .nf $ cd ~/my_project $ echo ${FOO-nope} nope $ echo export FOO=foo > .envrc \\.envrc is not allowed $ direnv allow . direnv: reloading direnv: loading .envrc direnv export: +FOO $ echo ${FOO-nope} foo $ cd .. direnv: unloading direnv export: ~PATH $ echo ${FOO-nope} nope .fi .RE .SH SETUP .PP For direnv to work properly it needs to be hooked into the shell. Each shell has it's own extension mechanism: .SS BASH .PP Add the following line at the end of the \fB\fC~/.bashrc\fR file: .PP .RS .nf eval "$(direnv hook bash)" .fi .RE .PP Make sure it appears even after rvm, git-prompt and other shell extensions that manipulate the prompt. .SS ZSH .PP Add the following line at the end of the \fB\fC~/.zshrc\fR file: .PP .RS .nf eval "$(direnv hook zsh)" .fi .RE .SS FISH .PP Add the following line at the end of the \fB\fC$XDG_CONFIG_HOME/fish/config.fish\fR file: .PP .RS .nf direnv hook fish | source .fi .RE .PP Fish supports 3 modes you can set with with the global environment variable \fB\fCdirenv_fish_mode\fR: .PP .RS .nf set -g direnv_fish_mode eval_on_arrow # trigger direnv at prompt, and on every arrow-based directory change (default) set -g direnv_fish_mode eval_after_arrow # trigger direnv at prompt, and only after arrow-based directory changes before executing command set -g direnv_fish_mode disable_arrow # trigger direnv at prompt only, this is similar functionality to the original behavior .fi .RE .SS TCSH .PP Add the following line at the end of the \fB\fC~/.cshrc\fR file: .PP .RS .nf eval `direnv hook tcsh` .fi .RE .SS Elvish .PP Run: .PP .RS .nf $> direnv hook elvish > ~/.elvish/lib/direnv.elv .fi .RE .PP and add the following line to your \fB\fC~/.elvish/rc.elv\fR file: .PP .RS .nf use direnv .fi .RE .SH USAGE .PP In some target folder, create an \fB\fC\&.envrc\fR file and add some export(1) and unset(1) directives in it. .PP On the next prompt you will notice that direnv complains about the \fB\fC\&.envrc\fR being blocked. This is the security mechanism to avoid loading new files automatically. Otherwise any git repo that you pull, or tar archive that you unpack, would be able to wipe your hard drive once you \fB\fCcd\fR into it. .PP So here we are pretty sure that it won't do anything bad. Type \fB\fCdirenv allow .\fR and watch direnv loading your new environment. Note that \fB\fCdirenv edit .\fR is a handy shortcut that opens the file in your $EDITOR and automatically reloads it if the file's modification time has changed. .PP Now that the environment is loaded you can notice that once you \fB\fCcd\fR out of the directory it automatically gets unloaded. If you \fB\fCcd\fR back into it it's loaded again. That's the base of the mechanism that allows you to build cool things. .PP Exporting variables by hand is a bit repetitive so direnv provides a set of utility functions that are made available in the context of the \fB\fC\&.envrc\fR file. Check the direnv-stdlib(1) man page for more details. You can also define your own extensions inside \fB\fC$XDG_CONFIG_HOME/direnv/direnvrc\fR or \fB\fC$XDG_CONFIG_HOME/direnv/lib/*.sh\fR files. .PP Hopefully this is enough to get you started. .SH ENVIRONMENT .TP \fB\fCXDG_CONFIG_HOME\fR Defaults to \fB\fC$HOME/.config\fR\&. .SH FILES .TP \fB\fC$XDG_CONFIG_HOME/direnv/direnv.toml\fR Direnv configuration. See direnv.toml(1). .TP \fB\fC$XDG_CONFIG_HOME/direnv/direnvrc\fR Bash code loaded before every \fB\fC\&.envrc\fR\&. Good for personal extensions. .TP \fB\fC$XDG_CONFIG_HOME/direnv/lib/*.sh\fR Bash code loaded before every \fB\fC\&.envrc\fR\&. Good for third-party extensions. .TP \fB\fC$XDG_DATA_HOME/direnv/allow\fR Records which \fB\fC\&.envrc\fR files have been \fB\fCdirenv allow\fRed. .SH CONTRIBUTE .PP Bug reports, contributions and forks are welcome. .PP All bugs or other forms of discussion happen on http://github.com/direnv/direnv/issues \[la]http://github.com/direnv/direnv/issues\[ra] .PP There is also a wiki available where you can share your usage patterns or other tips and tricks https://github.com/direnv/direnv/wiki \[la]https://github.com/direnv/direnv/wiki\[ra] .PP Or drop by on the #direnv channel on FreeNode \[la]irc://#direnv@FreeNode\[ra] to have a chat. .SH COPYRIGHT .PP MIT licence - Copyright (C) 2019 @zimbatm and contributors .SH SEE ALSO .PP direnv-stdlib(1), direnv.toml(1), direnv-fetchurl(1)