How are Ocaml packages managed in nixpkgs?

Hello,

I’m new to OCaml and I’ve been using devenv to manage my development environment. devenv builds on top of nix and provides a very smooth developer experience. All I had to do was to set languages.ocaml.enable = true, then it installs everything needed to start a small project: ocaml, lsp, utop, dune, ocamlformat, etc. Thanks to cachix which cahces artifacts for those things, there’s no need to build them locally on my machine so everything gets ready in a few minutes(depending mostly on network speed). In addition, it takes care of common development tasks like scripts, setting up git hooks to run tests and auto-formatting, etc.

All this is to say that devenv does a great job helping me getting started with OCaml, and I had a blast with OCaml doing 2024’s Advent of code.

However there’s one thing preventing me from using it for other projects: not all OCaml packages available from opam is available on nixpkgs. For example, [Dream](https://opam.ocaml.org/packages/dream/) isn’t there :frowning:
It would be great if such modern, well-documented packages can be accessed more widely.

Does anyone know how are OCaml packages managed in nixpkgs? Who maintains them? How are they added to nixpkgs?

Any info is appreciated!

3 Likes

Devenv doesn’t require using Nixpkgs, it also works with language-specific package managers like npm and pip. You can just use devenv to bootstrap the toolchain (dune + opam + etc.) and then use opam to install OCaml packages.

1 Like

That’s one option, but one of the biggest advantages of using OCaml packages from nixpkgs is that you don’t need to build them locally, which results in much faster bootstrap time(and any time you add a dependency).
Since someone is already maintaining OCaml packages, I want to understand how it works and maybe I could help adding the missing packages!

If you just want to understand how the OCaml packages in Nixpkgs are maintained, you could take a look at the git commit history of the subdirectory: History for pkgs/development/ocaml-modules - NixOS/nixpkgs · GitHub

This will also point you to the pull requests where the updates were done and the maintainers who sent and approved/merged the PRs.

1 Like

You may be interested in Developing OCaml with Nix by @ryang. The OCaml packages in Nixpkgs are definitely lagging behind and a lot are missing. This is probably due in part to the lack of maintainers able to put time into it, but also to different dependency resolution styles between opam and Nix: opam supports version constraints and uses a constraint solver, which is quite far from the Nix way of doing things and can cause complications.

The short answer is:

  • use a tool like opam-nix to generate per-project Nix shells. I know that other tools exist that fill a similar role.
  • do it imperatively using opam. This is what I do because I often work on the OCaml compiler and tools themselves and opam-nix isn’t quite designed for that use case.
  • You may want to try the Dune developer preview which is not integrated into Nix but does things in a very Nix-y way: global installation of packages (possibly in several versions) and per-project use of them.

See also Opam and nixos: is there an alternative to nix-shell?.

2 Likes

opam 2.4 (slotted for an April release) should also have a much better Nix support:

I’m not a Nix user but as far as i understand it should allow to transparently use opam on Nix

1 Like

Adding packages should be as simple as opening pull requests to nixpkgs.

The packages already there are probably a mix of dependencies for other nixpkgs packages and people like you who want to use them for their applications. The former use case (deps for others in nixpkgs) is likely what drives or holds back individual ocaml packages.

I’m still learning ocaml, but have worked extensively with nix and nixpkgs. I started off trying to use the nixpkgs ocaml packages, but quickly found many missing packages. I spent time packaging them, and even working to automate the process some, before eventually giving up and exploring the native ocaml ways. Using opam or the new dune package management work better for development purposes.

2 Likes