# How are Ocaml packages managed in nixpkgs?

**URL:** <https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100>\
**Category:** Learning\
**Tags:** nix, devenv\
**Created:** [February 8, 2025, 5:08am UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100 "2025-02-08T05:08:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blurrcat](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/blurrcat/32/5789_2.png) [@blurrcat](https://discuss.ocaml.org/u/blurrcat)\
**Post date:** [February 8, 2025, 5:08am UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/1 "2025-02-08T05:08:41Z")

</div>

Hello,

I’m new to OCaml and I’ve been using [devenv](https://devenv.sh/) 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](https://github.com/blurrcat/aoc).

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 ☹  
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!

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 9, 2025, 8:21pm UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/2 "2025-02-09T20:21:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blurrcat](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/blurrcat/32/5789_2.png) [@blurrcat](https://discuss.ocaml.org/u/blurrcat)\
**Post date:** [February 10, 2025, 9:00pm UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/3 "2025-02-10T21:00:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 11, 2025, 3:15am UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/4 "2025-02-11T03:15:38Z")

</div>

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](https://github.com/NixOS/nixpkgs/commits/master/pkgs/development/ocaml-modules)

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

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [February 11, 2025, 3:08pm UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/5 "2025-02-11T15:08:57Z")

</div>

You may be interested in [Developing OCaml with Nix](https://ryan.freumh.org/ocaml-nix.html) 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](https://preview.dune.build/) 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?](https://discuss.ocaml.org/t/opam-and-nixos-is-there-an-alternative-to-nix-shell/13726).

---

<div class="post-metadata">

**Author:** ![kit-ty-kate](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kit-ty-kate/32/5953_2.png) [@kit-ty-kate](https://discuss.ocaml.org/u/kit-ty-kate)\
**Post date:** [February 11, 2025, 5:06pm UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/6 "2025-02-11T17:06:38Z")

</div>

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

> <https://github.com/ocaml/opam/pull/5982>
>
> Nix doesn't install packages in the traditional sense -- instead it puts them in… a store and makes them available through environment variables. I.e., \`nix-shell -p gcc\` will drop us into a shell with gcc in the \`$PATH\`.
> 
> We can set appropriate environment variables with Opam to make system dependencies (depexts) available with Nix.
> Similar to how \`nix-shell\` works under the hood, we create a Nix derivation such as
> \`\`\`
> { pkgs ? import \<nixpkgs\> {} }:
> with pkgs;
> let
> packages = \[gmp \];
> inputs = with buildPackages; packages ++ \[pkg-config \];
> in
> stdenv.mkDerivation {
> name = "opam-nix-env";
> nativeBuildInputs = inputs;
> 
> phases = \["buildPhase" \];
> 
> buildPhase = ''
> vars=("NIX\_CC" "NIX\_CC\_FLAGS" "NIX\_CFLAGS\_COMPILE" "NIX\_CC\_WRAPPER\_TARGET\_HOST\_x86\_64\_unknown\_linux\_gnu" "NIX\_LDFLAGS" "PKG\_CONFIG\_PATH")
> for var in "''${vars\[@\]}"; do
> escaped="$(echo "''${!var}" | sed -e 's/^$/@/' -e 's/ /\\\\ /g')"
> echo "$var = $escaped Nix" \>\> $out
> done
> echo "PATH = $PATH Nix"
> '';
> 
> preferLocalBuild = true;
> }
> \`\`\`
> 
> Which we can build to output a file with environment variables that make depexts available, in Opam's environment variable format. This file is a Nix store root, so it's dependencies won't be garbage collected by Nix until the file is removed.
> 
> This approach came from conversations with @dra27 and is distinct from previous approaches in that it supports providing development environment, which imperative installations with Nix don't https://github.com/ocaml/opam/pull/5332#issuecomment-1710409758; and doesn't require the user to manage the environment outside of Opam, which would lead to a different workflow https://github.com/ocaml/opam/pull/5942.
> 
> Initial experiments were done using a package to set such environment variables https://github.com/RyanGibb/nix.opam.
> However, in order to work with generic depexts (as opposed to just conf packages), to avoid cyclic dependencies (if conf packages depend on nix.opam but nix.opam depends on conf packages), and due to opam package sandboxing (nix derivation fetching would require opam sandboxing to be disabled or the nix store to be 'primed'), it's better implemented as a depext mechanism.
> 
> This has been tested and successfully creates an environment to provide packages \`conf-gmp\` and \`conf-libseccomp\`, as well as depexts directly.

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

---

<div class="post-metadata">

**Author:** ![adamcstephens](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/adamcstephens/32/5089_2.png) [@adamcstephens](https://discuss.ocaml.org/u/adamcstephens)\
**Post date:** [February 12, 2025, 12:21am UTC](https://discuss.ocaml.org/t/how-are-ocaml-packages-managed-in-nixpkgs/16100/7 "2025-02-12T00:21:56Z")

</div>

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.
