From 25f55cf24dc96637ed13d2a0a109b8b6eb2add0f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 3 Dec 2023 15:57:49 +0100 Subject: [PATCH] use fenix/naersk for nix building, got local x86_64-linux build working --- .cargo/config.toml | 3 - .gitignore | 4 +- default.nix | 215 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 172 insertions(+), 50 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index d29d6c33..00000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,3 +0,0 @@ -[target.x86_64-unknown-linux-gnu] -linker = "clang" -rustflags = ["-C", "link-arg=-fuse-ld=mold"] diff --git a/.gitignore b/.gitignore index ef7a56eb..bd514c1c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ /pki **/*.rs.bk *.swp -/.direnv \ No newline at end of file +/.direnv +/.cargo +/result diff --git a/default.nix b/default.nix index ecdf6436..40a01ee4 100644 --- a/default.nix +++ b/default.nix @@ -1,56 +1,179 @@ -{ system ? builtins.currentSystem, git_version ? null, }: +{ + buildSystem ? builtins.currentSystem, + targetSystem ? buildSystem, + gitVersion ? null, + release ? false, + features ? null, +}: with import ./nix/common.nix; let - pkgs = import pkgsSrc { }; - compile = import ./nix/compile.nix; - - build_debug_and_release = (target: { - debug = (compile { - inherit system target git_version pkgsSrc cargo2nixOverlay; - release = false; - }).workspace.garage { compileMode = "build"; }; - - release = (compile { - inherit system target git_version pkgsSrc cargo2nixOverlay; - release = true; - }).workspace.garage { compileMode = "build"; }; - }); - - test = (rustPkgs: - pkgs.symlinkJoin { - name = "garage-tests"; - paths = - builtins.map (key: rustPkgs.workspace.${key} { compileMode = "test"; }) - (builtins.attrNames rustPkgs.workspace); - }); + newBuildTarget = { + nixPkgsSystem, + rustTarget ? nixPkgsSystem, + depsBuildBuild ? pkgsCross: [], + }: { + inherit nixPkgsSystem rustTarget depsBuildBuild; + }; -in { - pkgs = { - amd64 = build_debug_and_release "x86_64-unknown-linux-musl"; - i386 = build_debug_and_release "i686-unknown-linux-musl"; - arm64 = build_debug_and_release "aarch64-unknown-linux-musl"; - arm = build_debug_and_release "armv6l-unknown-linux-musleabihf"; + # centralize per-target configuration in a single place. + buildTargets = { + "x86_64-linux" = newBuildTarget { + nixPkgsSystem = "x86_64-unknown-linux-musl"; + }; + + "i686-linux" = newBuildTarget { + nixPkgsSystem = "i686-unknown-linux-musl"; + }; + + "aarch64-linux" = newBuildTarget { + nixPkgsSystem = "aarch64-unknown-linux-musl"; + }; + + # Old Raspberry Pi's + "armv6l-linux" = newBuildTarget { + nixPkgsSystem = "armv6l-unknown-linux-musleabihf"; + rustTarget = "arm-unknown-linux-musleabihf"; + }; + + "x86_64-windows" = newBuildTarget { + nixPkgsSystem = "x86_64-w64-mingw32"; + rustTarget = "x86_64-pc-windows-gnu"; + depsBuildBuild = pkgsCross: [ + pkgsCross.stdenv.cc + pkgsCross.windows.pthreads + ]; + }; }; - test = { - amd64 = test (compile { - inherit system git_version pkgsSrc cargo2nixOverlay; - target = "x86_64-unknown-linux-musl"; - features = [ - "garage/bundled-libs" - "garage/k2v" - "garage/sled" + + buildTarget = buildTargets.${targetSystem}; + + pkgs = import pkgsSrc { system = buildSystem; }; + pkgsCross = import pkgsSrc { + system = buildSystem; + crossSystem.config = buildTarget.nixPkgsSystem; + }; + + rustTarget = buildTarget.rustTarget; + + toolchain = let + fenix = import (pkgs.fetchFromGitHub { + owner = "nix-community"; + repo = "fenix"; + rev = "81ab0b4f7ae9ebb57daa0edf119c4891806e4d3a"; + hash = "sha256-bZmI7ytPAYLpyFNgj5xirDkKuAniOkj1xHdv5aIJ5GM="; + }) { + system = buildSystem; + }; + + mkToolchain = fenixTarget: fenixTarget.toolchainOf { + channel = "1.68.2"; + sha256 = "sha256-4vetmUhTUsew5FODnjlnQYInzyLNyDwocGa4IvMk3DM="; + }; + in + fenix.combine [ + (mkToolchain fenix).rustc + (mkToolchain fenix).cargo + (mkToolchain fenix.targets.${rustTarget}).rust-std + ]; + + naersk = let + naerskSrc = pkgs.fetchFromGitHub { + owner = "nix-community"; + repo = "naersk"; + rev = "d9a33d69a9c421d64c8d925428864e93be895dcc"; + hash = "sha256-e136hTT7LqQ2QjOTZQMW+jnsevWwBpMj78u6FRUsH9I="; + }; + in + pkgs.callPackages naerskSrc { + cargo = toolchain; + rustc = toolchain; + }; + + + # TODO build all of these + #build_debug_and_release = (target: { + # debug = (compile { + # inherit system target gitVersion pkgsSrc cargo2nixOverlay; + # release = false; + # }).workspace.garage { compileMode = "build"; }; + + # release = (compile { + # inherit system target gitVersion pkgsSrc cargo2nixOverlay; + # release = true; + # }).workspace.garage { compileMode = "build"; }; + #}); + + #test = (rustPkgs: + # pkgs.symlinkJoin { + # name = "garage-tests"; + # paths = + # builtins.map (key: rustPkgs.workspace.${key} { compileMode = "test"; }) + # (builtins.attrNames rustPkgs.workspace); + # }); + + builtFeatures = if features != null then + features + else ( + [ "garage/bundled-libs" "garage/sled" "garage/lmdb" "garage/k2v" ] ++ ( + if release then [ + "garage/consul-discovery" + "garage/kubernetes-discovery" + "garage/metrics" + "garage/telemetry-otlp" "garage/lmdb" "garage/sqlite" - ]; - }); - }; - clippy = { - amd64 = (compile { - inherit system git_version pkgsSrc cargo2nixOverlay; - target = "x86_64-unknown-linux-musl"; - compiler = "clippy"; - }).workspace.garage { compileMode = "build"; }; + ] else [ ] + ) + ); + +in { + + inherit pkgs; + + release = naersk.buildPackage rec { + inherit release; + + src = ./.; + strictDeps = true; + doCheck = false; + + depsBuildBuild = (buildTarget.depsBuildBuild pkgsCross) ++ [ + pkgs.protobuf + ]; + + cargoBuildOptions = prev: prev++[ + "--features=${builtins.concatStringsSep "," builtFeatures}" + ]; + + OPENSSL_STATIC = "1"; + OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${pkgsCross.pkgsStatic.openssl.dev}/include"; + + # Required because ring crate is special. This also seems to have + # fixed some issues with the x86_64-windows cross-compile :shrug: + TARGET_CC = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc"; + + CARGO_BUILD_TARGET = rustTarget; + CARGO_BUILD_RUSTFLAGS = [ + "-C" "target-feature=+crt-static" + + # -latomic is required to build openssl-sys for armv6l-linux, but + # it doesn't seem to hurt any other builds. + "-C" "link-args=-static -latomic" + + # https://github.com/rust-lang/cargo/issues/4133 + "-C" "linker=${TARGET_CC}" + ]; }; + + # TODO + #clippy = { + # amd64 = (compile { + # inherit system gitVersion pkgsSrc cargo2nixOverlay; + # target = "x86_64-unknown-linux-musl"; + # compiler = "clippy"; + # }).workspace.garage { compileMode = "build"; }; + #}; }