Skip to content

Build nativelink with musl #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ignore = ["nativelink-proto/genproto/lib.rs"]
max_width = 120
reorder_imports = true
imports_granularity = "Module"
Expand Down
37 changes: 32 additions & 5 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 47 additions & 22 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
crane = {
url = "github:ipetkov/crane";
Expand All @@ -17,6 +24,7 @@
self,
flake-parts,
crane,
rust-overlay,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
Expand All @@ -32,17 +40,15 @@
system,
...
}: let
isDarwin = builtins.elem system [
"x86_64-darwin"
"aarch64-darwin"
];
stable-rust = pkgs.rust-bin.stable."1.74.0";
nightly-rust = pkgs.rust-bin.nightly."2023-12-07";

maybeDarwinDeps = pkgs.lib.optionals isDarwin [
maybeDarwinDeps = pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.libiconv
];

customStdenv = import ./tools/llvmStdenv.nix {inherit pkgs isDarwin;};
customStdenv = import ./tools/llvmStdenv.nix {inherit pkgs;};

# TODO(aaronmondal): This doesn't work with rules_rust yet.
# Tracked in https://github.com/TraceMachina/nativelink/issues/477.
Expand All @@ -51,7 +57,13 @@
stdenv = customStdenv;
};

craneLib = crane.lib.${system};
craneLib =
if pkgs.stdenv.isDarwin
then crane.lib.${system}
else
(crane.mkLib pkgs).overrideToolchain (pkgs.rust-bin.stable.latest.default.override {
targets = ["x86_64-unknown-linux-musl"];
});

src = pkgs.lib.cleanSourceWith {
src = craneLib.path ./.;
Expand All @@ -60,18 +72,21 @@
|| (craneLib.filterCargoSources path type);
};

commonArgs = {
inherit src;
strictDeps = true;
buildInputs = maybeDarwinDeps;
nativeBuildInputs =
[
pkgs.cacert
]
++ maybeDarwinDeps
++ pkgs.lib.optionals (!isDarwin) [pkgs.autoPatchelfHook];
stdenv = customStdenv;
};
commonArgs =
{
inherit src;
stdenv =
if pkgs.stdenv.isDarwin
then customStdenv
else pkgs.pkgsMusl.stdenv;
strictDeps = true;
buildInputs = maybeDarwinDeps;
nativeBuildInputs = [pkgs.cacert] ++ maybeDarwinDeps;
}
// pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
};

# Additional target for external dependencies to simplify caching.
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
Expand All @@ -89,6 +104,10 @@

generate-toolchains = import ./tools/generate-toolchains.nix {inherit pkgs;};
in {
_module.args.pkgs = import self.inputs.nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
apps = {
default = {
type = "app";
Expand Down Expand Up @@ -129,13 +148,19 @@
# partitionType = "count";
# });
};
pre-commit.settings = {inherit hooks;};
pre-commit.settings = {
inherit hooks;
tools = let
mkOverrideTools = pkgs.lib.mkOverride (pkgs.lib.modules.defaultOverridePriority - 1);
in {
rustfmt = mkOverrideTools nightly-rust.rustfmt;
};
};
devShells.default = pkgs.mkShell {
nativeBuildInputs =
[
# Development tooling goes here.
pkgs.cargo
pkgs.rustc
stable-rust.default
pkgs.pre-commit
pkgs.bazel
pkgs.awscli2
Expand Down
8 changes: 2 additions & 6 deletions tools/llvmStdenv.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
pkgs,
isDarwin ? false,
...
}: let
{pkgs}: let
llvmPackages = pkgs.llvmPackages_17;

toolchain =
if isDarwin
if pkgs.stdenv.isDarwin
then
(
pkgs.overrideCC (
Expand Down
5 changes: 4 additions & 1 deletion tools/pre-commit-hooks.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{pkgs, ...}: let
excludes = ["^gencargo/" "^nativelink-proto/genproto"];
excludes = ["^nativelink-proto/genproto"];
in {
# Default hooks
trailing-whitespace-fixer = {
Expand Down Expand Up @@ -57,6 +57,9 @@ in {
statix.enable = true;
deadnix.enable = true;

# Rust
rustfmt.enable = true;

# Starlark
bazel-buildifier-format = {
enable = true;
Expand Down