[ANN] opam2nix (v1)

Anouncing opam2nix (v1)

opam2nix generates nix expressions from the opam OCaml package repository. It works similarly to bundix, node2nix, etc:

You run an (impure) command to resolve all transitive dependency versions using the current opam repository, generating a .nix file that locks down the exact package sources and versions. Then this file can be imported to provide buildInputs for building your ocaml project in nix.

What is nix and why would I care? Well, that’s a long story but the headline benefits of nix are:

  • reproducible builds (if it builds for me, it builds for you)
  • stateless (you don’t set up switches and then install packages, each expression specifies everything it needs, and anything you don’t have is fetched/built on demand)
  • language agnostic (takes care of non-ocaml dependencies)

It’s sadly not a shallow learning curve, but those benefits are hard to find elsewhere, so I obviously think it’s worthwhile. So if you use nix (or would like to), please give it a try and provide feedback. I’ll (slowly) start working on upstreaming it into nixpkgs.


This is exciting! I’m keen to try this for reproducible builds in particular.

Did you encounter any hardships with wrapping opam for this, or interpreting depexts? Please do file upstream issues if future versions can be simplified for this usecase.

Thanks, will do. Overall I was super happy with how reusable opam’s internals were, it took a while to get familiar with some of the more complex parts (formulas are way more powerful than I knew from using opam), but it could have been much harder.

The main difficulty I found was that anything tied to OpamState had to be reimplemented, because that assumes certain things which aren’t true in opam2nix (e.g. there’s no one lib directory, there’s one per dependency). I’ll detail the exact issues in github though :+1:

feedback reported: https://github.com/ocaml/opam/issues/4030

Guix is more suitable for reproducible and bootstrappable builds because of their efforts with creating binary “seeds”

Guix does have a more pure bootstrap story, true. But both should be equivalent in terms of reproducibility. If you use the default binary cache, you’re sidestepping the bootstrap process anyway.

1 Like