I am trying to cross compile a rust project to a docker image using cargo2nix. However the docker image seems to pull in all the build tools when I only relay want the static binary this results in rather large docker images.
{
description = "Cross-compiling nexus_s3";
inputs = {
cargo2nix.url = "github:cargo2nix/cargo2nix/main";
flake-utils.follows = "cargo2nix/flake-utils";
nixpkgs.follows = "cargo2nix/nixpkgs";
};
outputs = inputs:
with inputs;
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
# crossSystem.config = "aarch64-unknown-linux-musl";
crossSystem.config = "x86_64-unknown-linux-musl";
overlays = [cargo2nix.overlays.default];
};
appName = "nexus_s3";
# create the workspace & dependencies package set
rustPkgs = pkgs.rustBuilder.makePackageSet {
rustVersion = "1.85.0";
packageFun = import ./Cargo.nix;
};
in rec {
packages = {
cross-package = rustPkgs.workspace.nexus_s3 {};
docker = pkgs.dockerTools.buildLayeredImage {
name = appName;
config.Cmd = "${packages.cross-package}/bin/${appName}";
};
};
}
);
}
When I build the package with nix build .#cross-package
i get a binary that is 17Mb.
When building with nix build .#docker
i get an tar image that is 498Mb.
After inspecting the image I see that there all the rust build libs, gcc etc have been included is there an idiomatic way to exclude these and just keep the binary?