2022-02-16 05:42:59 +03:00
|
|
|
{
|
|
|
|
description = "Cross compiling a rust program using rust-overlay";
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
|
|
|
|
2024-08-30 02:29:16 +03:00
|
|
|
crane.url = "github:ipetkov/crane";
|
2022-02-16 05:42:59 +03:00
|
|
|
|
2022-04-28 04:10:22 +03:00
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
rust-overlay = {
|
|
|
|
url = "github:oxalica/rust-overlay";
|
2024-07-14 19:36:42 +03:00
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
2022-02-16 05:42:59 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-24 02:20:22 +03:00
|
|
|
outputs = { nixpkgs, crane, flake-utils, rust-overlay, ... }:
|
2023-04-04 08:08:23 +03:00
|
|
|
flake-utils.lib.eachDefaultSystem (localSystem:
|
2022-02-16 05:42:59 +03:00
|
|
|
let
|
|
|
|
# Replace with the system you want to build for
|
|
|
|
crossSystem = "aarch64-linux";
|
|
|
|
|
|
|
|
pkgs = import nixpkgs {
|
|
|
|
inherit crossSystem localSystem;
|
|
|
|
overlays = [ (import rust-overlay) ];
|
|
|
|
};
|
|
|
|
|
2024-06-29 21:29:52 +03:00
|
|
|
craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default);
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
# Note: we have to use the `callPackage` approach here so that Nix
|
|
|
|
# can "splice" the packages in such a way that dependencies are
|
|
|
|
# compiled for the appropriate targets. If we did not do this, we
|
|
|
|
# would have to manually specify things like
|
|
|
|
# `nativeBuildInputs = with pkgs.pkgsBuildHost; [ someDep ];` or
|
|
|
|
# `buildInputs = with pkgs.pkgsHostHost; [ anotherDep ];`.
|
|
|
|
#
|
|
|
|
# Normally you can stick this function into its own file and pass
|
|
|
|
# its path to `callPackage`.
|
|
|
|
crateExpression =
|
2023-05-08 03:18:21 +03:00
|
|
|
{ openssl
|
2022-11-20 03:08:15 +03:00
|
|
|
, libiconv
|
|
|
|
, lib
|
2022-02-16 05:42:59 +03:00
|
|
|
, pkg-config
|
|
|
|
, qemu
|
|
|
|
, stdenv
|
|
|
|
}:
|
|
|
|
craneLib.buildPackage {
|
2024-06-11 06:53:46 +03:00
|
|
|
src = craneLib.cleanCargoSource ./.;
|
2023-10-16 01:25:34 +03:00
|
|
|
strictDeps = true;
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
# Build-time tools which are target agnostic. build = host = target = your-machine.
|
|
|
|
# Emulators should essentially also go `nativeBuildInputs`. But with some packaging issue,
|
|
|
|
# currently it would cause some rebuild.
|
|
|
|
# We put them here just for a workaround.
|
|
|
|
# See: https://github.com/NixOS/nixpkgs/pull/146583
|
2023-08-11 03:13:17 +03:00
|
|
|
depsBuildBuild = [
|
|
|
|
qemu
|
|
|
|
];
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
# Dependencies which need to be build for the current platform
|
|
|
|
# on which we are doing the cross compilation. In this case,
|
|
|
|
# pkg-config needs to run on the build platform so that the build
|
|
|
|
# script can find the location of openssl. Note that we don't
|
|
|
|
# need to specify the rustToolchain here since it was already
|
|
|
|
# overridden above.
|
2022-11-20 03:08:15 +03:00
|
|
|
nativeBuildInputs = [
|
|
|
|
pkg-config
|
2024-06-03 05:48:19 +03:00
|
|
|
stdenv.cc
|
2023-08-11 03:13:17 +03:00
|
|
|
] ++ lib.optionals stdenv.buildPlatform.isDarwin [
|
|
|
|
libiconv
|
2022-11-20 03:08:15 +03:00
|
|
|
];
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
# Dependencies which need to be built for the platform on which
|
|
|
|
# the binary will run. In this case, we need to compile openssl
|
|
|
|
# so that it can be linked with our executable.
|
2022-11-20 03:08:15 +03:00
|
|
|
buildInputs = [
|
|
|
|
# Add additional build inputs here
|
|
|
|
openssl
|
|
|
|
];
|
2022-02-16 05:42:59 +03:00
|
|
|
|
|
|
|
# Tell cargo about the linker and an optional emulater. So they can be used in `cargo build`
|
|
|
|
# and `cargo run`.
|
|
|
|
# Environment variables are in format `CARGO_TARGET_<UPPERCASE_UNDERSCORE_RUST_TRIPLE>_LINKER`.
|
|
|
|
# They are also be set in `.cargo/config.toml` instead.
|
|
|
|
# See: https://doc.rust-lang.org/cargo/reference/config.html#target
|
|
|
|
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = "${stdenv.cc.targetPrefix}cc";
|
|
|
|
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER = "qemu-aarch64";
|
2022-02-25 04:49:22 +03:00
|
|
|
|
|
|
|
# Tell cargo which target we want to build (so it doesn't default to the build system).
|
|
|
|
# We can either set a cargo flag explicitly with a flag or with an environment variable.
|
2022-02-16 05:42:59 +03:00
|
|
|
cargoExtraArgs = "--target aarch64-unknown-linux-gnu";
|
2022-02-25 04:49:22 +03:00
|
|
|
# CARGO_BUILD_TARGET = "aarch64-unknown-linux-gnu";
|
|
|
|
|
2024-06-09 04:15:24 +03:00
|
|
|
# These environment variables may be necessary if any of your dependencies use a
|
2022-02-25 04:49:22 +03:00
|
|
|
# build-script which invokes the `cc` crate to build some other code. The `cc` crate
|
|
|
|
# should automatically pick up on our target-specific linker above, but this may be
|
|
|
|
# necessary if the build script needs to compile and run some extra code on the build
|
|
|
|
# system.
|
|
|
|
HOST_CC = "${stdenv.cc.nativePrefix}cc";
|
2024-06-09 04:15:24 +03:00
|
|
|
TARGET_CC = "${stdenv.cc.targetPrefix}cc";
|
2022-02-16 05:42:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
# Assuming the above expression was in a file called myCrate.nix
|
|
|
|
# this would be defined as:
|
|
|
|
# my-crate = pkgs.callPackage ./myCrate.nix { };
|
|
|
|
my-crate = pkgs.callPackage crateExpression { };
|
|
|
|
in
|
|
|
|
{
|
|
|
|
checks = {
|
|
|
|
inherit my-crate;
|
|
|
|
};
|
|
|
|
|
2022-04-28 05:23:30 +03:00
|
|
|
packages.default = my-crate;
|
2022-02-16 05:42:59 +03:00
|
|
|
|
2022-04-28 05:23:30 +03:00
|
|
|
apps.default = flake-utils.lib.mkApp {
|
2022-02-16 05:42:59 +03:00
|
|
|
drv = pkgs.writeScriptBin "my-app" ''
|
|
|
|
${pkgs.pkgsBuildBuild.qemu}/bin/qemu-aarch64 ${my-crate}/bin/cross-rust-overlay
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|