Created
July 17, 2025 18:47
-
-
Save unkcpz/040f25367a0306659f65cf9cbf3fb12f to your computer and use it in GitHub Desktop.
nix rust compiling aarch64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| # build in this way @johan | |
| # cargo build --target aarch64-unknown-linux-gnu | |
| description = "Rust cross-compilation example with flakes"; | |
| inputs = { | |
| nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; | |
| flake-utils.url = "github:numtide/flake-utils"; | |
| rust-overlay.url = "github:oxalica/rust-overlay"; | |
| }; | |
| outputs = { self, nixpkgs, flake-utils, rust-overlay, ... }: | |
| flake-utils.lib.eachDefaultSystem (system: | |
| let | |
| overlays = [ rust-overlay.overlays.default ]; | |
| pkgs = import nixpkgs { | |
| inherit system overlays; | |
| }; | |
| rustTarget = "aarch64-unknown-linux-gnu"; | |
| rustToolchain = pkgs.rust-bin.stable.latest.default.override { | |
| targets = [ rustTarget ]; | |
| }; | |
| crossPkgs = pkgs.pkgsCross.aarch64-multiplatform; | |
| in { | |
| devShells.default = pkgs.mkShell { | |
| packages = [ | |
| rustToolchain | |
| crossPkgs.buildPackages.gcc | |
| crossPkgs.buildPackages.binutils | |
| pkgs.pkg-config | |
| ]; | |
| shellHook = '' | |
| export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=${crossPkgs.buildPackages.gcc}/bin/aarch64-linux-gnu-gcc | |
| echo "Ready to cross-compile to ${rustTarget}" | |
| ''; | |
| }; | |
| # Optionally a custom build | |
| packages.default = pkgs.rustPlatform.buildRustPackage { | |
| pname = "your-project"; | |
| version = "0.1.0"; | |
| src = ./.; | |
| cargoLock = { | |
| lockFile = ./Cargo.lock; | |
| }; | |
| buildInputs = with crossPkgs; [ ]; | |
| target = rustTarget; | |
| RUSTFLAGS = "-C linker=${crossPkgs.buildPackages.gcc}/bin/aarch64-linux-gnu-gcc"; | |
| }; | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment