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