Using direnv, nix shell and VSCode

Usually I’m write Haskell code in Emacs and I have set ups where I use a nix shell to pull in development tools, at least GHC and HLS, then I use direnv to get access to the tools automatically. I’m using the direnv package in Emacs to make the tools available from there as well, in particular lsp-mode can find and start the HLS.

Sometimes at work I’d like to use VSCode instead of Emacs. (When collaborating with colleagues.) But I’ve not managed to get a similar setup to work in VSCode. I’ve installed the Haskell extension, the direnv extension, but still, when opening a project with such a setup I’m told the required tools aren’t available and need to be installed, starting with cabal. All tools are available in a shell terminal inside VSCode, just not to the extensions it seems.

What am I missing here?
Has someone else gotten a similar setup to work?
Is what I want even possible in VSCode?

2 Likes

I have experienced that the direnv tooling in VScode is somewhat sluggish sometimes. Other tools are started and complain about missing tools before direnv can do its work. Ignore all missing tools until you are in VScode, then use CTRL+Shift+p to get the command prompt and search for the direnv-reload item in the list. Trigger it and then force a reload of the window or even close VScode and reopen it.

This is at least the necessary process whenever I did changes in my python and go based environments.

Also, it really helps if you start VScode from the already activated environment, like code .

Not sure if it’s quite what you want but there’s also this extension:

1 Like

The problem is that the direnv won’t necessarily be loaded before the the Haskell extension. When it’s not (and sometimes it seems like usually not), the extension won’t find the binaries it needs in its PATH. It would be nice if one could specify a load order for extensions, but that’s not possible via configuration. Only extensions can specify their dependencies currently. I use a workaround in my config to do that, modifying the extension to take direnv as a dependency.

1 Like

Yes, I expect it to work if VSCode is started from inside a nix shell, but then that removes a lot of the flexibility, I think. Also I expect i wouldn’t need any direnv/nix extension then. It’s a fallback option I think. Thanks for pointing it out though.

Ah, nice. I haven’t tried this one yet. The web page l instills some hope :blush:

I’ll report back once I’ve tried it out.

Unfortunately the extension Nix Environment Selector - Visual Studio Marketplace doesn’t seem to work; it doesn’t stop “Applying environment” and the Haskell extension still wants to install cabal :disappointed: .

How about VSCode · direnv/direnv Wiki · GitHub

Are there any errors in the log?

I’m asking since I do have some issues with vscode-fhs myself that (as one of the issues) impact the functionality of the direnv module @peterbecich is recommending.
I’m not experienced enough to troubleshoot this issue, but I am wondering if any of the information in GitHub login on vscode-fhs broken · Issue #207855 · NixOS/nixpkgs · GitHub might help you indentifying if you experience the same issue?

As you might suspect I’ve not been trying very hard to get a working VSCode setup. I don’t know if there is anything in the logs that could have offered a hint at a solution.