Linux/Mac: per-directory environment variables?

Akin to dotenv in the JS ecosystem, do you guys use anything more generic in your Linux / Mac boxes that automatically loads a file (ideally called .env) when you enter the directory where the file is?

I found direnv so far. Anybody using it? Or anything else?


I prefer to manually source from a set of files which alter my environment to my needs.

During the ruby days I used rvm to manage ruby versions, which allowed to set up the environment when entering the directory. This allowed every random git repository I checked out to run arbitrary code due to how it was implemented.

Also it was kind of strict, as it only allowed to have a single environment, rather than many.

1 Like

That’s my current solution but since I participate in several projects it really gets tiring over time. :071:

1 Like

I’ve been using direnv for a very long time. It’s great, no manual sourcing. I always add “PATH_add bin” + needed env variables. The file is always git ignored but I back up it manually to gopass / get variables values from gopass.


I can install direnv alright but not sure how will I go about backing up the files. I use a monolithic single-encrypted-vault-file password manager synced through Dropbox (namely Enpass) already but it doesn’t have an API like gopass (command-line in this case).

I also have scripts that I am source-ing. Maybe I should make the files in the project directories source these because they are already backed up to Dropbox as well.

EDIT: Any idea how to make direnv work for a bunch of directories which are children of the one where your .envrc is located? I have a ~/code/<org_name>/...several projects... directory. I guess I can copy-paste the same file across all of them. :roll_eyes:

Put a one-liner .envrc file in each product directory containing e.g. . ~/code/<org_name>/.envrc.local which contains your variables and/or sources a common file or files.

1 Like

I’ve just created my own version of phx which contains the following

env $(cat .env.local | xargs) iex -S mix phx.server

I’ve place this in my $HOME/bin/phx and added $HOME/bin to my $PATH

Then I just place environtment variables in .env.local and run phx

Might not be the best solution but it has served me well for a long time.

1 Like

If you’re thinking in terms of project environment variables that works with Mix, then I’ve used Envy before which works pretty well. I don’t recommend this for production use, but for local development it works well.

I just have my dev.exs config file also import a dev.secrets.exs file which isn’t checked in (via .gitignore) and I try to avoid actual env vars on my local machine. Multiple projects with conflicting env vars on a local machine is a good way to lose your sanity.

1 Like

Yeah but I am very sure I will forget to backup such files… :frowning:

Quite interesting, checking it out!

To load .envrc from higher directories you need to explicitly use source_env [path] where [path] can be relative or absolute, however keep in mind that these files will not be source protected. If you want to load .env files you can use dotenv command in the same manner. More information can be found at man 1 direnv-stdlib.


I’ve been using direnv for a very long time now, I quite like it, especially how it handles whitelisting.