Rename crates nix- -> nix-bindings-

This way, the crates can be published without interfering with
potential future non-bindings `nix-` crates, if Nix proper wants to
have native rust code, for instance.
This commit is contained in:
Robert Hensing 2025-10-04 02:44:22 +02:00
parent 4b13929db3
commit b3171585d1
30 changed files with 209 additions and 1853 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "nix-bindings-bindgen-raw"
version = "0.1.0"
edition = "2021"
build = "build.rs"
license = "LGPL-2.1"
[lib]
path = "src/lib.rs"
[build-dependencies]
bindgen = "0.69"
pkg-config = "0.3"

View file

@ -0,0 +1,12 @@
# nix-bindings-bindgen-raw
This crate contains generated bindings for the Nix C API.
**You should not have to use this crate directly,** and so you should probably not add it to your dependencies.
Instead, use the `nix-bindings-util`, `nix-bindings-store` and `nix-bindings-expr` crates, which _should_ be sufficient.
## Design
Rust bindgen currently does not allow "layered" libraries to be split into separate crates.
For example, the expr crate would have all-new types that are distinct and incompatible with the store crate.
Ideally bindgen will support reusing already generated modules, and we could move the code generation into the appropriate crates, so that the system dependencies of each crate become accurate.

View file

@ -0,0 +1,60 @@
use std::env;
use std::path::PathBuf;
#[derive(Debug)]
struct StripNixPrefix {}
impl bindgen::callbacks::ParseCallbacks for StripNixPrefix {
fn item_name(&self, name: &str) -> Option<String> {
name.strip_prefix("nix_").map(String::from)
}
}
fn main() {
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=include/nix-c-raw.h");
println!("cargo:rustc-link-lib=nixflake");
// https://rust-lang.github.io/rust-bindgen/library-usage.html
let bindings = bindgen::Builder::default()
.header("include/nix-c-raw.h")
// Find the includes
.clang_args(c_headers())
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.parse_callbacks(Box::new(StripNixPrefix {}))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn c_headers() -> Vec<String> {
let mut args = Vec::new();
// args.push("-isystem".to_string());
for path in pkg_config::probe_library("nix-flake-c")
.unwrap()
.include_paths
.iter()
{
args.push(format!("-I{}", path.to_str().unwrap()));
}
for path in pkg_config::probe_library("bdw-gc")
.unwrap()
.include_paths
.iter()
{
args.push(format!("-I{}", path.to_str().unwrap()));
}
// write to stderr for debugging
eprintln!("c_headers: {:?}", args);
args
}

View file

@ -0,0 +1,7 @@
#include <nix_api_util.h>
#include <nix_api_store.h>
#define GC_THREADS
#include <gc/gc.h>
#include <nix_api_expr.h>
#include <nix_api_value.h>
#include <nix_api_flake.h>

View file

@ -0,0 +1,22 @@
//! Raw bindings to Nix C API
//!
//! This crate contains automatically generated bindings from the Nix C headers.
//! The bindings are generated by bindgen and include C-style naming conventions
//! and documentation comments that don't always conform to Rust standards.
//!
//! Normally you don't have to use this crate directly.
//! Instead use `nix-store` and `nix-expr`.
// Standard bindgen suppressions for C naming conventions
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
// Rustdoc suppressions for generated C documentation
// The C headers contain Doxygen-style documentation that doesn't translate
// well to Rust's rustdoc format, causing various warnings:
#![allow(rustdoc::broken_intra_doc_links)] // @param[in]/[out] references don't resolve
#![allow(rustdoc::bare_urls)] // C docs may contain unescaped URLs
#![allow(rustdoc::invalid_html_tags)] // Doxygen HTML tags like <setting>
#![allow(rustdoc::invalid_codeblock_attributes)] // C code examples may use unsupported attributes
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));