This project is intended to demonstrate a Bazel setup for Rust services with Docker support.
It has the following features:
- 🏗 Bazel build system
- 🦀 Compile, test and run Rust binaries, libraries and tests
- 🧰 Cargo workspace and Cargo Raze for managing Rust dependencies
- 🧐 Support for Rust-analyzer (and VS Code)
- ⚖️ Optimized Rust compiler flags for release targets
- 📦 Minimal Docker Rust images with distroless/static-debian11 as base image (2.36MB)
- 🚀 CircleCI pipeline with Bazel remote-cache on AWS S3
- ⚙️ Support for cross-compiling Rust targets from MacOS to Linux using Musl
-
Install Bazelisk
-
Install Rustup
-
Install Cargo Raze
$ cargo install cargo-raze- For cross-compilation from MacOS to Linux musl targets, install musl-cross
$ brew install filosottile/musl-cross/musl-cross- For Bazel remote cache, install https://github.com/buchgr/bazel-remote
$ bazel build //...$ bazel test //...$ bazel run //service1:app$ bazel run //service1:image$ bazel run //service1:image --//:release$ bazel run //service1:image --platforms //build/platform:muslCan also be combined with --//:release.
In order for Rust-Analyzer to work, it is necessary to generate a rust-project.json file. To do so, run
$ bazel run @rules_rust//tools/rust_analyzer:gen_rust_projectWhen using VS Code as IDE, install the following extension for Rust-Analyzer: https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer
In your VS Code settings.json, make sure to have the following configuration:
{
"rust-analyzer.checkOnSave.overrideCommand": ["cargo", "check" "--message-format=json"]
}The Rust-Analyzer extension will pick up the rust-project.json file which was generated earlier.
rust-project.json file whenever a new folder or dependency is added. It may also be needed to reload the workspace when the updated structure is not immediately recognized by Rust-Analyzer.
For each of the dependencies listed in any of the Cargo.toml files, a Bazel build should be generated. This is done automatically by running
$ cargo razeIf not already the case, this will add a cargo folder in the root of the project. Crates can now be referenced fron within Bazel through //cargo/*NAME* (eg. //cargo/ferris_says).
After adding a new external dependency in any of the Cargo.toml files, run the following steps:
- Run
cargo raze --generate-lockfile - Run
bazel build //... - Update
rust-project.jsonby runningbazel run @rules_rust//tools/rust_analyzer:gen_rust_project - Reload Rust Workspace in VS Code
- Start using crate in your Rust library or binary
This project provides a basic setup for cross-compiling from MacOS to Linux. The setup is inspired by rust-bazel-cross.
When applying the Bazel config flag --config=remote_cache, Bazel will connect to a remote cache running at localhost:9090. This server will sync files between a local cache and an AWS S3 bucket.
To start the bazel-remote server, run the script ./scripts/start_bazel_remote.sh. Provide the following environment variables:
- AWS_REGION
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_S3_BUCKET_BAZEL_CACHE