How to use buildrules
Install, initialize, build. Configuration and Examples are the next two pages; Why buildrules is the design.
main. The longer text is docs/how.md in the repository.What you need #
- Linux on x86_64 with glibc 2.34 or newer, and
libgcc_s.so.1andlibz.so.1(the Rust compiler loads them; buildrules does not pin them yet). - Buck2. The buildrules repository pins the release it is tested with in
tools/buck2. Newer releases may work; that is not tested here. - Git.
- Either a remote execution farm that speaks the Remote Execution API (REAPI), or the explicit local mode (see say where actions run). Nothing else runs on your machine: the generator is part of the
buildrulesprogram, and nothing needs Python or another interpreter.
Get the command line #
The install script picks the program for your machine, checks its sha256 against the release's SHA256SUMS, and installs it as ~/.buildrules/bin/buildrules. It needs curl, tar and awk, edits no startup file, needs no root, and installs nothing if a check fails:
curl -fsSL https://buildrules.dev/install.sh | shsh site/static/install.sh.Options follow sh -s -- when the script is piped: --version V installs that release instead of the latest, --prefix DIR installs DIR/bin/buildrules instead of under ~/.buildrules, --verify also checks the keyless signature of the tarball with cosign verify-blob (it stops if cosign is not installed), and --releases-url URL reads releases from a mirror. The macOS program is for Apple silicon: on an Intel Mac the script stops and says so before it asks for anything.
Or by hand: a release carries the command line as a static program for three platforms, as a Debian package for the two Linux ones, and a source archive. Pick a release from the releases page. To use a program, unpack the tarball of your platform (linux-x86_64, linux-aarch64, or macos-arm64), check it, and put buildrules on your PATH:
v=0.0.1 # the release you chose
t=linux-x86_64 # your platform
curl -fsSLO "https://github.com/komira-ai/buildrules/releases/download/v$v/buildrules-cli-$v-$t.tar.gz"
curl -fsSLO "https://github.com/komira-ai/buildrules/releases/download/v$v/SHA256SUMS"
sha256sum --check --ignore-missing SHA256SUMS
tar -xzf "buildrules-cli-$v-$t.tar.gz"
"buildrules-cli-$v-$t/buildrules" --version--version prints buildrules <version>, the number of the release. Each tarball carries a build provenance attestation and a signature, checked with gh attestation verify as for the source archive below. The macOS program (Apple silicon: there is no program for an Intel Mac) is not code-signed or notarised, and only the linux x86_64 program is run by the release's user beta. The rules themselves run on a linux x86_64 farm or machine.
Each release also builds the Homebrew formula (a pull request in the organisation's tap) and the Debian packages buildrules_<version>_amd64.deb and ..._arm64.deb, which are files of the release, checked like the tarballs. After setup, with OWNER the organisation that holds the repository:
brew install OWNER/tap/buildrules
sudo apt install buildrules # after adding the repository and its key, RELEASING.mdTo install the package of a release by hand before then: sudo apt install "./buildrules_${v}_amd64.deb" (arm64 on an Arm machine), after the checks above.
Or build the command line from source with Buck2. Check the source archive:
v=0.0.1 # the release you chose
curl -fsSLO "https://github.com/komira-ai/buildrules/releases/download/v$v/buildrules-$v.tar.gz"
curl -fsSLO "https://github.com/komira-ai/buildrules/releases/download/v$v/SHA256SUMS"
sha256sum --check SHA256SUMS
gh attestation verify "buildrules-$v.tar.gz" --repo OWNER/REPO
tar -xzf "buildrules-$v.tar.gz"
cd "buildrules-$v"OWNER/REPO are the two path segments of the repository URL above. The attestation is GitHub build provenance, signed with the release workflow's identity; it proves the archive was built by that workflow from a commit of main. gh is only needed for that line.
Build the program. With no farm, use local mode:
buck2 build -c buildrules_re.mode=local //cli:buildrules --show-output--show-output prints the path of the executable. Put it on your PATH as buildrules. buildrules --help lists the commands and buildrules <command> --help their flags. Exit codes are the same everywhere: 0 ok, 1 failed or stale (the fix is printed), 2 usage. A program built from the source archive prints <MAJOR.MINOR>-dev for --version: only the released tarballs are stamped with the release number.
Initialize your repository #
Run init once, in the repository root. It adds buildrules as a Buck2 cell pinned to one commit. Get the commit of the release tag:
git ls-remote https://github.com/komira-ai/buildrules 'refs/tags/v0.0.1^{}'buildrules init . --git https://github.com/komira-ai/buildrules --commit <the 40-hex commit above>The commit must be 40 hex digits. A branch or tag is refused, because it is not a pin. init writes the files below and never overwrites one that exists:
| File | What |
|---|---|
.buckconfig | cells, execution platform, the pinned buildrules commit, download timeouts |
toolchains/BUCK | one call: rust_toolchains(extra = EXTRA_RUST_VERSIONS) |
toolchains/rust_pins.bzl | the extra Rust versions from buildrules-pins.toml (empty at first; sync rewrites it) |
platforms/BUCK | the execution platform (remote by default) |
.buckconfig.local.example | a template for the farm settings |
rust-toolchain.toml | an exact compiler, if you have none |
Cargo.toml | a virtual workspace with the [workspace.metadata.buildrules] policy, if you have none |
.gitignore | lines for .buckconfig.local and buck-out/ |
Other flags: --path RELPATH instead of --git (buildrules is a local cell inside the repository, as a submodule or a vendored copy unpacked from the archive), --cell NAME (the name of your repository's own cell, default root) and --rust-version N.N.N (default 1.95.0). A root Cargo.toml that has a [package] is refused: the root must be a virtual workspace and your packages live in subdirectories.
Say where actions run #
Buck2 needs somewhere to run actions. buildrules has two modes, chosen by [buildrules_re] mode.
Local mode #
Local mode runs every action on the machine that runs Buck2, with the same pinned toolchains. Use it on a CI runner or without a farm. Pass the option on every command, or write it once in the gitignored .buckconfig.local:
buck2 build -c buildrules_re.mode=local //crates/hello:test[buildrules_re]
mode = localLocal mode is refused if an engine or CAS address or a worker property set is also configured: a machine that has a farm does not run locally.
Remote mode #
Remote mode is the default. It runs every action on a farm. Copy the template and fill it in; the copy is gitignored and must never be committed.
cp .buckconfig.local.example .buckconfig.local[buck2_re_client]
engine_address = <execution endpoint>
action_cache_address = <action cache endpoint>
cas_address = <content-addressable storage endpoint>
instance_name = <instance name>
tls = true
[buildrules_re]
linux_x86_64_properties = OSFamily=Linux,ISA=x86-64,container-image=docker://<image>@sha256:<digest>linux_x86_64_properties is your farm's own worker property set, comma-separated key=value pairs, passed through unchanged. If it is missing, the execution platform fails to load with a message that names the file; nothing falls back to your machine.
.buckconfig.local.example that init writes still says there is no local fallback. Local mode exists; the comment is out of date.Your first build #
buildrules cargo new crates/hello # a package, adopted: BUCK file, metadata, workspace member
buildrules sync # Cargo's files -> the generated table; commit the output
buck2 build //crates/hello:test //crates/hello:debug
buildrules check # offline: the pull-request gatecargo new(andcargo init) creates a package with--vcs noneand adopts it. Without--cargo <path>the Cargo step is skipped and a minimal manifest and source file are written. Add--libfor a library.synclets Cargo resolve (a new dependency updatesCargo.lock; nothing else is upgraded), reads the unit graph Cargo prints for each member, and writesbuildrules.lock.bzl,buildrules.bzl, the rootBUCK(only if absent) andthird_party/buildrules/. It obtains the pinned Cargo by buildingtoolchains//:cargo_<v>with Buck2, so execution must be configured first. The first sync downloads crate sources, so it needs the network; nothing is compiled.buck2 buildof:testruns the tests as build actions. A cached rebuild means they passed on identical inputs. In remote mode, check the summary line:Commands: N (cached: N, remote: N, local: 0).checkexits 1 if the generated files no longer matchCargo.toml,Cargo.lockandrust-toolchain.toml, and printsbuildrules syncas the fix.
Commit everything init and sync wrote except .buckconfig.local.
Build it a second time #
Run the same build again: it should be all cached. Change one source file of a crate and rebuild: only that crate and what depends on it run. That is the per-dependency cache working; on a farm it is shared by every machine and every CI job.
Adding it to CI #
On a GitHub-hosted Ubuntu runner, local mode works with no farm. The buildrules repository's own .github/workflows/ci.yml is a working example: it installs the pinned Buck2, verifies its sha256, and runs every buck2 command with -c buildrules_re.mode=local. Run buildrules check as a separate, cheap step that needs no build.
Reading a failure #
- A message that names a command (
buildrules sync) is telling you the fix. Run it. [buildrules_re] linux_x86_64_propertiesnot set:.buckconfig.localis missing or incomplete, or you meant local mode.STALE:fromcheck: a manifest or lockfile changed since the lastsync. Runsync, commit.- A rule fails at load because the compiler in the table differs from the one buildrules pins: a pin was bumped under the table.
syncrewrites the table; the diff is the bump. Invalid sha256 digest. Expected A, got B: a pinned download's bytes are not the pinned ones. Do not edit the hash to make it pass; find out why the upstream file changed.Timed out while making request: a pinned host dropped a request. Buck2 retries a failed download three times, and the[http]timeoutsinitwrites are longer than its defaults. Consider a mirror (see mirrors).- A test is red: it is a build action, so the output of the failing test is in the build's output. A red test is yours to diagnose; do not edit it green.
Where to go next #
- Configuration: every file,
.buckconfig, the toolchain files, policy, mirrors and CPU levels. - Examples: Rust, Node.js, Zig, C and C++, Mojo, several languages, CPU levels.
- Why buildrules: the design and the trade-offs.
docs/contract.md,docs/pins.mdandRELEASING.mdin the repository: the contract, the pins and mirrors, and how a release is made.