This commit is contained in:
Alex 2025-02-01 23:33:54 +01:00
parent e819e1a631
commit e4e2f48de4

View file

@ -15,12 +15,18 @@
let let
log = v: builtins.trace v v; log = v: builtins.trace v v;
targetEnvMap = { # NixOS and Rust/Cargo triples do not match for ARM, fix it here.
"armv6l-unknown-linux-musleabihf" = "ARMV6L_UNKNOWN_LINUX_MUSLEABIHF"; rustTarget = if target == "armv6l-unknown-linux-musleabihf" then
"aarch64-unknown-linux-musl" = "AARCH64_UNKNOWN_LINUX_MUSL"; "arm-unknown-linux-musleabihf"
"i686-unknown-linux-musl" = "I686_UNKNOWN_LINUX_MUSL"; else
"x86_64-unknown-linux-musl" = "X86_64_UNKNOWN_LINUX_MUSL"; target;
};
rustTargetEnvMap = {
"x86_64-unknown-linux-musl" = "X86_64_UNKNOWN_LINUX_MUSL";
"aarch64-unknown-linux-musl" = "AARCH64_UNKNOWN_LINUX_MUSL";
"i686-unknown-linux-musl" = "I686_UNKNOWN_LINUX_MUSL";
"arm-unknown-linux-musleabihf" = "ARM_UNKNOWN_LINUX_MUSLEABIHF";
};
pkgs = if target != null then pkgs = if target != null then
import nixpkgs { import nixpkgs {
@ -39,29 +45,15 @@ let
inherit (pkgs) lib stdenv; inherit (pkgs) lib stdenv;
toolchain = (p: p.symlinkJoin { toolchainFn = (p: p.rust-bin.stable."1.78.0".default.override {
name = "garage-rust-toolchain-1.78"; targets = lib.optional (target != null) [ rustTarget ];
paths = [ extensions = [
(p.rust-bin.stable."1.78.0".default.override { "rust-src"
targets = [ "rustfmt"
"arm-unknown-linux-musleabihf"
"aarch64-unknown-linux-musl"
"i686-unknown-linux-musl"
"x86_64-unknown-linux-musl"
];
extensions = [
"rust-src"
"rustfmt"
];
})
p.protobuf
] ++ lib.optionals (target == null) [
# p.clang
# p.mold
]; ];
}); });
craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; craneLib = (crane.mkLib pkgs).overrideToolchain toolchainFn;
src = craneLib.cleanCargoSource ../.; src = craneLib.cleanCargoSource ../.;
@ -73,14 +65,13 @@ let
rootFeatures = if features != null then rootFeatures = if features != null then
features features
else else
([ "bundled-libs" "lmdb" "sqlite" "k2v" ] ++ (if release then [ ([ "bundled-libs" "lmdb" "sqlite" "k2v" ] ++ (lib.optionals release [
"consul-discovery" "consul-discovery"
"kubernetes-discovery" "kubernetes-discovery"
"metrics" "metrics"
"telemetry-otlp" "telemetry-otlp"
"syslog" "syslog"
] else ]));
[ ]));
featuresStr = lib.concatStringsSep "," rootFeatures; featuresStr = lib.concatStringsSep "," rootFeatures;
@ -94,10 +85,8 @@ let
https://rust-lang.github.io/rfcs/1721-crt-static.html#specifying-dynamicstatic-c-runtime-linkage https://rust-lang.github.io/rfcs/1721-crt-static.html#specifying-dynamicstatic-c-runtime-linkage
*/ */
codegenOptsMap = { codegenOptsMap = {
"armv6l-unknown-linux-musleabihf" = [ "x86_64-unknown-linux-musl" =
"target-feature=+crt-static" [ "target-feature=+crt-static" "link-arg=-static-pie" ];
"link-arg=-static"
]; # compile as dynamic with static-pie
"aarch64-unknown-linux-musl" = [ "aarch64-unknown-linux-musl" = [
"target-feature=+crt-static" "target-feature=+crt-static"
"link-arg=-static" "link-arg=-static"
@ -106,16 +95,12 @@ let
"target-feature=+crt-static" "target-feature=+crt-static"
"link-arg=-static" "link-arg=-static"
]; # segfault with static-pie ]; # segfault with static-pie
"x86_64-unknown-linux-musl" = "armv6l-unknown-linux-musleabihf" = [
[ "target-feature=+crt-static" "link-arg=-static-pie" ]; "target-feature=+crt-static"
"link-arg=-static"
]; # compile as dynamic with static-pie
}; };
# NixOS and Rust/Cargo triples do not match for ARM, fix it here.
rustTarget = if target == "armv6l-unknown-linux-musleabihf" then
"arm-unknown-linux-musleabihf"
else
target;
codegenOpts = if target != null then codegenOptsMap.${target} else [ codegenOpts = if target != null then codegenOptsMap.${target} else [
# "link-args=-fuse-ld=mold" # "link-args=-fuse-ld=mold"
]; ];
@ -129,10 +114,14 @@ let
strictDeps = true; strictDeps = true;
cargoExtraArgs = "--features ${featuresStr}"; cargoExtraArgs = "--features ${featuresStr}";
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; ([
pkg-config pkg-config
stdenv.cc stdenv.cc
]; protobuf
] ++ lib.optionals (target == null) [
# clang
# mold
]);
GIT_VERSION = git_version; GIT_VERSION = git_version;
@ -146,7 +135,7 @@ let
(if rustTarget != null then { (if rustTarget != null then {
CARGO_BUILD_TARGET = rustTarget; CARGO_BUILD_TARGET = rustTarget;
"CARGO_TARGET_${targetEnvMap.${target}}_LINKER" = "${stdenv.cc.targetPrefix}cc"; "CARGO_TARGET_${rustTargetEnvMap.${rustTarget}}_LINKER" = "${stdenv.cc.targetPrefix}cc";
HOST_CC = "${stdenv.cc.nativePrefix}cc"; HOST_CC = "${stdenv.cc.nativePrefix}cc";
TARGET_CC = "${stdenv.cc.targetPrefix}cc"; TARGET_CC = "${stdenv.cc.targetPrefix}cc";
@ -155,12 +144,16 @@ let
}); });
in rec { in rec {
toolchain = toolchain pkgs; toolchain = toolchainFn pkgs;
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
buildInputs = [ buildInputs = [
toolchain toolchain
]; ] ++ (with pkgs; [
protobuf
clang
mold
]);
}; };
garage-deps = craneLib.buildDepsOnly commonArgs; garage-deps = craneLib.buildDepsOnly commonArgs;