- 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
Last active
September 25, 2024 02:14
-
-
Save yihuang/b874efb97e99d4b6d12bf039f98ae31e to your computer and use it in GitHub Desktop.
build rust project using bindgen with nix
This file contains 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
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"); | |
} |
This file contains 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
[package] | |
name = "test-rust" | |
version = "0.1.0" | |
authors = ["yihuang <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
[build-dependencies] | |
bindgen = "0.54" |
This file contains 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
{ 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" | |
''; | |
} |
This file contains 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
pub mod bindings { | |
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | |
} | |
fn main() { | |
println!("Hello, world! {}", bindings::TEST); | |
} |
This file contains 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
#ifndef TEST_H | |
#define TEST_H | |
#include <stdint.h> | |
#include <stdio.h> | |
#define TEST 0 | |
#endif /* TEST_H */ |
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 ];
};
nativeBuildInputs = [ rustPlatform.bindgenHook ];`
oh my god THANK YOU
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Words can't express how grateful I am for this information.