{ inputs = { naersk.url = "github:nix-community/naersk/master"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; utils.url = "github:numtide/flake-utils"; rust-overlay.url = "github:oxalica/rust-overlay"; }; outputs = { self, nixpkgs, utils, naersk, rust-overlay }: let supportedSystems = [ "x86_64-linux" "aarch64-linux" ]; buildTargetsBySystem = { "x86_64-linux" = "x86_64-unknown-linux-musl"; "aarch64-linux" = "aarch64-unknown-linux-musl"; }; eachCrossSystem = callback: (builtins.foldl' (overall: buildSystem: overall // { ${buildSystem} = (builtins.foldl' (inner: hostSystem: inner // { "cross-${hostSystem}" = callback buildSystem hostSystem; }) { default = callback buildSystem buildSystem; } supportedSystems ); }) {} supportedSystems ); 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 = crossPkgs: hostSystem: { OPENSSL_STATIC = "1"; 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 buildSystem; crossPkgs = mkPkgs buildSystem hostSystem; naersk-lib = pkgs.callPackage naersk { cargo = pkgs.rust-toolchain; rustc = pkgs.rust-toolchain; }; rustEnv = mkRustEnv crossPkgs hostSystem; in naersk-lib.buildPackage ({ src = ./.; doCheck = false; nativeBuildInputs = [ crossPkgs.pkgsStatic.stdenv.cc ]; } // rustEnv) ); devShells = eachCrossSystem (buildSystem: hostSystem: let pkgs = mkPkgs buildSystem buildSystem; crossPkgs = mkPkgs buildSystem hostSystem; rustEnv = mkRustEnv crossPkgs hostSystem; in pkgs.mkShell ({ nativeBuildInputs = [ crossPkgs.stdenv.cc crossPkgs.openssl pkgs.nmap # ncat pkgs.rust-toolchain ]; shellHook = '' export CARGO_HOME=$(pwd)/.cargo if [ -f "config.yml" ]; then export DOMANI_CONFIG_PATH=config-dev.yml fi ''; } // rustEnv)); }; }