Task in VSCode to automatically call nix-shell when a folder is opened

I have a task in vscode to automatically call nix-shell in the integrated terminal when a folder is opened to set up the environment, it seems to work but the only problem is that the task keeps running forever as long as the terminal is in nix-shell, it terminates when I call to exit the nix-shell. Is there a way set this task to terminate when the terminal enters in nix-shell? It seems something that should be simple but I couldn’t find a solution.

PS: I am aware of direnv e the extension nix-env-selector but what I want is something simple and I would like to know if it is possible to do this only using tasks in vscode.

Thanks.

This is the contents of the tasks.json file, the file ambientenix.sh just call “nix-shell”:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Ambiente Nix",
            "type": "shell",
            "command": "./scripts/ambientenix.sh",
            "group": "none",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "runOptions": {
                "runOn": "folderOpen", 
                "instanceLimit": 1
            } 
        }
    ]
}

There’s direnv project which automates “call nix-shell when entering a directory”. I think it is well supported by VS Code: direnv - Visual Studio Marketplace. Perhaps that can work for you?

1 Like

Yes, I tried direnv but didn’t quite like it (for small reasons I admit). direnv it was a little bit slower than just call nix-shell directly, also direnv was a little bit verbose and it has the same visual of the shell name, with direnv the nix-shell seems just like the ordinary shell and is not displayed in green, I like to imediately see what shell I am using just by the colors. But, yes, although not satisfactory, at least direnv could be a solution.

1 Like