Rename nix-bindings-bindgen-raw to nix-bindings-util-sys

Adopt idiomatic Rust `-sys` crate naming convention, aligning with
the direction in PR #37 without adopting the full crate splitting.
This commit is contained in:
Robert Hensing 2026-01-12 19:56:04 +01:00
parent 795dfddc04
commit e6148b587f
25 changed files with 78 additions and 87 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "nix-bindings-util-sys"
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-util-sys
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,31 @@
//! 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`.
// This file must only contain generated code, so that the module-level
// #![allow(...)] attributes don't suppress warnings in hand-written code.
// If you need to add hand-written code, use a submodule to isolate the
// generated code. See:
// https://github.com/nixops4/nixops4/pull/138/commits/330c3881be3d3cf3e59adebbe0ab1c0f15f6d2c9
// Standard bindgen suppressions for C naming conventions
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
// Clippy suppressions for generated C bindings
// bindgen doesn't generate safety docs
#![allow(clippy::missing_safety_doc)]
// 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"));