garage/nix/compile.nix

179 lines
4.7 KiB
Nix
Raw Normal View History

{
/* build inputs */
nixpkgs,
crane,
rust-overlay,
/* parameters */
system,
git_version ? "unknown",
target ? null,
release ? false,
features ? null,
}:
let
log = v: builtins.trace v v;
2025-02-01 23:33:54 +01:00
# 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;
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";
};
2025-02-01 23:22:13 +01:00
pkgs = if target != null then
import nixpkgs {
inherit system;
crossSystem = {
config = target;
isStatic = true;
};
overlays = [ (import rust-overlay) ];
}
else
import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
2025-02-01 23:22:13 +01:00
inherit (pkgs) lib stdenv;
2025-02-01 23:33:54 +01:00
toolchainFn = (p: p.rust-bin.stable."1.78.0".default.override {
targets = lib.optionals (target != null) [ rustTarget ];
2025-02-01 23:33:54 +01:00
extensions = [
"rust-src"
"rustfmt"
];
2025-02-01 23:22:13 +01:00
});
2025-02-01 23:33:54 +01:00
craneLib = (crane.mkLib pkgs).overrideToolchain toolchainFn;
src = craneLib.cleanCargoSource ../.;
/* We ship some parts of the code disabled by default by putting them behind a flag.
It speeds up the compilation (when the feature is not required) and released crates have less dependency by default (less attack surface, disk space, etc.).
But we want to ship these additional features when we release Garage.
In the end, we chose to exclude all features from debug builds while putting (all of) them in the release builds.
*/
rootFeatures = if features != null then
features
else
2025-02-01 23:33:54 +01:00
([ "bundled-libs" "lmdb" "sqlite" "k2v" ] ++ (lib.optionals release [
"consul-discovery"
"kubernetes-discovery"
"metrics"
"telemetry-otlp"
"syslog"
2025-02-01 23:33:54 +01:00
]));
featuresStr = lib.concatStringsSep "," rootFeatures;
/* We compile fully static binaries with musl to simplify deployment on most systems.
When possible, we reactivate PIE hardening (see above).
Also, if you set the RUSTFLAGS environment variable, the following parameters will
be ignored.
For more information on static builds, please refer to Rust's RFC 1721.
https://rust-lang.github.io/rfcs/1721-crt-static.html#specifying-dynamicstatic-c-runtime-linkage
*/
codegenOptsMap = {
2025-02-01 23:33:54 +01:00
"x86_64-unknown-linux-musl" =
[ "target-feature=+crt-static" "link-arg=-static-pie" ];
"aarch64-unknown-linux-musl" = [
"target-feature=+crt-static"
"link-arg=-static"
]; # segfault with static-pie
"i686-unknown-linux-musl" = [
"target-feature=+crt-static"
"link-arg=-static"
]; # segfault with static-pie
2025-02-01 23:33:54 +01:00
"armv6l-unknown-linux-musleabihf" = [
"target-feature=+crt-static"
"link-arg=-static"
]; # compile as dynamic with static-pie
};
codegenOpts = if target != null then codegenOptsMap.${target} else [
2025-02-01 23:39:41 +01:00
"link-arg=-fuse-ld=mold"
];
commonArgs =
{
inherit src;
pname = "garage";
version = "none";
strictDeps = true;
cargoExtraArgs = "--locked --features ${featuresStr}";
2025-02-01 23:33:54 +01:00
nativeBuildInputs = with pkgs; ([
2025-02-01 23:22:13 +01:00
pkg-config
stdenv.cc
2025-02-01 23:33:54 +01:00
protobuf
] ++ lib.optionals (target == null) [
2025-02-01 23:39:41 +01:00
clang
mold
2025-02-01 23:33:54 +01:00
]);
2025-02-01 23:22:13 +01:00
CARGO_PROFILE = if release then "release" else "dev";
CARGO_BUILD_RUSTFLAGS =
lib.concatStringsSep
" "
(builtins.map (flag: "-C ${flag}") codegenOpts);
}
//
(if rustTarget != null then {
CARGO_BUILD_TARGET = rustTarget;
2025-02-01 23:22:13 +01:00
2025-02-01 23:33:54 +01:00
"CARGO_TARGET_${rustTargetEnvMap.${rustTarget}}_LINKER" = "${stdenv.cc.targetPrefix}cc";
2025-02-01 23:22:13 +01:00
HOST_CC = "${stdenv.cc.nativePrefix}cc";
TARGET_CC = "${stdenv.cc.targetPrefix}cc";
} else {
2025-02-01 23:39:41 +01:00
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "clang";
2025-02-01 23:22:13 +01:00
});
in rec {
2025-02-01 23:33:54 +01:00
toolchain = toolchainFn pkgs;
devShell = pkgs.mkShell {
buildInputs = [
toolchain
2025-02-01 23:33:54 +01:00
] ++ (with pkgs; [
protobuf
clang
mold
]);
};
garage-deps = craneLib.buildDepsOnly commonArgs;
garage = craneLib.buildPackage (commonArgs // {
cargoArtifacts = garage-deps;
doCheck = false;
version = git_version;
GIT_VERSION = git_version;
});
garage-test-deps = craneLib.buildDepsOnly (commonArgs // {
pname = "garage-test";
cargoArtifacts = garage;
cargoExtraArgs = "${commonArgs.cargoExtraArgs} --tests --workspace";
CARGO_PROFILE = "test";
});
garage-test = craneLib.cargoTest (commonArgs // {
cargoTestExtraArgs = "--workspace";
cargoArtifacts = garage-test-deps;
CARGO_PROFILE = "test";
});
}