#8 chore: build static binaries with musl target
Some checks are pending
ci/woodpecker/push/ci Pipeline is pending

Statically build binaries as musl targets

Reviewed-on: #8
This commit is contained in:
Lars Sjöström 2025-02-11 10:55:25 +01:00
commit 64c971e68e
2 changed files with 31 additions and 20 deletions

View file

@ -1,10 +0,0 @@
[alias]
xtask = "run --package xtask --quiet --"
[profile.dev]
debug = 0
strip = "debuginfo"
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold", "-C", "target-cpu=native"]

View file

@ -27,6 +27,8 @@
system:
let
rustVersion = "1.83.0";
target = "x86_64-unknown-linux-musl";
isStatic = true;
overlays = [
(import rust-overlay)
@ -43,7 +45,24 @@
];
pkgs = import nixpkgs { inherit overlays system; };
craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rust-toolchain;
basePkgs = import nixpkgs (
{
localSystem = system;
overlays = [
(import rust-overlay)
];
}
// pkgs.lib.optionalAttrs isStatic { crossSystem.config = target; }
);
crossPkgs = (if isStatic then basePkgs.pkgsStatic else basePkgs);
craneLib = (crane.mkLib crossPkgs).overrideToolchain (
p:
p.rust-bin.stable.${rustVersion}.default.override {
targets = [ target ];
}
);
src = pkgs.lib.fileset.toSource {
root = ./.;
@ -58,19 +77,21 @@
commonArgs = {
inherit src;
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
strictDeps = true;
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
nativeBuildInputs = with pkgs; [
clang
mold-wrapped
pkg-config
];
nativeBuildInputs = with crossPkgs.pkgsBuildHost; [ pkg-config ];
buildInputs = with crossPkgs.pkgsHostHost; [ openssl ];
buildInputs = with pkgs; [
openssl
];
CARGO_BUILD_TARGET = target;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
"CARGO_TARGET_${pkgs.lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] target)}_LINKER" =
"${crossPkgs.stdenv.cc.targetPrefix}cc";
OPENSSL_STATIC = true;
OPENSSL_DIR = "${crossPkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${crossPkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${crossPkgs.openssl.dev}/include/";
};
buildCrate =