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" ];
buildTargetsBySystem = {
"x86_64-linux" = "x86_64-unknown-linux-musl";
"aarch64-linux" = "aarch64-unknown-linux-musl";
};
pkgs = import nixpkgs { eachCrossSystem = callback: (builtins.foldl'
inherit system; (overall: buildSystem: overall // {
overlays = [ (import rust-overlay) ]; ${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 { naersk-lib = pkgs.callPackage naersk {
cargo = toolchain; cargo = pkgs.rust-toolchain;
rustc = toolchain; rustc = pkgs.rust-toolchain;
}; };
rustEnv = mkRustEnv pkgs;
opensslEnv = {
OPENSSL_STATIC = "1";
OPENSSL_LIB_DIR = "${pkgs.pkgsStatic.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.pkgsStatic.openssl.dev}/include";
};
in in
{ naersk-lib.buildPackage ({
defaultPackage = 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"
]