thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

Direnv nested

β€’ 122 words β€’ 1 min β€’ updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

I covered direnv before.

New scenario: Given a directory structure with two nested .envrcs:

% tree -a top
top
β”œβ”€β”€ .envrc
└── inner
    └── .envrc

It 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.