From dce01bd31b37c962039699f7dd306f0de7cd5b43 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 9 Nov 2023 23:52:07 +0100 Subject: [PATCH] Fix cross-compilation --- flake.nix | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/flake.nix b/flake.nix index 0e90f38..afad8bc 100644 --- a/flake.nix +++ b/flake.nix @@ -29,7 +29,7 @@ supportedSystems ); - mkPkgs = buildSystem: hostSystem: import nixpkgs { + mkPkgs = buildSystem: hostSystem: import nixpkgs ({ system = buildSystem; overlays = [ (import rust-overlay) @@ -44,42 +44,49 @@ # only set up the cross-compiling config if the target platform is # different. crossSystem.config = hostSystem; - }); + })); - mkRustEnv = pkgs: { + mkRustEnv = crossPkgs: hostSystem: { OPENSSL_STATIC = "1"; - OPENSSL_LIB_DIR = "${pkgs.pkgsStatic.openssl.out}/lib"; - OPENSSL_INCLUDE_DIR = "${pkgs.pkgsStatic.openssl.dev}/include"; - CARGO_BUILD_TARGET = buildTargetsBySystem.${pkgs.stdenv.hostPlatform.system}; - CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static"; + OPENSSL_LIB_DIR = "${crossPkgs.pkgsStatic.openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${crossPkgs.pkgsStatic.openssl.dev}/include"; + CARGO_BUILD_TARGET = buildTargetsBySystem.${hostSystem}; + CARGO_BUILD_RUSTFLAGS = [ + "-C" "target-feature=+crt-static" + "-C" "linker=${crossPkgs.stdenv.cc}/bin/${crossPkgs.stdenv.cc.targetPrefix}cc" + "-C" "link-arg=-static" + ]; }; + in { packages = eachCrossSystem (buildSystem: hostSystem: let - pkgs = mkPkgs buildSystem hostSystem; + pkgs = mkPkgs buildSystem buildSystem; + crossPkgs = mkPkgs buildSystem hostSystem; naersk-lib = pkgs.callPackage naersk { cargo = pkgs.rust-toolchain; rustc = pkgs.rust-toolchain; }; - rustEnv = mkRustEnv pkgs; + rustEnv = mkRustEnv crossPkgs hostSystem; in naersk-lib.buildPackage ({ src = ./.; doCheck = false; - nativeBuildInputs = [ pkgs.pkgsStatic.stdenv.cc ]; + nativeBuildInputs = [ crossPkgs.pkgsStatic.stdenv.cc ]; } // rustEnv) ); devShells = eachCrossSystem (buildSystem: hostSystem: let - pkgs = mkPkgs buildSystem hostSystem; - rustEnv = mkRustEnv pkgs; + pkgs = mkPkgs buildSystem buildSystem; + crossPkgs = mkPkgs buildSystem hostSystem; + rustEnv = mkRustEnv crossPkgs hostSystem; in pkgs.mkShell ({ nativeBuildInputs = [ - pkgs.stdenv.cc - pkgs.openssl - pkgs.rust-toolchain + crossPkgs.stdenv.cc + crossPkgs.openssl pkgs.nmap # ncat + pkgs.rust-toolchain ]; shellHook = '' export CARGO_HOME=$(pwd)/.cargo