Hi there. I tried hard to put together a “stitched” flake to cross-compile rust projects for Raspberry Pi:
{
description = "The Rust cross-compiling devShell for Raspberry Pi 3/4/5";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, rust-overlay }: let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
crossSystem.config = "aarch64-unknown-linux-gnu";
};
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
nativeBuildInputs = with pkgs; [
rustToolchain
pkg-config ];
buildInputs = with pkgs; [
#glibc.static
protobuf
openssl
libiio
];
in with pkgs; {
devShells.x86_64-linux.default = callPackage (
{ mkShell, pkg-config }:
mkShell {
inherit buildInputs nativeBuildInputs;
}
) {};
};
}
This works fine when building dynamically linked binaries, but if I uncomment the glibc.static
package and try to build statically linked binaries (with rustflags
= ["-C", "target-feature=+crt-static"]
), it can’t find libiio
:
/nix/store/<digest>-aarch64-unknown-linux-gnu-binutils-2.40/bin/aarch64-unknown-linux-gnu-ld: cannot find -liio: No such file or directory
Digging further into the build log, -liio
is populated, but not -L${libdir}
.
I don’t know how to check if packages are set up properly because pkg-config
throws me an error message: cannot execute binary file: Exec format error.
Nevertheless, I do find the package under /nix/store/<digest>-libiio-aarch64-unknown-linux-gnu-0.24-dev/lib/pkgconfig/libiio.pc
, and it says:
prefix=/nix/store/bjs21s0av4qzpmqyfw8xgfmaa2440ia7-libiio-aarch64-unknown-linux-gnu-0.24
exec_prefix=${prefix}
libdir=/nix/store/gpp73c39nmimlqx60q3jvm6na727zy91-libiio-aarch64-unknown-linux-gnu-0.24-lib/lib
includedir=/nix/store/6d0q5ma128fcxmv6mb2190b3p186gali-libiio-aarch64-unknown-linux-gnu-0.24-dev/include
Name: libiio
Description: Library for interfacing IIO devices
Version: 0.24
Requires:
Libs: -L${libdir} -liio
Cflags: -I${includedir}
My gut feeling is that adding the library path manually is a bad idea because the digest will change over time. Please advise the proper practice to set up libraries with flakes.
Thanks.