Refactor nix flake to allow cross compiling

This commit is contained in:
Brian Picciano 2023-11-08 21:40:25 +01:00
parent 6cabb9b742
commit ab2e5fb55a
2 changed files with 64 additions and 27 deletions

View File

@ -8,43 +8,77 @@
}; };
outputs = { self, nixpkgs, utils, naersk, rust-overlay }: outputs = { self, nixpkgs, utils, naersk, rust-overlay }:
utils.lib.eachDefaultSystem (system:
let let
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
pkgs = import nixpkgs { buildTargetsBySystem = {
inherit system; "x86_64-linux" = "x86_64-unknown-linux-musl";
overlays = [ (import rust-overlay) ]; "aarch64-linux" = "aarch64-unknown-linux-musl";
}; };
toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; eachCrossSystem = callback: (builtins.foldl'
(overall: buildSystem: overall // {
${buildSystem} = (builtins.foldl'
(inner: hostSystem: inner // {
"cross-${hostSystem}" = callback buildSystem hostSystem;
})
{ default = callback buildSystem buildSystem; }
supportedSystems
);
})
{}
supportedSystems
);
naersk-lib = pkgs.callPackage naersk { mkPkgs = buildSystem: hostSystem: import nixpkgs {
cargo = toolchain; system = buildSystem;
rustc = toolchain; 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;
});
opensslEnv = { mkRustEnv = pkgs: {
OPENSSL_STATIC = "1"; OPENSSL_STATIC = "1";
OPENSSL_LIB_DIR = "${pkgs.pkgsStatic.openssl.out}/lib"; OPENSSL_LIB_DIR = "${pkgs.pkgsStatic.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.pkgsStatic.openssl.dev}/include"; 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 in
{ {
defaultPackage = naersk-lib.buildPackage ({ packages = eachCrossSystem (buildSystem: hostSystem: let
pkgs = mkPkgs buildSystem hostSystem;
naersk-lib = pkgs.callPackage naersk {
cargo = pkgs.rust-toolchain;
rustc = pkgs.rust-toolchain;
};
rustEnv = mkRustEnv pkgs;
in
naersk-lib.buildPackage ({
src = ./.; src = ./.;
doCheck = false; doCheck = false;
nativeBuildInputs = [ pkgs.pkgsStatic.stdenv.cc ]; nativeBuildInputs = [ pkgs.pkgsStatic.stdenv.cc ];
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; } // rustEnv)
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static"; );
} // opensslEnv);
devShell = pkgs.mkShell ({ devShells = eachCrossSystem (buildSystem: hostSystem: let
pkgs = mkPkgs buildSystem hostSystem;
rustEnv = mkRustEnv pkgs;
in
pkgs.mkShell ({
nativeBuildInputs = [ nativeBuildInputs = [
pkgs.stdenv.cc pkgs.stdenv.cc
pkgs.openssl pkgs.openssl
toolchain pkgs.rust-toolchain
pkgs.nmap # ncat pkgs.nmap # ncat
]; ];
shellHook = '' shellHook = ''
@ -54,6 +88,6 @@
export DOMANI_CONFIG_PATH=config-dev.yml export DOMANI_CONFIG_PATH=config-dev.yml
fi fi
''; '';
} // opensslEnv); } // rustEnv));
}); };
} }

View File

@ -1,4 +1,7 @@
[toolchain] [toolchain]
channel = "nightly-2023-07-23" channel = "nightly-2023-07-23"
components = [ "rustfmt", "rustc-dev", "clippy", "cargo" ] components = [ "rustfmt", "rustc-dev", "clippy", "cargo" ]
targets = [ "x86_64-unknown-linux-musl" ] targets = [
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl"
]