diff --git a/flake.nix b/flake.nix index ce28751..0e90f38 100644 --- a/flake.nix +++ b/flake.nix @@ -8,43 +8,77 @@ }; outputs = { self, nixpkgs, utils, naersk, rust-overlay }: - utils.lib.eachDefaultSystem (system: - let + let + supportedSystems = [ "x86_64-linux" "aarch64-linux" ]; + buildTargetsBySystem = { + "x86_64-linux" = "x86_64-unknown-linux-musl"; + "aarch64-linux" = "aarch64-unknown-linux-musl"; + }; - pkgs = import nixpkgs { - inherit system; - overlays = [ (import rust-overlay) ]; - }; + eachCrossSystem = callback: (builtins.foldl' + (overall: buildSystem: overall // { + ${buildSystem} = (builtins.foldl' + (inner: hostSystem: inner // { + "cross-${hostSystem}" = callback buildSystem hostSystem; + }) + { default = callback buildSystem buildSystem; } + supportedSystems + ); + }) + {} + supportedSystems + ); - toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + mkPkgs = buildSystem: hostSystem: import nixpkgs { + system = buildSystem; + overlays = [ + (import rust-overlay) + (final: prev: { + rust-toolchain = prev.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + }) + ]; + } // (if buildSystem == hostSystem then {} else { + # The nixpkgs cache doesn't have any packages where cross-compiling has + # been enabled, even if the target platform is actually the same as the + # build platform (and therefore it's not really cross-compiling). So we + # only set up the cross-compiling config if the target platform is + # different. + crossSystem.config = hostSystem; + }); + mkRustEnv = pkgs: { + 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"; + }; + in + { + packages = eachCrossSystem (buildSystem: hostSystem: let + pkgs = mkPkgs buildSystem hostSystem; naersk-lib = pkgs.callPackage naersk { - cargo = toolchain; - rustc = toolchain; + cargo = pkgs.rust-toolchain; + rustc = pkgs.rust-toolchain; }; - - opensslEnv = { - OPENSSL_STATIC = "1"; - OPENSSL_LIB_DIR = "${pkgs.pkgsStatic.openssl.out}/lib"; - OPENSSL_INCLUDE_DIR = "${pkgs.pkgsStatic.openssl.dev}/include"; - }; - + rustEnv = mkRustEnv pkgs; in - { - defaultPackage = naersk-lib.buildPackage ({ + naersk-lib.buildPackage ({ src = ./.; doCheck = false; nativeBuildInputs = [ pkgs.pkgsStatic.stdenv.cc ]; - CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; - CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static"; - } // opensslEnv); + } // rustEnv) + ); - devShell = pkgs.mkShell ({ + devShells = eachCrossSystem (buildSystem: hostSystem: let + pkgs = mkPkgs buildSystem hostSystem; + rustEnv = mkRustEnv pkgs; + in + pkgs.mkShell ({ nativeBuildInputs = [ pkgs.stdenv.cc pkgs.openssl - toolchain - + pkgs.rust-toolchain pkgs.nmap # ncat ]; shellHook = '' @@ -54,6 +88,6 @@ export DOMANI_CONFIG_PATH=config-dev.yml fi ''; - } // opensslEnv); - }); + } // rustEnv)); + }; } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index ebd487e..1917628 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,7 @@ [toolchain] channel = "nightly-2023-07-23" components = [ "rustfmt", "rustc-dev", "clippy", "cargo" ] -targets = [ "x86_64-unknown-linux-musl" ] +targets = [ + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl" +]