[ANN] onix 0.0.5

Hi all! It is my pleasure to announce a project I have been working on and using for some time.

Onix is a tool for building OCaml projects with Nix. Onix provides an alternative to opam and keeps full compatibility with opam repository and opam files.

Features

  • Fully hermetic and deterministic builds based on a lock file. The lockfile includes precise package versions generated from your opam files, source hashes and the Git revision of the opam repository used to perform the resolution. See an example here.
  • Robust cross-project cache powered by Nix store.
  • Support for pin-depends to add packages outside of the opam repository (this can be used to test experimental packages or pull requests, for example).
  • Support for automated depexts installation from nixpkgs.
  • Conditional compilation of with-test, with-doc and with-dev-setup dependencies. In particular to support local development workflows with ocamlformat, ocaml-lsp-server, etc.
  • Support for compiler variants similar to opam (for example, the flambda compiler can be built).
  • Compilation of locally vendored packages. I use this workflow to test patches to project dependencies before submitting PRs.
  • Generation of opam-compatible “locked” files. This way any onix project can also be used directly with opam.

Motivation

On a more personal note, I started working on onix out of frustration with the existing tools like opam, esy, monorepo, opam2nix and opam-nix. Partially because the UX for working with local projects in a reproducible way is not great, and partially because I had a need to precisely manage system dependencies which is only possible with Nix. In the end, I decided to build something that is highly flexible and yet requires almost no Nix knowledge and (hopefully) is easy to use. Even though there are still some rough edges, I’m currently using onix in almost all of my OCaml projects.

Dune-compatibility and future work

I know there is work being done to include lock-file generation and source fetching in dune and I hope there will be a way to find compatibility between the onix lock format and the dune lock format. I do not think that this new direction in dune will eliminate the need for onix because dune will not manage system dependencies in the same deterministic way as Nix. For instance, in the Rust community there is an abundance of nix-based tools to work with Cargo’s lock-files. I’m very excited to see how dune solves this problem and want to keep onix in sync with this effort.

There are some other areas that I’d like to explore in the future that haven’t been a priority for me, namely:

Final thoughts

Note that this release is not published on opam as currently the workflow is fully driven by Nix. So follow the instructions in the README if you would like to give onix a try. There is also a dedicated repository with examples to showcase more advanced use cases: GitHub - rizo/onix-examples.

Hope someone finds onix useful and happy to answer any questions.

Happy building! :camel:

30 Likes

Nice.

Quick question, with regards to OCaml compiler and packages, does onyx get it from opam repo and not the nixpkgs? If so, how to do I use the OCaml 5.1 rc1? Like below perhaps ?

in onix.env {
  path = ./.;
  deps = { "ocaml" = "5.1.0~rc1"; };
}

Hmmm … perhpas this https://github.com/rizo/onix-examples/blob/master/ocaml-variants/default.nix ?

Hi @BikalGurung. Onix works with the opam repository, but can also use the ocaml compiler package from nixpkgs when an equivalent version is available.

Here are some additional considerations:

  • The nixpkgs repository will always lag behind opam repository so recent ocaml compiler versions will not be available;
  • The ocaml package on opam is a virtual package and requires that a concrete compiler package is installed: ocaml-system, ocaml-variants or ocaml-base-compiler.
    • For most users this should be ocaml-base-compiler since that’s what opam uses by default.
    • If you want to build a custom compiler variant, use ocaml-variants.
    • If the compiler version you wish to use is on nixpkgs, feel free to use ocaml-system to avoid building the compiler from source.
  • The combinations of versions and options for the different compiler variants offer a lot of flexibility but can be a bit hard to understand. Check the versions and dependencies for each compiler package if you have very specific needs.

Note that there is no ocaml package with version 5.1.0~rc1, but there is a ocaml-base-compiler package with that version. So, in short, you should try:

onix.env {
  path = ./.;
  deps = { "ocaml-base-compiler" = "5.1.0~rc1"; };
}

I have updated the instructions in the README to make this a little bit clearer: GitHub - rizo/onix: Build OCaml projects with Nix.

3 Likes

As a Nixer dipping their toes in the OCaml ecosystem, this is very cool!

Two things:

  • would it be possible to add a comparison with the other existing tools in the OCaml → Nix space?
  • Do you plan on announcing this on discourse.nixos.org?

All the best!

2 Likes