Using ocaml + nix flakes with VSCode?

Heya!

I’m trying to get visual studio code to work with my nix flakes to create a reusable setup. I’ve installed and set up a nix flake that works within the terminal with neovim.

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    opam-nix.url = "github:tweag/opam-nix";
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.follows = "opam-nix/nixpkgs";
  };
  outputs = { self, flake-utils, opam-nix, nixpkgs }@inputs:
    let package = "my_package";
    in flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        on = opam-nix.lib.${system};
        devPackagesQuery = {
          # You can add "development" packages here. They will get added to the devShell automatically.
          ocaml-lsp-server = "*";
          ocamlformat = "*";
          ocamlfind = "1.9.5";
          utop = "*";
        };
        query = devPackagesQuery // {
          ## You can force versions of certain packages here, e.g:
          ## - force the ocaml compiler to be taken from opam-repository:
          ocaml-base-compiler = "*";
          ## - or force the compiler to be taken from nixpkgs and be a certain version:
          # ocaml-system = "4.14.0";
          ## - or force ocamlfind to be a certain version:
        };
        scope = on.buildOpamProject' { } ./. query;
        overlay = final: prev: {
          # You can add overrides here
          ${package} = prev.${package}.overrideAttrs (_: {
            # Prevent the ocaml dependencies from leaking into dependent environments
            doNixSupport = false;
          });
        };
        scope' = scope.overrideScope' overlay;
        # The main package containing the executable
        main = scope'.${package};
        # Packages from devPackagesQuery
        devPackages = builtins.attrValues
          (pkgs.lib.getAttrs (builtins.attrNames devPackagesQuery) scope');
      in {
        legacyPackages = scope';

        packages.default = main;

        devShells.default = pkgs.mkShell {
          inputsFrom = [ main ];
          buildInputs = devPackages ++ [
            # You can add packages from nixpkgs here
            pkgs."gmp"
            pkgs."libev"
            pkgs."openssl"
            pkgs."libargon2"
          ];
        };
      });
}

Now I want to get it up with vscode so my colleagues can contribute. I installed nix extension pack and the ocaml platform on vscode, the terminal correctly connects with direnv, where I can run dune build etc. However, the ocaml platform vscode extension fails with

[Error - 13:41:12] Client OCaml Platform VS Code extension: connection to server is erroring.
write EPIPE
Shutting down server.
[Error - 13:41:12] Client OCaml Platform VS Code extension: connection to server is erroring.
write EPIPE
[Error - 13:41:12] Stopping server failed
Error: Client is not running and can't be stopped. It's current state is: starting
	at LanguageClient.shutdown (/home/marcc/.vscode-oss/extensions/ocamllabs.ocaml-platform-1.14.1/dist/vscode_ocaml_platform.bc.js:41:11715)
	at LanguageClient.stop (/home/marcc/.vscode-oss/extensions/ocamllabs.ocaml-platform-1.14.1/dist/vscode_ocaml_platform.bc.js:41:11213)
	at LanguageClient.stop (/home/marcc/.vscode-oss/extensions/ocamllabs.ocaml-platform-1.14.1/dist/vscode_ocaml_platform.bc.js:44:54761)
	at LanguageClient.handleConnectionError (/home/marcc/.vscode-oss/extensions/ocamllabs.ocaml-platform-1.14.1/dist/vscode_ocaml_platform.bc.js:43:75)
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[Error - 13:41:12] Server initialization failed.
  Message: write EPIPE
  Code: -32099 
[Error - 13:41:12] OCaml Platform VS Code extension client: couldn't create connection to server.
  Message: write EPIPE
  Code: -32099 
[Info  - 13:41:12] Connection to server got closed. Server will restart.
true
/bin/sh: 1: ocamllsp: not found
[Error - 13:41:12] Server process exited with code 127.
[Error - 13:41:12] Server process exited with code 127.
[Error - 13:41:12] Client OCaml Platform VS Code extension: connection to server is erroring.
Cannot call write after a stream was destroyed
[Error - 13:41:12] Server initialization failed.
  Message: Cannot call write after a stream was destroyed
  Code: -32099 

And the ocaml commands outputs

$ ocamlc --version
5.1.1~rc1

$ ocamllsp --version
1.16.2

$ /nix/store/nlzx3jjirr9aqwbb1cq0vkrz3dn2h0n7-opam/bin/opam --version
$ /nix/store/nlzx3jjirr9aqwbb1cq0vkrz3dn2h0n7-opam/bin/opam --version
2.0.0
2.0.0

$ /nix/store/nlzx3jjirr9aqwbb1cq0vkrz3dn2h0n7-opam/bin/opam var root

$ /nix/store/nlzx3jjirr9aqwbb1cq0vkrz3dn2h0n7-opam/bin/opam var root
/tmp/opam/tmp/opam

So not quite sure where to go from here. I’d love it if people can get their dev environment up and running with nix but that requires them to be able to use it with vscode.

Do you have the direnv vscode extension? It can help setup paths.