timbertson / opam2nix

Generate nix expressions from opam packages
MIT License
93 stars 28 forks source link

Problems (and solutions) for use with the OCaml 5 beta #87

Open talex5 opened 1 year ago

talex5 commented 1 year ago

I'm trying to use opam2nix with OCaml 5.0.0 (beta1), but had a few problems:

First, the --ocaml-version option actually selects a version of ocaml-base-compiler, which is confusing as the version numbers aren't always the same. ocaml-base-compiler.5.0.0~beta1 is used with ocaml.5.0.0.

So first, you need to pass the beta part:

$ dune init project demo
$ cd demo
$ dune build
$ opam2nix resolve --ocaml-version=5.0.0~beta1 ./demo.opam

To try using it, I then added this flake.nix:

{
  inputs = {
    nixpkgs.url = "nixpkgs";

    opam2nix-src = {
      url = "https://github.com/timbertson/opam2nix/archive/v1.tar.gz";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, opam2nix-src }:
  let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    opam2nix = import opam2nix-src { inherit pkgs; };
    ocaml5 = pkgs.ocaml-ng.ocamlPackages_5_0.ocaml;
    selection =
      opam2nix.build {
        ocaml = ocaml5;
        selection = ./opam-selection.nix;
        src = ./.;
      };
  in
  {
    packages.${system}.default = selection.demo;
  };
}

However, that didn't quite work. First, it prints this warning:

trace: warning: Dependencies were selected for ocaml version 5.0.0~beta1 but you are building with 5.0.0-beta1

However, editing the version to match what it wants doesn't work (if you have any dependencies). It looks like opam2nixHooks only adds libraries to OCAMLPATH if there is an ocaml.version directory. Nix reports ocaml.version as 5.0.0-beta1, but the directory is just 5.0.0.

The solution here is to override the version number like this:

    ocaml5 = pkgs.ocaml-ng.ocamlPackages_5_0.ocaml // { version = "5.0.0"; };

The next problem is that it didn't install:

$ nix build . --impure
trace: warning: Dependencies were selected for ocaml version 5.0.0~beta1 but you are building with 5.0.0
[...]
       >  + dune build -p demo -j 8 --promote-install-files=false @install
       >  + dune install -p demo --create-install-files demo
       > Error: The mandir installation directory is unknown.

It's looking for $OPAM_SWITCH_PREFIX to decide where to install the files. This happens when my dune-project used (lang dune 2.9). Changing that to 3.5, it seems to work! (but some of my dependencies use 2.9 too and also fail)

Also, Nix requires opam2nix to be run with --impure. I have an experimental branch at https://github.com/timbertson/opam2nix/compare/v1...talex5:opam2nix:flakes?expand=1 that fixes that.

Hope that's useful to someone!