direnv/direnv-vscode

How to choose cache depending on env variable?

Closed this issue · 0 comments

yajo commented

I have a folder which needs to be loaded using different env depending on an env variable.

The .envrc looks like this:

# Set different cache dir per devshell
direnv_layout_dir () {
    echo "$PWD/.direnv/$(echo $DIRENV_USE_FLAKE_ARGS | tr -cd '[:alnum:]')"
}

use flake $DIRENV_USE_FLAKE_ARGS --impure --accept-flake-config

It's using direnv_layout_dir to define a custom cache dir, as explained in https://github.com/direnv/direnv/wiki/Customizing-cache-location.

In the terminal, when I set that variable and enter the folder, it correctly picks up the cached folder (I use nix-direnv).

When I do it with VSCode by using 2 different project.code-workspace files, the extension doesn't react until I issue a direnv reload command in the terminal. Those files look like:

{"folders":[{"path":"."}],"settings":{"terminal.integrated.env.linux":{"DIRENV_USE_FLAKE_ARGS":".#poetry-16_0"}}}

and the other file is:

{"folders":[{"path":"."}],"settings":{"terminal.integrated.env.linux":{"DIRENV_USE_FLAKE_ARGS":".#poetry-15_0"}}}

So, is there any way to tell this extension some env variable that it must load before attempting to enable direnv? Or anything that lets me define 2 envs in the same project?