Unclutter your .profile
Go Shell HTML Makefile Other
Latest commit 151116c Nov 10, 2016 @danielcavanagh danielcavanagh committed with zimbatm fix tcsh escaping (#241)
prevent a final $ in a variable value from causing a tcsh error
Permalink
Failed to load latest commit information.
docs Update repo urls to point to the new org Jun 27, 2015
man Use go-md2man to generate the man pages Aug 29, 2016
script Use Travis to push release builds to github Jun 30, 2014
test Remove allow files in tests Mar 26, 2016
.gitignore Use go-md2man to generate the man pages Aug 29, 2016
.nojekyll New site: put the doc inside the project so it stays in sync Jun 12, 2014
.travis.yml Build release with Go 1.7 Sep 20, 2016
CHANGELOG.md Release v2.9.0 Jul 3, 2016
CNAME New site: put the doc inside the project so it stays in sync Jun 12, 2014
LICENSE.md Update repo urls to point to the new org Jun 27, 2015
Makefile Use go-md2man to generate the man pages Aug 29, 2016
README.md Link to Fedora package Sep 2, 2016
cmd_allow.go Display command arguments in the help Apr 1, 2013
cmd_apply_dump.go Only backup the diff of environments. Fixes #82 Jan 11, 2014
cmd_current.go Handle `direnv current` with no argument May 19, 2016
cmd_deny.go Corrected spelling mistake in deny command Feb 22, 2015
cmd_dotenv.go Fix dotenv with explicit .env Jun 24, 2014
cmd_dotenv_test.go Support more of the dotenv syntax format Nov 20, 2013
cmd_dump.go WATCHES passes all tests Mar 13, 2016
cmd_edit.go Some general cleanup. Also fixed a bug introduced in edit Mar 21, 2016
cmd_exec.go Some general cleanup. Also fixed a bug introduced in edit Mar 21, 2016
cmd_expand_path.go Display command arguments in the help Apr 1, 2013
cmd_export.go Simplify ranges without values Jun 21, 2016
cmd_help.go Add command aliases. `direnv --help` is an alias to `direnv help` Dec 24, 2013
cmd_hook.go refactor shells Mar 26, 2016
cmd_prune.go prune: better desc Mar 26, 2016
cmd_reload.go Fix `go vet` warnings Jun 21, 2016
cmd_status.go Simplify ranges without values Jun 21, 2016
cmd_stdlib.go Move the stdlib into a .sh file to benefit from syntax coloring Apr 21, 2014
cmd_version.go Move the version information into the direnv executable. Apr 1, 2013
cmd_watch.go refactor shells Mar 26, 2016
commands.go Merge pull request #153 from avnik/master Mar 26, 2016
config.go Build-time bash path Apr 4, 2016
const.go Fixing "lagging" on first entry Mar 20, 2016
default.nix default.nix to build direnv during development Jul 3, 2016
env.go refactor shells Mar 26, 2016
env_diff.go refactor shells Mar 26, 2016
env_diff_test.go Fix `go vet` warnings Jun 21, 2016
env_test.go Fix `go vet` warnings Jun 21, 2016
file_times.go Fixing "lagging" on first entry Mar 20, 2016
file_times_test.go File times impl working, tested Mar 10, 2016
index.html Beginning process of watching multiple files Mar 5, 2016
log.go Some general cleanup. Also fixed a bug introduced in edit Mar 21, 2016
look_path.go Try to detect an editor in the PATH if EDITOR is not set. Jun 15, 2014
main.go Build-time bash path Apr 4, 2016
marshal.go File times impl working, tested Mar 10, 2016
rc.go Merge pull request #153 from avnik/master Mar 26, 2016
rc_test.go If two paths don't have a common ancestors, don't make them relative. Feb 28, 2014
shell.go direnv export json: a new universal format Mar 26, 2016
shell_bash.go refactor shells Mar 26, 2016
shell_fish.go refactor shells Mar 26, 2016
shell_json.go direnv export json: a new universal format Mar 26, 2016
shell_tcsh.go fix tcsh escaping (#241) Nov 10, 2016
shell_test.go refactor shells Mar 26, 2016
shell_vim.go refactor shells Mar 26, 2016
shell_zsh.go refactor shells Mar 26, 2016
stdlib.go Add more escaping Aug 22, 2016
stdlib.sh Add more escaping Aug 22, 2016
version.go Release v2.9.0 Jul 3, 2016
version.txt Release v2.9.0 Jul 3, 2016
xdg.go Still WIP but approaching a workable state Feb 7, 2013

README.md

direnv -- Unclutter your .profile

direnv is an environment switcher for the shell. It knows how to hook into bash, zsh, tcsh and fish shell to load or unload environment variables depending on the current directory. This allows project-specific environment variables without cluttering the "~/.profile" file.

Before each prompt, direnv checks for the existence of a ".envrc" file in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.

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 and phpenv.

Example

$ 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

Install

From source

Dependencies: make, golang

git clone https://github.com/direnv/direnv
cd direnv
make install
# or symlink ./direnv into the $PATH

From system packages

direnv is packaged for a variety of systems:

From binary builds

Binary builds for a variety of architectures are also available for each release.

Fetch the binary, chmod +x direnv and put it somewhere in your PATH.

Setup

For direnv to work properly it needs to be hooked into the shell. Each shell has its own extension mechanism:

BASH

Add the following line at the end of the "~/.bashrc" file:

eval "$(direnv hook bash)"

Make sure it appears even after rvm, git-prompt and other shell extensions that manipulate the prompt.

ZSH

Add the following line at the end of the "~/.zshrc" file:

eval "$(direnv hook zsh)"

FISH

Add the following line at the end of the "~/.config/fish/config.fish" file:

eval (direnv hook fish)

TCSH

Add the following line at the end of the "~/.cshrc" file:

eval `direnv hook tcsh`

Usage

In some target folder, create a ".envrc" file and add some export(1) directives in it.

Note that the contents of the .envrc file must be valid bash syntax, regardless of the shell you are using. This is because direnv always executes the .envrc with bash (a sort of lowest common denominator of UNIX shells) so that direnv can work across shells. If you try to use some syntax that doesn't work in bash (like zsh's nested expansions), you will run into trouble.

On the next prompt you will notice that direnv complains about the ".envrc" 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 cd into it.

So here we are pretty sure that it won't do anything bad. Type direnv allow . and watch direnv loading your new environment. Note that direnv edit . is a handy shortcut that opens the file in your $EDITOR and automatically allows it if the file's modification time has changed.

Now that the environment is loaded, you will notice that once you cd out of the directory it automatically gets unloaded. If you cd back into it, it's loaded again. That's the basis of the mechanism that allows you to build cool things.

The stdlib

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 ".envrc" file.

As an example, the PATH_add function is used to expand and prepend a path to the $PATH environment variable. Instead of export $PATH=$PWD/bin:$PATH you can write PATH_add bin. It's shorter and avoids a common mistake where $PATH=bin.

To find the documentation for all available functions check the direnv-stdlib(1) man page.

It's also possible to create your own extensions by creating a bash file at "~/.config/direnv/direnvrc" or "~/.direnvrc". This file is loaded before your ".envrc" and thus allows you to make your own extensions to direnv.

Loading layered .envrc

Let's say you have the following structure:

  • "/a/.envrc"
  • "/a/b/.envrc"

If you add the following line in "/a/b/.envrc", you can load both of the ".envrc" files when you are in /a/b:

source_env ..

Similar projects

  • Environment Modules - one of the oldest (in a good way) environment-loading systems
  • autoenv - lightweight; doesn't support unloads
  • zsh-autoenv - a feature-rich mixture of autoenv and smartcd: enter/leave events, nesting, stashing (Zsh-only).

Contribute

Bug reports, contributions and forks are welcome.

All bugs or other forms of discussion happen on http://github.com/direnv/direnv/issues.

There is a wiki available where you can share your usage patterns or other tips and tricks https://github.com/direnv/direnv/wiki

For longer form discussions you can also write to direnv-discuss@googlegroups.com

Or drop by on IRC (#direnv on freenode) to have a chat. If you ask a question make sure to stay around as not everyone is active all day.

Build Status

COPYRIGHT

Copyright (C) 2014 shared by all contributors under the MIT licence.