Homebrew bottle builder and tap manager for OCaml monorepos
README.md

bottler #

Homebrew bottle builder and tap manager for OCaml monorepos.

Overview #

Homebrew installs a package from a formula, a Ruby file that says where the package comes from and how to install it. A bottle is a prebuilt binary of that package for one platform, a gzipped tar archive that brew downloads instead of compiling. A tap is a git repository of formulae that brew tap adds to the local list. Shipping an OCaml binary through Homebrew takes several steps that each fail in their own way: build a bottle per platform, upload it somewhere brew can download it, write a formula that carries the bottle's SHA-256, and push that formula to a tap. Bottler runs all of them from one homebrew.yml at the root of a dune monorepo. It builds the bottles, uploads them to S3-compatible storage (Scaleway), checks that the store serves exactly the bytes it uploaded, writes the formulae and pushes the tap.

The formula and tap primitives come from the ocaml-homebrew library. Bottler adds the parts specific to a monorepo: the dune release build, the YAML configuration, the upload and the GitHub Actions workflow that runs a release in CI.

Bottles are byte-reproducible for a given release version. Bottler writes the tar archive itself, with fixed metadata (mtime 0, uid and gid 0, empty user and group names), and compresses it at level 9 through zlib's gzip wrapper, which writes a header with modification time 0 and no file name; the one header byte that varies, zlib's operating-system code, is constant for one platform. No external tar or gzip is involved. The release version is part of the archived path (NAME/VERSION/bin/NAME) and of the binary's --version output, so two builds give the same bytes when they share a version: a fixed PROJECT_VERSION, or the same commit built on the same day (see Configuration).

Installation #

Install with opam:

$ opam install bottler

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install bottler

Usage #

$ bottler config          # Show parsed homebrew.yml
$ bottler build           # Build bottles for build.platforms
$ bottler upload          # Upload bottles to S3
$ bottler formula NAME    # Emit a Ruby formula
$ bottler release         # Full pipeline: build + upload + tap push
$ bottler workflow        # Generate GitHub Actions release workflow
$ bottler doctor          # Verify credentials and connectivity
$ bottler login           # Set up Scaleway credentials

bottler release is the command most people run. It builds and uploads the bottles, reads each one back from the store and compares its digest before any formula is written, then pushes the tap. When several homebrew.yml sources share one tap and another release pushes first, bottler resets its checkout to the new tip, writes its formulae again and retries.

The other commands look after the tap and the bucket. bottler install installs or upgrades packages from the tap, and bottler install --local builds bottles from the working tree and installs them without uploading anything. bottler list shows every formula in the tap and bottler remove NAME deletes one. bottler gc deletes old bottle versions from the bucket, bottler usage reports how much space it holds, and bottler keys provisions upload-only Scaleway credentials for CI. bottler init writes the bin/version helper that makes a binary report the release version from --version.

Configuration #

Bottler reads homebrew.yml at the project root:

handle: samoht
storage:
  bucket: homebrew-bottles
  region: fr-par
  profile: homebrew-samoht
tap:
  clone_url: https://github.com/samoht/homebrew-tap.git
packages: [merlint, prune]

The release switch comes from the tree. A release builds in a dune context of its own, an opam context on the release switch under the release profile, which the tree declares in dune-workspace.release:

(lang dune 3.0)

(context
 (opam
  (switch lto-5.4.1)
  (name release)
  (profile release)))

A manifest beside this workspace omits build.switch; a duplicate declaration is refused.

Bottler builds each target as ./_build/release/<target> through that workspace, so the release builds in _build/release beside _build/default and never rebuilds the default context under the release profile. It builds with --cache=disabled, which wins over a caller's DUNE_CACHE, so a bottle holds nothing restored from dune's shared cache. The workspace declares the switch once; the manifest names the checks the release runs:

build:
  checks: [switch-pins]

A tree with no dune-workspace.release sets build.switch; bottler writes a workspace declaring the release context on that switch. A tree with a dune-workspace of its own supplies dune-workspace.release, so bottler can use its build configuration. bottler config prints the switch with where it came from.

A package is its name when the tree says the rest. Its target is the dune executable whose public_name is that name, found by reading the tree's dune files as dune does, and its description is the synopsis of the opam package that installs the executable: the (package P) of its stanza, or its project's only package, read from P.opam, or from the dune-project when there is no such file. The object form sets what the tree does not say or says otherwise, beside the per-package keys:

packages:
  - merlint
  - name: mq                      # a binary no public_name installs as mq
    target: ocaml-merge-queue/bin/main.exe
    description: "Local merge queue"
    companions:
      - name: daemon-supervisor
        target: ocaml-daemon/bin/supervisor.exe

A name that no executable or several are installed under is refused with the candidates and a request to set target. tap.push_url is the SSH form of tap.clone_url on github.com and gitlab.com, and git@KNOT:HANDLE/REPO for a tangled.org clone URL on the knot tap.knot names; another forge needs tap.push_url. bottler config prints each of these values with where it came from.

build:
  linux: static          # or linuxbrew
  checks: [switch-pins]  # root aliases the release build also builds
  platforms: [arm64_sonoma, x86_64_linux, arm64_linux]
  image: ocaml/opam:alpine-ocaml-5.4@sha256:8c7649db5d344300b493e045e8110ddc68de55cf6819887a358db7e49a1f400a

build.platforms lists the bottles a release builds, every one in the same release so that the formula carries them all; without it, a release builds the host's platform alone. The host builds its own platform. With build.linux: static, a Linux platform is built in a container of build.image with Docker, for that platform's architecture, from any host: the tree is copied in, the release switch made there as below, and the release build linked with -ccopt -static -no-pie in the release context. These options are declared in the context's env-vars in a generated workspace copy, so dune keys each action on them. The binary runs on any Linux distribution and can be shipped outside Homebrew too. build.image is pinned by digest, REPOSITORY[:TAG]@sha256:HEX, and a tag alone is refused, since a tag moves and two hosts would build in two userlands; there is no default. The container's opam root is a named volume, one per image digest, platform and switch, that the next release reuses, so the switch is built once and moves with the lock after; a stock image serves. bottler build and bottler release take --worker NAME, a worker of the merge queue: a platform the worker's machine builds natively builds there, through mq worker run on the commit HEAD names, a Linux one in the same container and a macOS one on a Mac of its architecture with no container, and the binaries come back on its output with their digests. doc/runners.md says where each platform builds and why. A macOS platform builds only on a Mac of its architecture, this host or a worker, and a linuxbrew bottle only on its Linux machine.

bottler build and bottler release make the release switch before they build, from the tree's root opam files, as the merge queue's workers make theirs: root.opam and its lock root.opam.locked, whose pin-depends name every pin. Where opam has no such switch it is created empty; each release then runs opam install --deps-only --locked of the root into it, so the switch follows the lock and the switch-pins check holds. The same steps run on the host, in a Linux container and on a CI runner; bottler config lists the files the switch is made from.

A tree whose release links with a compiler its other switches do not hold, such as one with -use-lto, names an opam file that pins that compiler, under the version the lock binds, and depends on it and on its options:

build:
  compiler: scripts/release-compiler/opam
  checks: [switch-pins]

Each release installs that file's dependencies into the switch before the lock (opam install --deps-only scripts/release-compiler/opam), so the lock's install keeps that build of the compiler, and a move of its pin rebuilds the release switch alone. Without the key the switch takes the lock's compiler.

A tree released to opam cannot put -use-lto in its own dune files, since a stock compiler rejects the flag: build.lto: true links its release with the flag through a workspace file of bottler's, and needs a build.compiler file, or root opam files, that name a compiler with it. A tree with its own dune-workspace turns the flag on there instead. bottler workflow generates a GitHub Actions workflow that builds each platform on a runner of its own with bottler build --platform, uploads the bottles, and writes the formulas into the tap the workflow runs in. Its dispatch names the commit to release, 40 hexadecimal digits, and every job fetches that commit alone and builds bottler from it, so the bottles, the formulas and the release version name one revision; a tree that carries no bottler/bin/dune is refused. Every action is pinned by its commit and every container by its digest, the token reads the repository, and only the job that pushes the tap may write it.

A macOS bottle is labelled arm64_sonoma or sonoma, so brew pours it on macOS 14 and later, the macOS Homebrew's sonoma names. A package built on a later macOS records that release in its archives, and a compiler configured there uses the functions it found, such as pipe2 and dup3 on macOS 27, so the binary would not start on macOS 14. The release switch for a macOS bottle carries, in its own setenv, MACOSX_DEPLOYMENT_TARGET = "14.0" and ac_cv_func_pipe2 = "no", ac_cv_func_dup3 = "no": opam applies them to every package it builds there, so a later pin or lock move keeps the target. A package built before is found by its archives, mapped to its package through opam's .changes files, and reinstalled alone, never the whole switch; an archive still recording a later macOS after that refuses the build, naming it and its package. A C library a release links statically is built there too, from opam's source package, after the lock: Homebrew's copy records the macOS of the machine that built it, a later one than the label's. Those are gmp, opam's source package, and libzstd and libbrotli, each built from its release tarball with its checksum, from the opam overlay git+https://tangled.org/gazagnaire.org/opam-overlay.git, which the switch's repositories select. The tree's release link takes them from the switch before any Homebrew directory. The bottle's own build runs with the same environment. Each binary is checked before it is bottled: it must record no later minimum macOS, and import no function weakly, which is how a function later than the deployment target is linked.

The release profile links with -use-lto. LTO is link-time optimisation: the compiler keeps an intermediate form of every module and optimises across modules when the program is linked. The stock OCaml compiler has no -use-lto flag and refuses it; only a compiler built with that extension accepts it, so the release context names a switch whose compiler has it, usually not the switch used for day-to-day development. bottler doctor changes nothing: it says whether the switch exists, whether its ocamlopt accepts -use-lto, and what a release would do to it.

A build owns the terminal. dune, opam, the linker and the container print to their own standard output and error; each line goes to the release's log, <build_dir>/<version>.log, which the build row names, and on a terminal to a window under the row of the step that ran it: muted, its latest lines alone, rolling as they come. A step that succeeds collapses to its row, which names the bottle it wrote; one that fails keeps its window, and the refusal gives the error lines dune printed and the log's path. Off a terminal, each row is a line when it starts and one when it ends, and the children's lines stay in the log. -v prints them above the rows as they come.

build.checks names root-directory dune aliases the release build builds beside the first package's binaries, as @@_build/release/<alias> in the same command. A failing check fails the build, so no bottle is made, and its output says why. The monorepo names switch-pins, which refuses a release switch whose pins or versions drifted from its lock, root.opam.locked; no other build depends on that alias.

Each package's primary binary must report the release version from --version, and bottler checks this with PROJECT_VERSION cleared from the environment. The packages are built one at a time and each primary is checked before the next is built, so a binary that reports another version stops the release at its own build. Companion binaries are built and bottled at the same version, then installed beside the primary binary by source-built formulae. The release version is PROJECT_VERSION when it is set, and otherwise YYYYMMDD-<commit> from git HEAD, with -dirty appended when the tree has uncommitted changes. The date is the build machine's local date at build time, not the commit date.

Bottler refuses a configuration it cannot use before building anything. A missing file, a missing section or key, and a tree with no release switch each produce an error that names the file it resolved against the current directory, what is absent, and the lines to add or the file to declare it in. An unknown key is an error too, so a misspelt field cannot silently drop out of a formula.

Licence #

ISC License. See LICENSE.md for details.