Getting started
This page takes a Rust repository from nothing to a green build, then shows the Node rules. It assumes you know Cargo and have used Buck2 once.
What you need #
- Linux on x86_64. Every command here runs there; other platforms are not supported yet.
- Buck2. The repository pins the release it was tested with in
tools/buck2. - A remote execution farm that speaks the Remote Execution API (REAPI). By default buildrules registers no local execution platform, so without a farm nothing builds unless you opt in to local mode with
-c buildrules_re.mode=local. Remote execution says how to connect a farm and how local mode works. - Git. Nothing else runs on your machine: the generator is part of the
buildrulesprogram, and it needs no interpreter.
1. Get the command line #
A release carries the command line as a tarball for linux x86_64, linux aarch64, macOS arm64 and macOS x86_64 (see Releases): unpack the one for your platform and put buildrules on your PATH. To build it yourself instead, it is a Zig program in the buildrules repository; build it with Buck2 on your farm, from a checkout (or an unpacked release archive) that has its own .buckconfig.local (step 3 below describes that file).
git clone https://github.com/komira-ai/buildrules.git
cd buildrules
buck2 build //cli:buildrules --show-output--show-output prints the path of the executable. Put it on your PATH as buildrules.
2. Initialize your repository #
Run init once in the repository that holds your Rust workspace. It adds buildrules as a Buck2 cell pinned to one commit, so every machine builds with the same rules.
buildrules init . --git https://github.com/komira-ai/buildrules --commit <40-hex commit>The commit must be a full 40-character SHA; a branch or tag is refused, because it is not a pin. init writes these files and never overwrites an existing one:
.buckconfig, which declares the cells, the execution platform and the pinned buildrules commit;toolchains/BUCKandplatforms/BUCK, one call each;.buckconfig.local.example, a template for the farm settings;rust-toolchain.toml, pinning an exact compiler (channel = "1.95.0"), if you have none;- a virtual-workspace
Cargo.tomlwith a[workspace.metadata.buildrules]policy table, if you have none; - lines for
.buckconfig.localandbuck-out/in.gitignore.
Cargo.toml with a [package] is refused; the root must be a virtual workspace, and packages live in subdirectories.3. Connect the farm #
Copy the template and fill in your farm. The copy is gitignored; never commit it.
cp .buckconfig.local.example .buckconfig.local
# edit .buckconfig.local: the REAPI endpoints and the worker propertiesThe settings are explained on Remote execution. A build prints local: 0 in its summary when all actions ran remotely, which is what the default remote mode gives you. In local mode the summary shows local actions instead.
4. Create or adopt a crate #
buildrules cargo new crates/hello --libcargo new and cargo init create a package with Cargo, then adopt it. To use your own Cargo, or the one buildrules pins, pass --cargo <path>; without --cargo the Cargo step is skipped and a minimal manifest and source file are written instead. For a package that already builds with Cargo, run adopt:
buildrules cargo adopt crates/existing # or no path: every workspace memberAdopting adds the package to the workspace members, adds an empty [package.metadata.buildrules] table to its Cargo.toml, and writes its BUCK file:
load("//:buildrules.bzl", "cargo_package")
cargo_package(path = ".")5. Sync #
buildrules syncsync resolves with Cargo (a new dependency updates Cargo.lock; nothing else is upgraded), reads the unit graphs Cargo prints for each package, and writes the generated files: buildrules.lock.bzl, buildrules.bzl, the root BUCK if absent, and third_party/buildrules/. Commit all of them. The first sync downloads crate sources, so it needs the network; nothing is compiled. Run sync again after every change to Cargo.toml, Cargo.lock or rust-toolchain.toml.
6. Build and test #
buck2 build //crates/hello:test //crates/hello:debugEach package gets :lib, :bin (if it has a binary), :debug, :release, :test, :lint, :typecheck, :fmt and one target per action named in the policy. The Rust rules page lists them. Building :test runs the package's tests on the farm, so a cached rebuild means the tests passed on identical inputs.
7. Check in CI #
buildrules checkcheck is offline and read-only. It exits 1 if the generated files no longer match Cargo.toml, Cargo.lock and rust-toolchain.toml, and prints buildrules sync as the fix. Use it as the pull-request gate. Concepts explains why there are two layers of checking.
Using the Node rules #
For a directory with a package.json and a lockfile version 3 package-lock.json, the front door is two commands:
buildrules npm sync .
buck2 build //:typecheck //:test //:buildnpm sync writes buildrules.lock.bzl and buildrules.bzl and, if there is none, a BUCK file with npm_package(path = "."). The targets are the scripts of package.json; the first sync downloads each tarball once to learn its sha256. buildrules npm check is the offline pull-request gate. The Node rules describe the targets and the "buildrules" policy key.
Without the front door, as this site does, you write the BUCK file by hand. You need a package.json, the lockfile, and a pins.lock.bzl generated from the lockfile by kpin, which ships in the buildrules cell:
buck2 run buildrules//rules/kpin -- lock --pins pins.toml --lock pins.lock.bzl --npm package-lock.jsonpins.toml may hold only a comment when you pin nothing but npm packages. lock is offline when the lock is already on disk; the one command that uses the network is kpin update, and it never runs inside a build. Then:
load("@buildrules//rules/node:modules.bzl", "jest_test", "npm_modules", "tsc_check")
load(":pins.lock.bzl", "NPM")
npm_modules(
name = "modules",
lockfile = "package-lock.json",
npm = NPM,
)
tsc_check(
name = "typecheck",
modules = ":modules",
srcs = glob(["src/**", "test/**", "package.json", "tsconfig.json"]),
)
jest_test(
name = "test",
modules = ":modules",
srcs = glob(["src/**", "test/**", "package.json", "tsconfig.json", "jest.config.js"]),
)buck2 build //:typecheck //:testThis is how this site is built; its source has the full example. npm test still works on the same files outside Buck2. The Node rules page lists every rule.
Using the Mojo rules #
A Mojo package is a mopm project: a mopm.toml and a mopm.lock, with a BUCK file that calls mojo_package. Write a mopm-toolchain.toml that names the mopm and Mojo versions, then:
buildrules mojo sync
buck2 build //pkg:ci //pkg:coverage
buildrules mojo checkmopm 0.0.48, the published release, is the mopm that buildrules pins; pinning mopm says how to use it or your own.
If something is red #
- An error that names a command (
buildrules sync) is telling you the fix; run it. - An error saying
[buildrules_re] linux_x86_64_propertiesis not set means.buckconfig.localis missing or incomplete. See Remote execution. - The rules fail at load if the compiler in your table differs from the one buildrules pins. See rust-toolchain.toml.