Skip to content

Instantly share code, notes, and snippets.

@yihuang
Last active April 12, 2024 18:38
Show Gist options
  • Save yihuang/b874efb97e99d4b6d12bf039f98ae31e to your computer and use it in GitHub Desktop.
Save yihuang/b874efb97e99d4b6d12bf039f98ae31e to your computer and use it in GitHub Desktop.
build rust project using bindgen with nix
  • setup remote builder on mac
  • run nix-build --argstr system "x86_64-linux" get error:
    --- stderr
    test.h:4:10: fatal error: 'stdint.h' file not found
    test.h:4:10: fatal error: 'stdint.h' file not found, err: true
    thread 'main' panicked at 'error generating bindings: ()', build.rs:5:20
    
use std::env;
use std::path::PathBuf;
fn main() {
let build = bindgen::Builder::default();
let bindings = build
.header("test.h")
.generate()
.expect("error generating bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("failed to write bindings to file");
}
[package]
name = "test-rust"
version = "0.1.0"
authors = ["yihuang <yi.codeplayer@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[build-dependencies]
bindgen = "0.54"
{ system ? builtins.currentSystem, pkgs ? import <nixpkgs> { inherit system; } }:
with pkgs;
rustPlatform.buildRustPackage {
pname = "test-rust";
version = "0.0.1";
src = lib.cleanSource ./.;
cargoSha256 = sha256:11d5mdm891drmn2z31mj3921kzlsqpc7qzivnc0x8qzvnk4plp3s;
buildInputs = [clang];
LIBCLANG_PATH="${llvmPackages.libclang}/lib";
buildInputs = [llvmPackages.libclang.lib stdenv.cc.libc];
LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib";
preConfigure = ''
export BINDGEN_EXTRA_CLANG_ARGS="-isystem ${clang}/resource-root/include $NIX_CFLAGS_COMPILE"
'';
}
pub mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
fn main() {
println!("Hello, world! {}", bindings::TEST);
}
#ifndef TEST_H
#define TEST_H
#include <stdint.h>
#include <stdio.h>
#define TEST 0
#endif /* TEST_H */
@bobvanderlinden
Copy link

This took me quite a while to figure out what was going wrong building a large project that included rust-bindgen somewhere. For anyone else running into issues with rust-bindgen while packaging for Nix: use

nativeBuildInputs = [ rustPlatform.bindgenHook ];`

@nuxeh
Copy link

nuxeh commented Mar 3, 2023

nativeBuildInputs = [ rustPlatform.bindgenHook ];`

Words can't express how grateful I am for this information.

@surprisetalk
Copy link

surprisetalk commented Apr 12, 2024

Life-changing information! Thank you!! This worked for us when trying to build from GitHub Actions:

        defaultPackage = with pkgs; rustPlatform.buildRustPackage {
          # ...
          cargoLock = {
            lockFile = ./Cargo.lock;
            allowBuiltinFetchGit = true;
          };
          nativeBuildInputs = [ cmake llvmPackages_latest.llvm rustPlatform.bindgenHook ];
          buildInputs = if stdenv.isLinux then [ ] else [ libiconv darwin.apple_sdk.frameworks.SystemConfiguration ];
        };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment