I’d like to list all the opam dependencies, and their versions, that my project requires. From that list, I’d like to create a dependency graph of all the transitive dependencies and figure out exact versions that will allow me to build the project. Is there a library to do this?
Not a library but opam (CLI) can do it: opam-lock manpage
opam does it in an implicit way based on your own environment, which I’m not happy with. Ideally I want a library that I can use to do that work based on a configuration file (not based on my own switch, for example)
You could try Opam’s 0install solver library. Its documentation, including an example using the API with a local Opam package repository but no switch, is at opam-0install 0.4.3 · OCaml Package
The solvers won’t differentiate between your project’s dependencies and the transitive dependencies pulled in by those. See, for example, the solutions which the ocaml-ci system computes for builds (e.g. for ocurrent’s master branch).
For getting the list of opam dependencies for a Dune project, there’s opam-dune-lint, but that project’s in a little hiatus at the moment because the feature to extract dependencies from dune files was removed in Dune 3 (an alternate feature suitable for use with opam-dune-lint is scoped and could be re-added to Dune, but it’s in need of a contributor!).
Do you need the precise dependency graph? The opam library can provide you with that. The solver is effectively a black box and just replies with a list of packages which will co-install and maximise the constraint criteria given - opam then constructs an actual dependency and from that a parallelisable graph of actions, and these can be manipulated programmatically using the opam-solver and opam-state libraries, but it’s involved.
A related library/tool is
opam-graph - but it’s based on reading an
opam switch export --full file. We use it to generate the dependency graph seen here: Job albatross 2022-05-10 19:53:40Z