Skip to content

Commit 68acc75

Browse files
committed
Build nativelink with musl
Reduce container image sizes by over 50% to about 28 megabytes. The nativelink executable is now fully self-contained and portable across linux systems regardless of the host's libc. This change introduces new Rust toolchains on the nix side that mirror the Bazel toolchains. The new toolchains enable `cargo clippy` invocations without Bazel and allow invoking the same nightly `rustfmt` that is used in the Bazel build via pre-commit hooks.
1 parent beb87cf commit 68acc75

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
ignore = ["nativelink-proto/genproto/lib.rs"]
12
max_width = 120
23
reorder_imports = true
34
imports_granularity = "Module"

flake.lock

Lines changed: 32 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
67
pre-commit-hooks = {
78
url = "github:cachix/pre-commit-hooks.nix";
89
inputs.nixpkgs.follows = "nixpkgs";
10+
inputs.flake-utils.follows = "flake-utils";
11+
};
12+
rust-overlay = {
13+
url = "github:oxalica/rust-overlay";
14+
inputs.nixpkgs.follows = "nixpkgs";
15+
inputs.flake-utils.follows = "flake-utils";
916
};
1017
crane = {
1118
url = "github:ipetkov/crane";
@@ -17,6 +24,7 @@
1724
self,
1825
flake-parts,
1926
crane,
27+
rust-overlay,
2028
...
2129
}:
2230
flake-parts.lib.mkFlake {inherit inputs;} {
@@ -32,6 +40,8 @@
3240
system,
3341
...
3442
}: let
43+
stable-rust = pkgs.rust-bin.stable."1.74.0";
44+
nightly-rust = pkgs.rust-bin.nightly."2023-12-07";
3545
isDarwin = builtins.elem system [
3646
"x86_64-darwin"
3747
"aarch64-darwin"
@@ -51,7 +61,9 @@
5161
stdenv = customStdenv;
5262
};
5363

54-
craneLib = crane.lib.${system};
64+
craneLib = (crane.mkLib pkgs).overrideToolchain (pkgs.rust-bin.stable.latest.default.override {
65+
targets = ["x86_64-unknown-linux-musl"];
66+
});
5567

5668
src = pkgs.lib.cleanSourceWith {
5769
src = craneLib.path ./.;
@@ -62,15 +74,16 @@
6274

6375
commonArgs = {
6476
inherit src;
77+
inherit (pkgs.pkgsMusl) stdenv;
6578
strictDeps = true;
6679
buildInputs = maybeDarwinDeps;
6780
nativeBuildInputs =
6881
[
6982
pkgs.cacert
7083
]
71-
++ maybeDarwinDeps
72-
++ pkgs.lib.optionals (!isDarwin) [pkgs.autoPatchelfHook];
73-
stdenv = customStdenv;
84+
++ maybeDarwinDeps;
85+
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
86+
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
7487
};
7588

7689
# Additional target for external dependencies to simplify caching.
@@ -89,6 +102,10 @@
89102

90103
generate-toolchains = import ./tools/generate-toolchains.nix {inherit pkgs;};
91104
in {
105+
_module.args.pkgs = import self.inputs.nixpkgs {
106+
inherit system;
107+
overlays = [(import rust-overlay)];
108+
};
92109
apps = {
93110
default = {
94111
type = "app";
@@ -129,13 +146,19 @@
129146
# partitionType = "count";
130147
# });
131148
};
132-
pre-commit.settings = {inherit hooks;};
149+
pre-commit.settings = {
150+
inherit hooks;
151+
tools = let
152+
mkOverrideTools = pkgs.lib.mkOverride (pkgs.lib.modules.defaultOverridePriority - 1);
153+
in {
154+
rustfmt = mkOverrideTools nightly-rust.rustfmt;
155+
};
156+
};
133157
devShells.default = pkgs.mkShell {
134158
nativeBuildInputs =
135159
[
136160
# Development tooling goes here.
137-
pkgs.cargo
138-
pkgs.rustc
161+
stable-rust.default
139162
pkgs.pre-commit
140163
pkgs.bazel
141164
pkgs.awscli2

tools/pre-commit-hooks.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{pkgs, ...}: let
2-
excludes = ["^gencargo/" "^nativelink-proto/genproto"];
2+
excludes = ["^nativelink-proto/genproto"];
33
in {
44
# Default hooks
55
trailing-whitespace-fixer = {
@@ -57,6 +57,9 @@ in {
5757
statix.enable = true;
5858
deadnix.enable = true;
5959

60+
# Rust
61+
rustfmt.enable = true;
62+
6063
# Starlark
6164
bazel-buildifier-format = {
6265
enable = true;

0 commit comments

Comments
 (0)