2025-02-01 21:48:10 +01:00
|
|
|
{
|
|
|
|
/* build inputs */
|
|
|
|
nixpkgs,
|
|
|
|
crane,
|
|
|
|
rust-overlay,
|
|
|
|
|
|
|
|
/* parameters */
|
|
|
|
system,
|
|
|
|
git_version ? "unknown",
|
|
|
|
target ? null,
|
|
|
|
release ? false,
|
|
|
|
features ? null,
|
|
|
|
}:
|
2022-07-25 14:58:47 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
log = v: builtins.trace v v;
|
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
pkgs =
|
|
|
|
import nixpkgs {
|
2023-01-26 12:20:12 +01:00
|
|
|
inherit system;
|
2025-02-01 21:48:10 +01:00
|
|
|
overlays = [ (import rust-overlay) ];
|
2023-01-26 12:20:12 +01:00
|
|
|
};
|
2022-07-25 14:58:47 +02:00
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
inherit (pkgs) lib;
|
|
|
|
|
|
|
|
toolchain = pkgs.symlinkJoin {
|
|
|
|
name = "garage-rust-toolchain-1.78";
|
|
|
|
paths = [
|
|
|
|
(pkgs.rust-bin.stable."1.78.0".default.override {
|
|
|
|
targets = [
|
|
|
|
"arm-unknown-linux-musleabihf"
|
|
|
|
"aarch64-unknown-linux-musl"
|
|
|
|
"i686-unknown-linux-musl"
|
|
|
|
"x86_64-unknown-linux-musl"
|
|
|
|
];
|
|
|
|
extensions = [
|
|
|
|
"rust-src"
|
|
|
|
"rustfmt"
|
|
|
|
];
|
2023-01-26 12:20:12 +01:00
|
|
|
})
|
2025-02-01 21:48:10 +01:00
|
|
|
pkgs.clang
|
|
|
|
pkgs.mold
|
|
|
|
pkgs.protobuf
|
|
|
|
];
|
|
|
|
};
|
2023-01-26 12:20:12 +01:00
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
craneLib = (crane.mkLib pkgs).overrideToolchain toolchain;
|
2023-01-26 12:20:12 +01:00
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
src = craneLib.cleanCargoSource ../.;
|
2023-01-26 12:20:12 +01:00
|
|
|
|
|
|
|
/* 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 21:48:10 +01:00
|
|
|
([ "bundled-libs" "lmdb" "sqlite" "k2v" ] ++ (if release then [
|
|
|
|
"consul-discovery"
|
|
|
|
"kubernetes-discovery"
|
|
|
|
"metrics"
|
|
|
|
"telemetry-otlp"
|
|
|
|
"syslog"
|
2023-01-26 12:20:12 +01:00
|
|
|
] else
|
|
|
|
[ ]));
|
2022-10-13 14:35:39 +02:00
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
featuresStr = lib.concatStringsSep "," rootFeatures;
|
2022-07-25 14:58:47 +02:00
|
|
|
|
2023-01-26 12:20:12 +01:00
|
|
|
/* We compile fully static binaries with musl to simplify deployment on most systems.
|
|
|
|
When possible, we reactivate PIE hardening (see above).
|
2022-07-25 14:58:47 +02:00
|
|
|
|
2023-01-26 12:20:12 +01:00
|
|
|
Also, if you set the RUSTFLAGS environment variable, the following parameters will
|
|
|
|
be ignored.
|
2022-07-25 14:58:47 +02:00
|
|
|
|
2023-01-26 12:20:12 +01:00
|
|
|
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
|
2022-07-25 14:58:47 +02:00
|
|
|
*/
|
2025-02-01 21:48:10 +01:00
|
|
|
codegenOptsMap = {
|
2023-01-26 12:20:12 +01:00
|
|
|
"armv6l-unknown-linux-musleabihf" = [
|
|
|
|
"target-feature=+crt-static"
|
|
|
|
"link-arg=-static"
|
|
|
|
]; # compile as dynamic with 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
|
|
|
|
"x86_64-unknown-linux-musl" =
|
|
|
|
[ "target-feature=+crt-static" "link-arg=-static-pie" ];
|
2022-07-25 14:58:47 +02:00
|
|
|
};
|
|
|
|
|
2023-01-26 12:20:12 +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;
|
|
|
|
|
2025-02-01 21:48:10 +01:00
|
|
|
codegenOpts = if target != null then codegenOptsMap.${target} else [
|
|
|
|
"link-args=-fuse-ld=mold"
|
|
|
|
];
|
|
|
|
|
|
|
|
commonArgs =
|
|
|
|
{
|
|
|
|
inherit src;
|
|
|
|
pname = "garage";
|
|
|
|
version = git_version;
|
|
|
|
|
|
|
|
strictDeps = true;
|
|
|
|
cargoExtraArgs = "--features ${featuresStr}";
|
|
|
|
|
|
|
|
GIT_VERSION = git_version;
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else {});
|
|
|
|
|
|
|
|
in rec {
|
|
|
|
inherit toolchain;
|
|
|
|
|
|
|
|
devShell = pkgs.mkShell {
|
|
|
|
buildInputs = [
|
|
|
|
toolchain
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
garage-deps = craneLib.buildDepsOnly commonArgs;
|
|
|
|
|
|
|
|
garage = craneLib.buildPackage (commonArgs // {
|
|
|
|
cargoArtifacts = garage-deps;
|
|
|
|
doCheck = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
garage-test = craneLib.cargoTest (commonArgs // {
|
|
|
|
cargoTestExtraArgs = "--workspace";
|
|
|
|
cargoArtifacts = garage;
|
|
|
|
CARGO_PROFILE = "test";
|
|
|
|
});
|
|
|
|
}
|