Corrupted compiled interface after `dune build`

Hey, I’m using VSCode and seeing an error when using the OCaml extension on my files: I’m trying to run the OCaml version into v5.1.1.

Corrupted compiled interface ~/.opam/5.1.1/lib/ocaml/stdlib.cmi

In case, I can run the dune build without any problem and it can be executed, but I’m trying to understand why it’s happening? Any idea why?

BTW, I’m using macOS + Nix Flakes to run this OCaml project. I put here how my flake.nix has been wrote:

{
  description = "A development environment for ocaml";

  inputs = {
    nixpkgs.url = "github:nix-ocaml/nix-overlays";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages."${system}".extend (self: super: {
          ocamlPackages = super.ocaml-ng.ocamlPackages_5_1;
        });
        ocamlPackages = pkgs.ocamlPackages;
        packages = [
          ocamlPackages.brr
          ocamlPackages.utop
          ocamlPackages.ocamlformat
          ocamlPackages.ocaml
          ocamlPackages.dune
          ocamlPackages.opam
          ocamlPackages.ocaml-lsp
          ocamlPackages.merlin
        ];
      in
      {
        formatter = nixpkgs.legacyPackages.x86_64-linux.nixpkgs-fmt;
        defaultPackage = pkgs.stdenv.mkDerivation {
          name = "ocamlbyexample";
          src = ./.;
        };

        devShell = pkgs.mkShell {
          nativeBuildInputs = with pkgs.ocamlPackages; [ cppo findlib ];

          buildInputs = with pkgs; [
            packages
            caddy
            curl
            wget
          ];

          shellHook = ''
            export OPAMCURL="curl"
          '';
        };
      }
    );
}

Any idea how to debug it and why it’s happening?


I think that can have some relationship with my setup, because when I run the opam install ppx_inline_test, it display a similar error related to:

# File "src/sexp.mli", line 1:
# Error: The files ~/.opam/5.1.1/lib/ocaml/stdlib.cmi
#        and /nix/store/hash-ocaml5.1.1-sexplib0-0.17.0/lib/ocaml/5.1.1/site-lib/sexplib0/sexplib0.cmi
#        make inconsistent assumptions over interface Stdlib

It appears that you are trying to mix OCaml libraries installed system-wide with Nix, with an OCaml compiler available in an opam switch.
The opam switch should probably be created with a system compiler in that case, instead of building a compiler anew (even if it is the same version, there could be other subtle differences between the 2 builds that might result in different interface hashes).

P.S. might also be useful to upgrade to 5.2 while you are at it.

1 Like

It makes a lot of sense in general. But do you know exactly how to approach on that to fix?

In case, I don’t know exactly how can I approach to understand how could I ignore the system-wide version (maybe) of this compiler version. And in case, seeing the libe that I want to install, it seems to be related to 5.1.1.

Theoretically, it should work, no? Or I missing some point at all?

The opam-installed switch and its compiler are in the picture because their environment variables have been set, for example by entering the command eval $(opam env). On some installations this command is executed automatically on every new shell. If you want to use only the Nix installation of the compiler and libraries, you will need to find a way to disable the opam environment variables.

The manual way to do that would be to run opam env, look at the output to see what environment variables are being set, then unset those, then start your Nix shell.