Skip to content

Instantly share code, notes, and snippets.

@brainrake
Created March 31, 2023 14:31
Show Gist options
  • Save brainrake/9969eea3bb17a403d7238fc80c80ead2 to your computer and use it in GitHub Desktop.
Save brainrake/9969eea3bb17a403d7238fc80c80ead2 to your computer and use it in GitHub Desktop.
cross compile rust to nix
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs @ { self, nixpkgs, fenix, naersk, ... }:
let
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
perSystem = nixpkgs.lib.genAttrs systems;
toolchainFor = system: fenix.packages.${system}.minimal.toolchain;
packageFor = system:
(naersk.lib.${system}.override {
cargo = toolchainFor system;
rustc = toolchainFor system;
}).buildPackage {
name = "piled";
src = ./.;
cargoLock = ./Cargo.lock;
};
packageCrossPiFor = system:
let
target = "aarch64-unknown-linux-gnu";
toolchain = with fenix.packages.${system}; combine [
minimal.cargo
minimal.rustc
targets.${target}.latest.rust-std
];
in
(naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
}).buildPackage {
src = ./.;
cargoLock = ./Cargo.lock;
CARGO_BUILD_TARGET = target;
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER =
"${nixpkgs.legacyPackages.${system}.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/${target}-gcc";
};
deployFor = system: nixpkgs.legacyPackages.${system}.writeShellScriptBin "deploy" ''
echo Not implemented.
exit 1
'';
in
{
packages = perSystem (system: {
default = self.packages.${system}.piled;
piled = packageFor system;
deploy = deployFor system;
# packages cross-compiled for Raspberry Pi 4
crossPi = {
piled = packageCrossPiFor system;
};
});
devShells = perSystem (system: {
default = nixpkgs.legacyPackages.${system}.mkShell {
nativeBuildInputs = [
(toolchainFor system)
(deployFor system)
];
};
});
apps = perSystem
(system: {
deploy = {
type = "app";
program = "${self.packages.${system}.deploy}/bin/deploy";
};
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment