tools_opam%opam Last updated Mar 18, 2025

opam = use_extension("@tools_opam//extensions:opam.bzl", "opam") opam.deps(debug, ocaml_version, opam_verbosity, opam_version, pkgs, toolchain, verbosity)

This module extension enables seamless integration of opam dependencies into the Bazel environment.

Methods

deps

The deps method (tag class) runs code that

  1. ensures the selected opam switch is properly configured, and

  2. generates a Bazel repository for each opam package listed in the pkgs attribute

The Bazel repository for package pkg will be named opam.pkg, so in your BUILD.bazel files you refer to it as @opam.pkg//lib.

The Bazel repositories are lazily evaluated. The method will configure a repository for every package in the complete dependency graph - that is, generate Bazel files for it and register it with Bazel. But repositories are not ``materialized'' (evaluated) until needed. So your project might have many opam dependencies, but if you build a target that only depends on one, then only that one repository will actually be constructed and used.

Usage example
MODULE.bazel
bazel_dep(name = "tools_opam", version = "1.0.0")
opam = use_extension("@tools_opam//extensions:opam.bzl", "opam")
opam.deps(pkgs = {"ounit2": "2.2.7"})
use_repo(opam, "opam.ounit2")
use_repo(opam, "opam", "opam.ocamlsdk")                               (1)
register_toolchains("@opam.ocamlsdk//toolchain/selectors/local:all")  (2)
register_toolchains("@opam.ocamlsdk//toolchain/profiles:all")         (2)
1 Modules opam and opam.ocamlsdk are always implicitly configured.
2 The toolchains defined by module opam.ocamlsdk must always be registered.
BUILD.bazel
ocaml_module(name="A", struct="a.ml", deps=["@opam.ounit2//lib"],...)

Attributes

Name

Description

Type

Mandatory

Default

debug

Debug level

Integer

optional

0

ocaml_version

Version of OCaml

String

optional

"5.3.0"

opam_verbosity

Verbosity level for opam commands

Integer

optional

0

opam_version

Version of opam to use for xdg toolchain

String

optional

"2.3.0"

pkgs

List of opam pkg Ids

Dictionary: String -> String

optional

{}

toolchain

opam toolchain: xdg, local, or global

String

optional

"xdg"

verbosity

Verbosity level

Integer

optional

0