Alex Auvolat
05a98ff45c
Some checks failed
ci/woodpecker/pr/debug Pipeline was successful
ci/woodpecker/push/debug Pipeline was successful
ci/woodpecker/deployment/debug Pipeline was successful
ci/woodpecker/deployment/release/3 Pipeline failed
ci/woodpecker/deployment/release/2 Pipeline failed
ci/woodpecker/deployment/release/1 Pipeline failed
ci/woodpecker/deployment/release/4 Pipeline failed
ci/woodpecker/deployment/publish unknown status
197 lines
5.2 KiB
Nix
197 lines
5.2 KiB
Nix
{
|
|
/* build inputs */
|
|
nixpkgs,
|
|
crane,
|
|
rust-overlay,
|
|
|
|
/* parameters */
|
|
system,
|
|
git_version ? null,
|
|
target ? null,
|
|
release ? false,
|
|
features ? null,
|
|
extraTestEnv ? {}
|
|
}:
|
|
|
|
let
|
|
log = v: builtins.trace v v;
|
|
|
|
# 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";
|
|
};
|
|
|
|
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) ];
|
|
};
|
|
|
|
inherit (pkgs) lib stdenv;
|
|
|
|
toolchainFn = (p: p.rust-bin.stable."1.78.0".default.override {
|
|
targets = lib.optionals (target != null) [ rustTarget ];
|
|
extensions = [
|
|
"rust-src"
|
|
"rustfmt"
|
|
];
|
|
});
|
|
|
|
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
|
|
([ "bundled-libs" "lmdb" "sqlite" "k2v" ] ++ (lib.optionals release [
|
|
"consul-discovery"
|
|
"kubernetes-discovery"
|
|
"metrics"
|
|
"telemetry-otlp"
|
|
"syslog"
|
|
]));
|
|
|
|
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 = {
|
|
"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
|
|
"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 [
|
|
"link-arg=-fuse-ld=mold"
|
|
];
|
|
|
|
commonArgs =
|
|
{
|
|
inherit src;
|
|
pname = "garage";
|
|
version = "dev";
|
|
|
|
strictDeps = true;
|
|
cargoExtraArgs = "--locked --features ${featuresStr}";
|
|
|
|
nativeBuildInputs = with pkgs; ([
|
|
pkg-config
|
|
stdenv.cc
|
|
protobuf
|
|
] ++ lib.optionals (target == null) [
|
|
clang
|
|
mold
|
|
]);
|
|
|
|
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;
|
|
|
|
"CARGO_TARGET_${rustTargetEnvMap.${rustTarget}}_LINKER" = "${stdenv.cc.targetPrefix}cc";
|
|
|
|
HOST_CC = "${stdenv.cc.nativePrefix}cc";
|
|
TARGET_CC = "${stdenv.cc.targetPrefix}cc";
|
|
} else {
|
|
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "clang";
|
|
});
|
|
|
|
testArgs = commonArgs // {
|
|
pname = "garage-tests";
|
|
cargoExtraArgs = "${commonArgs.cargoExtraArgs} --tests --workspace";
|
|
CARGO_PROFILE = "test";
|
|
};
|
|
|
|
in rec {
|
|
toolchain = toolchainFn pkgs;
|
|
|
|
devShell = pkgs.mkShell {
|
|
buildInputs = [
|
|
toolchain
|
|
] ++ (with pkgs; [
|
|
protobuf
|
|
clang
|
|
mold
|
|
]);
|
|
};
|
|
|
|
# ---- building garage ----
|
|
|
|
garage-deps = craneLib.buildDepsOnly commonArgs;
|
|
|
|
garage = craneLib.buildPackage (commonArgs // {
|
|
cargoArtifacts = garage-deps;
|
|
|
|
doCheck = false;
|
|
} //
|
|
(if git_version != null then {
|
|
version = git_version;
|
|
GIT_VERSION = git_version;
|
|
} else {}));
|
|
|
|
# ---- testing garage ----
|
|
|
|
garage-test-deps = craneLib.buildDepsOnly testArgs;
|
|
# cargoArtifacts = garage would do nothing for this derivation
|
|
# because it's not the same profile
|
|
# this is a truly independent build
|
|
|
|
garage-test-bin = craneLib.cargoBuild (testArgs // {
|
|
cargoArtifacts = garage-test-deps;
|
|
|
|
doCheck = false;
|
|
});
|
|
|
|
garage-test = craneLib.cargoTest (testArgs // {
|
|
cargoArtifacts = garage-test-bin;
|
|
nativeBuildInputs = commonArgs.nativeBuildInputs ++ [
|
|
pkgs.cacert
|
|
];
|
|
} // extraTestEnv);
|
|
}
|