1DIRENV(1) User Manuals DIRENV(1)
2
3
4
6 direnv - unclutter your .profile
7
8
10 direnv command ...
11
12
14 direnv is an environment variable manager for your shell. It knows how
15 to hook into bash, zsh and fish shell to load or unload environment
16 variables depending on your current directory. This allows you to have
17 project-specific environment variables and not clutter the "~/.profile"
18 file.
19
20
21 Before each prompt it checks for the existence of an .envrc file in the
22 current and parent directories. If the file exists, it is loaded into a
23 bash sub-shell and all exported variables are then captured by direnv
24 and then made available to your current shell, while unset variables
25 are removed.
26
27
28 Because direnv is compiled into a single static executable it is fast
29 enough to be unnoticeable on each prompt. It is also language agnostic
30 and can be used to build solutions similar to rbenv, pyenv, phpenv, ...
31
32
34 $ cd ~/my_project
35 $ echo ${FOO-nope}
36 nope
37 $ echo export FOO=foo > .envrc
38 \.envrc is not allowed
39 $ direnv allow .
40 direnv: reloading
41 direnv: loading .envrc
42 direnv export: +FOO
43 $ echo ${FOO-nope}
44 foo
45 $ cd ..
46 direnv: unloading
47 direnv export: ~PATH
48 $ echo ${FOO-nope}
49 nope
50
51
52
54 For direnv to work properly it needs to be hooked into the shell. Each
55 shell has it's own extension mechanism:
56
57
58 BASH
59 Add the following line at the end of the ~/.bashrc file:
60
61
62 eval "$(direnv hook bash)"
63
64
65
66 Make sure it appears even after rvm, git-prompt and other shell exten‐
67 sions that manipulate the prompt.
68
69
70 ZSH
71 Add the following line at the end of the ~/.zshrc file:
72
73
74 eval "$(direnv hook zsh)"
75
76
77
78 FISH
79 Add the following line at the end of the $XDG_CONFIG_HOME/fish/con‐
80 fig.fish file:
81
82
83 direnv hook fish | source
84
85
86
87 Fish supports 3 modes you can set with with the global environment
88 variable direnv_fish_mode:
89
90
91 set -g direnv_fish_mode eval_on_arrow # trigger direnv at prompt, and on every arrow-based directory change (default)
92 set -g direnv_fish_mode eval_after_arrow # trigger direnv at prompt, and only after arrow-based directory changes before executing command
93 set -g direnv_fish_mode disable_arrow # trigger direnv at prompt only, this is similar functionality to the original behavior
94
95
96
97 TCSH
98 Add the following line at the end of the ~/.cshrc file:
99
100
101 eval `direnv hook tcsh`
102
103
104
105 Elvish
106 Run:
107
108
109 $> direnv hook elvish > ~/.elvish/lib/direnv.elv
110
111
112
113 and add the following line to your ~/.elvish/rc.elv file:
114
115
116 use direnv
117
118
119
121 In some target folder, create an .envrc file and add some export(1) and
122 unset(1) directives in it.
123
124
125 On the next prompt you will notice that direnv complains about the .en‐
126 vrc being blocked. This is the security mechanism to avoid loading new
127 files automatically. Otherwise any git repo that you pull, or tar ar‐
128 chive that you unpack, would be able to wipe your hard drive once you
129 cd into it.
130
131
132 So here we are pretty sure that it won't do anything bad. Type direnv
133 allow . and watch direnv loading your new environment. Note that di‐
134 renv edit . is a handy shortcut that opens the file in your $EDITOR and
135 automatically reloads it if the file's modification time has changed.
136
137
138 Now that the environment is loaded you can notice that once you cd out
139 of the directory it automatically gets unloaded. If you cd back into it
140 it's loaded again. That's the base of the mechanism that allows you to
141 build cool things.
142
143
144 Exporting variables by hand is a bit repetitive so direnv provides a
145 set of utility functions that are made available in the context of the
146 .envrc file. Check the direnv-stdlib(1) man page for more details. You
147 can also define your own extensions inside $XDG_CONFIG_HOME/direnv/di‐
148 renvrc or $XDG_CONFIG_HOME/direnv/lib/*.sh files.
149
150
151 Hopefully this is enough to get you started.
152
153
155 XDG_CONFIG_HOME
156 Defaults to $HOME/.config.
157
158
160 $XDG_CONFIG_HOME/direnv/direnv.toml
161 Direnv configuration. See direnv.toml(1).
162
163
164 $XDG_CONFIG_HOME/direnv/direnvrc
165 Bash code loaded before every .envrc. Good for personal exten‐
166 sions.
167
168
169 $XDG_CONFIG_HOME/direnv/lib/*.sh
170 Bash code loaded before every .envrc. Good for third-party ex‐
171 tensions.
172
173
174 $XDG_DATA_HOME/direnv/allow
175 Records which .envrc files have been direnv allowed.
176
177
179 Bug reports, contributions and forks are welcome.
180
181
182 All bugs or other forms of discussion happen on http://github.com/di‐
183 renv/direnv/issues ⟨http://github.com/direnv/direnv/issues⟩
184
185
186 There is also a wiki available where you can share your usage patterns
187 or other tips and tricks https://github.com/direnv/direnv/wiki
188 ⟨https://github.com/direnv/direnv/wiki⟩
189
190
191 Or drop by on the #direnv channel on FreeNode ⟨irc://#direnv@FreeNode⟩
192 to have a chat.
193
194
196 MIT licence - Copyright (C) 2019 @zimbatm and contributors
197
198
200 direnv-stdlib(1), direnv.toml(1), direnv-fetchurl(1)
201
202
203
204direnv 2019 DIRENV(1)