From 8d04ae7014991319e97d4280f0e9d7a70c89f10b Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 13 Oct 2022 14:35:39 +0200 Subject: cargo2nix unstable (patched), rust 1.63.0, nixpkgs 22.05 (32-bit builds are broken) --- nix/common.nix | 20 +++++++--- nix/compile.nix | 112 +++++++++++++++++++++++------------------------------- nix/toolchain.nix | 4 ++ 3 files changed, 65 insertions(+), 71 deletions(-) (limited to 'nix') diff --git a/nix/common.nix b/nix/common.nix index aa59cdc0..031a75c0 100644 --- a/nix/common.nix +++ b/nix/common.nix @@ -4,19 +4,27 @@ rec { */ pkgsSrc = fetchTarball { # As of 2021-10-04 - url = "https://github.com/NixOS/nixpkgs/archive/b27d18a412b071f5d7991d1648cfe78ee7afe68a.tar.gz"; - sha256 = "1xy9zpypqfxs5gcq5dcla4bfkhxmh5nzn9dyqkr03lqycm9wg5cr"; + #url = "https://github.com/NixOS/nixpkgs/archive/b27d18a412b071f5d7991d1648cfe78ee7afe68a.tar.gz"; + #sha256 = "1xy9zpypqfxs5gcq5dcla4bfkhxmh5nzn9dyqkr03lqycm9wg5cr"; + + # NixOS 22.05 + url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.zip"; + sha256 = "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik"; + + # As of 2022-10-13 + #url = "https://github.com/NixOS/nixpkgs/archive/a3073c49bc0163fea6a121c276f526837672b555.zip"; + #sha256 = "1bz632psfbpmicyzjb8b4265y50shylccvfm6ry6mgnv5hvz324s"; }; cargo2nixSrc = fetchGit { - # As of 2022-08-29, stacking two patches: superboum@dedup_propagate and Alexis211@fix_fetchcrategit + # As of 2022-10-14: (TODO) url = "https://github.com/Alexis211/cargo2nix"; - ref = "fix_fetchcrategit"; - rev = "4b31c0cc05b6394916d46e9289f51263d81973b9"; + ref = "custom_unstable"; + rev = "15543df35485bef9e2092391ecafa78eae5fa740"; }; /* * Shared objects */ cargo2nix = import cargo2nixSrc; - cargo2nixOverlay = import "${cargo2nixSrc}/overlay"; + cargo2nixOverlay = cargo2nix.overlays.default; } diff --git a/nix/compile.nix b/nix/compile.nix index adb07886..62eb956a 100644 --- a/nix/compile.nix +++ b/nix/compile.nix @@ -1,9 +1,10 @@ { system ? builtins.currentSystem, - target ? null, + target, compiler ? "rustc", release ? false, git_version ? null, + features ? null, }: with import ./common.nix; @@ -13,19 +14,12 @@ let pkgs = import pkgsSrc { inherit system; - ${ if target == null then null else "crossSystem" } = { config = target; }; + crossSystem = { + config = target; + }; overlays = [ cargo2nixOverlay ]; }; - /* - Rust and Nix triples are not the same. Cargo2nix has a dedicated library - to convert Nix triples to Rust ones. We need this conversion as we want to - set later options linked to our (rust) target in a generic way. Not only - the triple terminology is different, but also the "roles" are named differently. - Nix uses a build/host/target terminology where Nix's "host" maps to Cargo's "target". - */ - rustTarget = log (pkgs.rustBuilder.rustLib.rustTriple pkgs.stdenv.hostPlatform); - /* Cargo2nix is built for rustOverlay which installs Rust from Mozilla releases. We want our own Rust to avoid incompatibilities, like we had with musl 1.2.0. @@ -37,7 +31,8 @@ let In practise, rustOverlay ships rustc+cargo in a single derivation while NixOS ships them in separate ones. We reunite them with symlinkJoin. */ - rustChannel = { + /* + rustToolchain = { rustc = pkgs.symlinkJoin { name = "rust-channel"; paths = [ @@ -54,29 +49,7 @@ let ]; }; }.${compiler}; - - clippyBuilder = pkgs.writeScriptBin "clippy" '' - #!${pkgs.stdenv.shell} - . ${cargo2nixSrc + "/overlay/utils.sh"} - isBuildScript= - args=("$@") - for i in "''${!args[@]}"; do - if [ "xmetadata=" = "x''${args[$i]::9}" ]; then - args[$i]=metadata=$NIX_RUST_METADATA - elif [ "x--crate-name" = "x''${args[$i]}" ] && [ "xbuild_script_" = "x''${args[$i+1]::13}" ]; then - isBuildScript=1 - fi - done - if [ "$isBuildScript" ]; then - args+=($NIX_RUST_BUILD_LINK_FLAGS) - else - args+=($NIX_RUST_LINK_FLAGS) - fi - touch invoke.log - echo "''${args[@]}" >>invoke.log - - exec ${rustChannel}/bin/clippy-driver --deny warnings "''${args[@]}" - ''; + */ buildEnv = (drv: { rustc = drv.setBuildEnv; @@ -86,7 +59,8 @@ let echo --- BUILDING WITH CLIPPY --- echo - export RUSTC=${clippyBuilder}/bin/clippy + export NIX_RUST_BUILD_FLAGS="''${NIX_RUST_BUILD_FLAGS} --deny warnings" + export RUSTC="''${CLIPPY_DRIVER}" ''; }.${compiler}); @@ -97,7 +71,7 @@ let You can have a complete list of the available options by looking at the overriden object, mkcrate: https://github.com/cargo2nix/cargo2nix/blob/master/overlay/mkcrate.nix */ - overrides = pkgs.rustBuilder.overrides.all ++ [ + packageOverrides = pkgs: pkgs.rustBuilder.overrides.all ++ [ /* [1] We add some logic to compile our crates with clippy, it provides us many additional lints @@ -113,12 +87,7 @@ let As we do not want to consider the .git folder as part of the input source, we ask the user (the CI often) to pass the value to Nix. - [4] 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. - - [5] We don't want libsodium-sys and zstd-sys to try to use pkgconfig to build against a system library. + [4] We don't want libsodium-sys and zstd-sys to try to use pkgconfig to build against a system library. However the features to do so get activated for some reason (due to a bug in cargo2nix?), so disable them manually here. */ @@ -136,10 +105,6 @@ let /* [1] */ setBuildEnv = (buildEnv drv); /* [2] */ hardeningDisable = [ "pie" ]; }; - overrideArgs = old: { - /* [4] */ features = [ "bundled-libs" "sled" "metrics" "k2v" ] - ++ (if release then [ "kubernetes-discovery" "telemetry-otlp" "lmdb" "sqlite" ] else []); - }; }) (pkgs.rustBuilder.rustLib.makeOverride { @@ -190,18 +155,38 @@ let (pkgs.rustBuilder.rustLib.makeOverride { name = "libsodium-sys"; overrideArgs = old: { - features = [ ]; /* [5] */ + features = [ ]; /* [4] */ }; }) (pkgs.rustBuilder.rustLib.makeOverride { name = "zstd-sys"; overrideArgs = old: { - features = [ ]; /* [5] */ + features = [ ]; /* [4] */ }; }) ]; + /* + 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 + ([ + "garage/bundled-libs" + "garage/sled" + "garage/k2v" + ] ++ (if release then [ + "garage/kubernetes-discovery" + "garage/metrics" + "garage/telemetry-otlp" + "garage/lmdb" + "garage/sqlite" + ] else [])); + + packageFun = import ../Cargo.nix; /* @@ -222,23 +207,20 @@ let "x86_64-unknown-linux-musl" = [ "target-feature=+crt-static" "link-arg=-static-pie" ]; }; -in /* - The following definition is not elegant as we use a low level function of Cargo2nix - that enables us to pass our custom rustChannel object. We need this low level definition - to pass Nix's Rust toolchains instead of Mozilla's one. + 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; - target is mandatory but must be kept to null to allow cargo2nix to set it to the appropriate value - for each crate. - */ +in pkgs.rustBuilder.makePackageSet { - inherit packageFun rustChannel release codegenOpts; - packageOverrides = overrides; - target = null; - - buildRustPackages = pkgs.buildPackages.rustBuilder.makePackageSet { - inherit rustChannel packageFun codegenOpts; - packageOverrides = overrides; - target = null; - }; + inherit release packageFun packageOverrides codegenOpts rootFeatures; + + #inherit rustToolchain; + rustVersion = "1.63.0"; + + target = rustTarget; + extraRustComponents = [ "clippy" ]; } diff --git a/nix/toolchain.nix b/nix/toolchain.nix index 7f5d89d3..d6fe295a 100644 --- a/nix/toolchain.nix +++ b/nix/toolchain.nix @@ -14,18 +14,22 @@ let pkgsList = builtins.map (target: import pkgsSrc { inherit system; crossSystem = { config = target; }; + overlays = [ cargo2nixOverlay ]; }) platforms; pkgsHost = import pkgsSrc {}; lib = pkgsHost.lib; kaniko = (import ./kaniko.nix) pkgsHost; winscp = (import ./winscp.nix) pkgsHost; + manifestTool = (import ./manifest-tool.nix) pkgsHost; in lib.flatten (builtins.map (pkgs: [ pkgs.rustPlatform.rust.rustc pkgs.rustPlatform.rust.cargo + pkgs.clippy pkgs.buildPackages.stdenv.cc ]) pkgsList) ++ [ kaniko winscp + manifestTool ] -- cgit v1.2.3 From e89e047c5af33ac47dabbcfdc12305d544a13ade Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 14 Oct 2022 15:45:37 +0200 Subject: Fix i386 build with custom toolchain (armv6 unknown state) --- nix/common.nix | 8 ++++---- nix/compile.nix | 48 ++++++++++++++++++++---------------------------- nix/toolchain.nix | 10 ++++++---- 3 files changed, 30 insertions(+), 36 deletions(-) (limited to 'nix') diff --git a/nix/common.nix b/nix/common.nix index 031a75c0..2b9cc751 100644 --- a/nix/common.nix +++ b/nix/common.nix @@ -8,12 +8,12 @@ rec { #sha256 = "1xy9zpypqfxs5gcq5dcla4bfkhxmh5nzn9dyqkr03lqycm9wg5cr"; # NixOS 22.05 - url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.zip"; - sha256 = "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik"; + #url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.zip"; + #sha256 = "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik"; # As of 2022-10-13 - #url = "https://github.com/NixOS/nixpkgs/archive/a3073c49bc0163fea6a121c276f526837672b555.zip"; - #sha256 = "1bz632psfbpmicyzjb8b4265y50shylccvfm6ry6mgnv5hvz324s"; + url = "https://github.com/NixOS/nixpkgs/archive/a3073c49bc0163fea6a121c276f526837672b555.zip"; + sha256 = "1bz632psfbpmicyzjb8b4265y50shylccvfm6ry6mgnv5hvz324s"; }; cargo2nixSrc = fetchGit { # As of 2022-10-14: (TODO) diff --git a/nix/compile.nix b/nix/compile.nix index 62eb956a..9406bb44 100644 --- a/nix/compile.nix +++ b/nix/compile.nix @@ -13,9 +13,10 @@ let log = v: builtins.trace v v; pkgs = import pkgsSrc { - inherit system; + inherit system; crossSystem = { config = target; + isStatic = true; }; overlays = [ cargo2nixOverlay ]; }; @@ -30,26 +31,22 @@ let we emulate the structure of the Rust object created by rustOverlay. In practise, rustOverlay ships rustc+cargo in a single derivation while NixOS ships them in separate ones. We reunite them with symlinkJoin. - */ - /* - rustToolchain = { - rustc = pkgs.symlinkJoin { - name = "rust-channel"; - paths = [ - pkgs.rustPlatform.rust.cargo - pkgs.rustPlatform.rust.rustc - ]; - }; - clippy = pkgs.symlinkJoin { - name = "clippy-channel"; - paths = [ - pkgs.rustPlatform.rust.cargo - pkgs.rustPlatform.rust.rustc - pkgs.clippy - ]; - }; - }.${compiler}; */ + toolchainOptions = + if target == "x86_64-unknown-linux-musl" || target == "aarch64-unknown-linux-musl" then { + rustVersion = "1.63.0"; + extraRustComponents = [ "clippy" ]; + } else { + rustToolchain = pkgs.symlinkJoin { + name = "rust-static-toolchain-${target}"; + paths = [ + pkgs.rustPlatform.rust.cargo + pkgs.rustPlatform.rust.rustc + # clippy not needed, it only runs on amd64 + ]; + }; + }; + buildEnv = (drv: { rustc = drv.setBuildEnv; @@ -57,7 +54,7 @@ let ${drv.setBuildEnv or "" } echo echo --- BUILDING WITH CLIPPY --- - echo + echo export NIX_RUST_BUILD_FLAGS="''${NIX_RUST_BUILD_FLAGS} --deny warnings" export RUSTC="''${CLIPPY_DRIVER}" @@ -215,12 +212,7 @@ let else target; in - pkgs.rustBuilder.makePackageSet { + pkgs.rustBuilder.makePackageSet ({ inherit release packageFun packageOverrides codegenOpts rootFeatures; - - #inherit rustToolchain; - rustVersion = "1.63.0"; - target = rustTarget; - extraRustComponents = [ "clippy" ]; - } + } // toolchainOptions) diff --git a/nix/toolchain.nix b/nix/toolchain.nix index d6fe295a..079fcf13 100644 --- a/nix/toolchain.nix +++ b/nix/toolchain.nix @@ -6,14 +6,17 @@ with import ./common.nix; let platforms = [ - "x86_64-unknown-linux-musl" + #"x86_64-unknown-linux-musl" "i686-unknown-linux-musl" - "aarch64-unknown-linux-musl" + #"aarch64-unknown-linux-musl" "armv6l-unknown-linux-musleabihf" ]; pkgsList = builtins.map (target: import pkgsSrc { inherit system; - crossSystem = { config = target; }; + crossSystem = { + config = target; + isStatic = true; + }; overlays = [ cargo2nixOverlay ]; }) platforms; pkgsHost = import pkgsSrc {}; @@ -25,7 +28,6 @@ in lib.flatten (builtins.map (pkgs: [ pkgs.rustPlatform.rust.rustc pkgs.rustPlatform.rust.cargo - pkgs.clippy pkgs.buildPackages.stdenv.cc ]) pkgsList) ++ [ kaniko -- cgit v1.2.3 From 00cf076412b88ae0c6254fc16b9bbe661c4f01f2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 18 Oct 2022 12:15:45 +0200 Subject: Fix cargo2nix feature discovery --- nix/common.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nix') diff --git a/nix/common.nix b/nix/common.nix index 2b9cc751..576121e4 100644 --- a/nix/common.nix +++ b/nix/common.nix @@ -19,7 +19,7 @@ rec { # As of 2022-10-14: (TODO) url = "https://github.com/Alexis211/cargo2nix"; ref = "custom_unstable"; - rev = "15543df35485bef9e2092391ecafa78eae5fa740"; + rev = "a7a61179b66054904ef6a195d8da736eaaa06c36"; }; /* -- cgit v1.2.3 From 667ca9d3e3971d57d1c95defbff790ab5dc26221 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 18 Oct 2022 12:48:31 +0200 Subject: Cleanup nix scripts --- nix/common.nix | 10 +--------- nix/compile.nix | 5 +++-- 2 files changed, 4 insertions(+), 11 deletions(-) (limited to 'nix') diff --git a/nix/common.nix b/nix/common.nix index 576121e4..90e3afaf 100644 --- a/nix/common.nix +++ b/nix/common.nix @@ -3,20 +3,12 @@ rec { * Fixed dependencies */ pkgsSrc = fetchTarball { - # As of 2021-10-04 - #url = "https://github.com/NixOS/nixpkgs/archive/b27d18a412b071f5d7991d1648cfe78ee7afe68a.tar.gz"; - #sha256 = "1xy9zpypqfxs5gcq5dcla4bfkhxmh5nzn9dyqkr03lqycm9wg5cr"; - - # NixOS 22.05 - #url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.zip"; - #sha256 = "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik"; - # As of 2022-10-13 url = "https://github.com/NixOS/nixpkgs/archive/a3073c49bc0163fea6a121c276f526837672b555.zip"; sha256 = "1bz632psfbpmicyzjb8b4265y50shylccvfm6ry6mgnv5hvz324s"; }; cargo2nixSrc = fetchGit { - # As of 2022-10-14: (TODO) + # As of 2022-10-18: two small patches over unstable branch, one for clippy and one to fix feature detection url = "https://github.com/Alexis211/cargo2nix"; ref = "custom_unstable"; rev = "a7a61179b66054904ef6a195d8da736eaaa06c36"; diff --git a/nix/compile.nix b/nix/compile.nix index 9406bb44..45c7efa1 100644 --- a/nix/compile.nix +++ b/nix/compile.nix @@ -23,8 +23,9 @@ let /* Cargo2nix is built for rustOverlay which installs Rust from Mozilla releases. - We want our own Rust to avoid incompatibilities, like we had with musl 1.2.0. - rustc was built with musl < 1.2.0 and nix shipped musl >= 1.2.0 which lead to compilation breakage. + This is fine for 64-bit platforms, but for 32-bit platforms, we need our own Rust + to avoid incompatibilities with time_t between different versions of musl + (>= 1.2.0 shipped by NixOS, < 1.2.0 with which rustc was built), which lead to compilation breakage. So we want a Rust release that is bound to our Nix repository to avoid these problems. See here for more info: https://musl.libc.org/time64.html Because Cargo2nix does not support the Rust environment shipped by NixOS, -- cgit v1.2.3 From 002b9fc50c5b69e0e10c84e4db5ecea1b3941fad Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 18 Oct 2022 18:38:20 +0200 Subject: Add TLS support for Consul discovery + refactoring --- nix/compile.nix | 1 + 1 file changed, 1 insertion(+) (limited to 'nix') diff --git a/nix/compile.nix b/nix/compile.nix index 45c7efa1..303c3aa4 100644 --- a/nix/compile.nix +++ b/nix/compile.nix @@ -177,6 +177,7 @@ let "garage/sled" "garage/k2v" ] ++ (if release then [ + "garage/consul-discovery" "garage/kubernetes-discovery" "garage/metrics" "garage/telemetry-otlp" -- cgit v1.2.3