Direnv nested
β’ 122 words β’ 1 min β’ updated
I covered direnv before.
New scenario: Given a directory structure with two nested .envrcs:
% tree -a top
top
βββ .envrc
βββ inner
βββ .envrcIt turns out that, surprisingly, cd top/inner will load only
top/inner/.envrc. The top-level top/.envrc will not be automatically
loaded.
This is intentional, meant for security reasons.
In order to source the parent .envrc, add source_up to the inner one.
Alternatively, source_env .. also works. I prefer source_up.
There’s also source_up_if_exists, which works gracefully even if there’s
no env file to source upwards.
The full list of built-in functions is available here: https://direnv.net/man/direnv-stdlib.1.html
Reference: https://github.com/direnv/direnv/issues/757
Use Case: Add top-level environment variables to top/.envrc. Add layout
to top/inner/.envrc. For example, perhaps inner is a custom git repository
/ project.
Backlinks
- direnv: automatically load .env everywhere (Jul 31, 2025)