difftreelog
feat basic primop interface
in: trunk
3 files changed
crates/nix-eval/src/lib.rsdiffbeforeafterboth--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -1,12 +1,13 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};
-use std::ptr::null_mut;
+use std::ptr::{null, null_mut};
use std::sync::LazyLock;
+use std::{array, fmt, slice};
use std::{collections::HashMap, path::PathBuf};
-use std::{fmt, slice};
use anyhow::{Context, anyhow, bail};
+use itertools::Itertools;
use serde::Serialize;
use serde::de::DeserializeOwned;
@@ -18,25 +19,26 @@
use self::nix_raw::{
BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,
GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,
- GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder,
- Store as c_store, StorePath as c_store_path, alloc_value, bindings_builder_free,
- bindings_builder_insert, c_context, c_context_create, c_context_free, clear_err, err_code,
- err_info_msg, err_msg, eval_state_build, eval_state_builder_load, eval_state_builder_new,
- eval_state_builder_set_eval_setting, expr_eval_from_string, fetchers_settings,
- fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags,
- flake_lock_flags_free, flake_lock_flags_new, flake_reference,
+ GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder, PrimOp,
+ PrimOpFun, Store as c_store, StorePath as c_store_path, alloc_primop, alloc_value,
+ bindings_builder_free, bindings_builder_insert, c_context, c_context_create, c_context_free,
+ clear_err, copy_value, err_code, err_info_msg, err_msg, eval_state_build,
+ eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,
+ expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,
+ flake_lock, flake_lock_flags, flake_lock_flags_free, flake_lock_flags_new, flake_reference,
flake_reference_and_fragment_from_string, flake_reference_parse_flags,
flake_reference_parse_flags_free, flake_reference_parse_flags_new,
flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,
flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,
get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,
- init_string, libexpr_init, libstore_init, libutil_init, list_builder_free, list_builder_insert,
- locked_flake, locked_flake_free, locked_flake_get_output_attrs, make_attrs,
- make_bindings_builder, make_list, make_list_builder, realised_string, realised_string_free,
- realised_string_get_buffer_size, realised_string_get_buffer_start,
- realised_string_get_store_path, realised_string_get_store_path_count, set_err_msg, setting_set,
- state_free, store_open, store_parse_path, store_path_free, store_path_name, string_realise,
- value, value_call, value_decref, value_incref,
+ init_primop, init_string, libexpr_init, libstore_init, libutil_init, list_builder_free,
+ list_builder_insert, locked_flake, locked_flake_free, locked_flake_get_output_attrs,
+ make_attrs, make_bindings_builder, make_list, make_list_builder, realised_string,
+ realised_string_free, realised_string_get_buffer_size, realised_string_get_buffer_start,
+ realised_string_get_store_path, realised_string_get_store_path_count, register_primop,
+ set_err_msg, setting_set, state_free, store_copy_closure, store_get_fs_closure, store_open,
+ store_parse_path, store_path_free, store_path_name, string_realise, value, value_call,
+ value_decref, value_incref,
};
// Contains macros helpers
@@ -166,6 +168,7 @@
}
}
+#[repr(transparent)]
pub struct NixContext(*mut c_context);
impl NixContext {
pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {
@@ -540,6 +543,7 @@
}
}
+#[repr(transparent)]
pub struct Value(*mut value);
unsafe impl Send for Value {}
@@ -626,6 +630,12 @@
}
impl Value {
+ pub fn new_primop(v: NativeFn) -> Self {
+ let out = Self::new_uninit();
+ with_default_context(|c, _| unsafe { init_primop(c, out.0, v.0) })
+ .expect("primop initialization should not fail");
+ out
+ }
pub fn new_attrs(v: HashMap<&str, Value>) -> Self {
let out = Self::new_uninit();
let mut b = AttrsBuilder::new(v.len());
@@ -913,6 +923,70 @@
nix_logging_cxx::apply_tracing_logger();
}
+unsafe extern "C" fn nix_primop_closure_adapter<const N: usize>(
+ user_data: *mut c_void,
+ context: *mut c_context,
+ state: *mut nix_raw::EvalState,
+ args: *mut *mut value,
+ ret: *mut value,
+) {
+ let user_closure: &UserClosure<N> = unsafe { &*user_data.cast_const().cast() };
+ let args: [&Value; N] = array::from_fn(|i| {
+ let v: &Value = unsafe { &*args.add(i).cast_const().cast() };
+ v
+ });
+ let ctx: &mut NixContext = unsafe { &mut *context.cast() };
+
+ match user_closure(args) {
+ Ok(v) => {
+ unsafe { copy_value(context, ret, v.0) };
+ }
+ Err(e) => {
+ ctx.set_err(
+ NixErrorKind::Unknown,
+ &CString::new(e.to_string()).expect("error should not contain internal nuls"),
+ );
+ }
+ }
+}
+
+type UserClosure<const N: usize> = Box<dyn Fn([&Value; N]) -> Result<Value>>;
+
+struct NativeFn(*mut PrimOp);
+impl NativeFn {
+ pub fn new<const N: usize>(
+ name: &'static CStr,
+ doc: &'static CStr,
+ args: [&'static CStr; N],
+ f: impl Fn([&Value; N]) -> Result<Value> + 'static,
+ ) -> Self {
+ // Double-boxing to make it thin pointer, as vtable gets outside of first Box
+ let closure: Box<UserClosure<N>> = Box::new(Box::new(f));
+ let f: PrimOpFun = Some(nix_primop_closure_adapter::<N>);
+ let mut args = args.into_iter().map(|v| v.as_ptr()).collect_vec();
+ args.push(null());
+ let args = args.as_mut_ptr();
+ let primop = with_default_context(|c, _| unsafe {
+ alloc_primop(
+ c,
+ f,
+ N as i32,
+ name.as_ptr(),
+ args,
+ doc.as_ptr(),
+ Box::into_raw(closure).cast(),
+ )
+ })
+ .expect("primop allocation should not fail");
+
+ Self(primop)
+ }
+ pub fn register(self) {
+ with_default_context(|c, _| unsafe { register_primop(c, self.0) })
+ .expect("primop registration should not fail");
+ }
+}
+
struct StorePath(*mut c_store_path);
impl StorePath {}
@@ -949,6 +1023,20 @@
let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);
let s = CString::new(s.to_string()?).expect("path str is cstring");
+ let uppercase_suffix = Value::new_primop(NativeFn::new(
+ c"uppercase_suffix",
+ c"make string uppercase and add suffix",
+ [c"str", c"suffix"],
+ |[str, suffix]: [&Value; 2]| {
+ let str = str.to_string()?;
+ let suffix = suffix.to_string()?;
+ Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))
+ },
+ ));
+
+ let test_result: String = nix_go_json!(test_data.testPrimop(uppercase_suffix));
+ assert_eq!(test_result, "PREFIX_BODY_SUFFIX");
+
let nix_ctx = NixContext::new();
let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;
flake.lockdiffbeforeafterboth2 "nodes": {2 "nodes": {3 "crane": {3 "crane": {4 "locked": {4 "locked": {5 "lastModified": 1762538466,5 "lastModified": 1763511871,6 "narHash": "sha256-8zrIPl6J+wLm9MH5ksHcW7BUHo7jSNOu0/hA0ohOOaM=",7 "owner": "ipetkov",6 "owner": "ipetkov",8 "repo": "crane",7 "repo": "crane",9 "rev": "0cea393fffb39575c46b7a0318386467272182fe",8 "rev": "099f9014bc8d0cd6e445470ea1df0fd691d5a548",10 "type": "github"9 "type": "github"11 },10 },12 "original": {11 "original": {38 ]37 ]39 },38 },40 "locked": {39 "locked": {41 "lastModified": 1762810396,40 "lastModified": 1763759067,42 "narHash": "sha256-dxFVgQPG+R72dkhXTtqUm7KpxElw3u6E+YlQ2WaDgt8=",43 "owner": "hercules-ci",41 "owner": "hercules-ci",44 "repo": "flake-parts",42 "repo": "flake-parts",45 "rev": "0bdadb1b265fb4143a75bd1ec7d8c915898a9923",43 "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0",46 "type": "github"44 "type": "github"47 },45 },48 "original": {46 "original": {51 "type": "github"49 "type": "github"52 }50 }53 },51 },54 "flake-parts_2": {55 "inputs": {56 "nixpkgs-lib": [57 "nix",58 "nixpkgs"59 ]60 },61 "locked": {62 "lastModified": 1748821116,63 "narHash": "sha256-F82+gS044J1APL0n4hH50GYdPRv/5JWm34oCJYmVKdE=",64 "rev": "49f0870db23e8c1ca0b5259734a02cd9e1e371a1",65 "revCount": 377,66 "type": "tarball",67 "url": "https://api.flakehub.com/f/pinned/hercules-ci/flake-parts/0.1.377%2Brev-49f0870db23e8c1ca0b5259734a02cd9e1e371a1/01972f28-554a-73f8-91f4-d488cc502f08/source.tar.gz"68 },69 "original": {70 "type": "tarball",71 "url": "https://flakehub.com/f/hercules-ci/flake-parts/0.1"72 }73 },74 "fleet-tf": {52 "fleet-tf": {75 "inputs": {53 "inputs": {76 "flake-parts": [54 "flake-parts": [85 },63 },86 "locked": {64 "locked": {87 "lastModified": 1759080490,65 "lastModified": 1759080490,88 "narHash": "sha256-6eog70ItEoiusftwCp1vjY/7kA1+BDTUuRwg4KmszUs=",89 "owner": "CertainLach",66 "owner": "CertainLach",90 "repo": "fleet-tf",67 "repo": "fleet-tf",91 "rev": "878bd8c23933d628bf750378bbe527b841901c3d",68 "rev": "878bd8c23933d628bf750378bbe527b841901c3d",123 },100 },124 "nix": {101 "nix": {125 "inputs": {102 "inputs": {126 "flake-parts": "flake-parts_2",103 "flake-parts": [104 "flake-parts"105 ],127 "git-hooks-nix": "git-hooks-nix",106 "git-hooks-nix": "git-hooks-nix",128 "nixpkgs": "nixpkgs",107 "nixpkgs": [108 "nixpkgs"109 ],129 "nixpkgs-23-11": "nixpkgs-23-11",110 "nixpkgs-23-11": "nixpkgs-23-11",130 "nixpkgs-regression": "nixpkgs-regression"111 "nixpkgs-regression": "nixpkgs-regression"131 },112 },132 "locked": {113 "locked": {133 "lastModified": 1762216145,114 "lastModified": 1763844027,134 "narHash": "sha256-ff2SX5zKVsOAvN84AT+81TmWob5B5dxlkMcFexPZHAA=",135 "owner": "deltarocks",115 "owner": "deltarocks",136 "repo": "nix",116 "repo": "nix",137 "rev": "dece43d5bae876aa475033d5a5a389b478b1644e",117 "rev": "6b07ab20f1a65bd4aa54ec1418ff6f179dde64ac",138 "type": "github"118 "type": "github"139 },119 },140 "original": {120 "original": {146 },126 },147 "nixpkgs": {127 "nixpkgs": {148 "locked": {128 "locked": {149 "lastModified": 1755922037,129 "lastModified": 1763844746,150 "narHash": "sha256-wY1+2JPH0ZZC4BQefoZw/k+3+DowFyfOxv17CN/idKs=",130 "owner": "nixos",151 "rev": "b1b3291469652d5a2edb0becc4ef0246fff97a7c",131 "repo": "nixpkgs",152 "revCount": 808723,132 "rev": "d20f025296df84f7d97dee7c1a069bea8b17c8f2",153 "type": "tarball",133 "type": "github"154 "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2505.808723%2Brev-b1b3291469652d5a2edb0becc4ef0246fff97a7c/0198daf7-011a-7703-95d7-57146e794342/source.tar.gz"155 },134 },156 "original": {135 "original": {157 "type": "tarball",136 "owner": "nixos",158 "url": "https://flakehub.com/f/NixOS/nixpkgs/0.2505"137 "ref": "release-25.05",138 "repo": "nixpkgs",139 "type": "github"159 }140 }160 },141 },161 "nixpkgs-23-11": {142 "nixpkgs-23-11": {190 "type": "github"171 "type": "github"191 }172 }192 },173 },193 "nixpkgs_2": {194 "locked": {195 "lastModified": 1762881507,196 "narHash": "sha256-P59mF2fC4/ijf5ZoCQA+wcaN6b0OgKKA3BQZpfPqHtY=",197 "owner": "nixos",198 "repo": "nixpkgs",199 "rev": "2bbfb713196ae4cf5428530e8387cadcf95d3e2f",200 "type": "github"201 },202 "original": {203 "owner": "nixos",204 "ref": "release-25.05",205 "repo": "nixpkgs",206 "type": "github"207 }208 },209 "root": {174 "root": {210 "inputs": {175 "inputs": {211 "crane": "crane",176 "crane": "crane",212 "flake-parts": "flake-parts",177 "flake-parts": "flake-parts",213 "fleet-tf": "fleet-tf",178 "fleet-tf": "fleet-tf",214 "nix": "nix",179 "nix": "nix",215 "nixpkgs": "nixpkgs_2",180 "nixpkgs": "nixpkgs",216 "rust-overlay": "rust-overlay",181 "rust-overlay": "rust-overlay",217 "shelly": "shelly",182 "shelly": "shelly",218 "treefmt-nix": "treefmt-nix"183 "treefmt-nix": "treefmt-nix"225 ]190 ]226 },191 },227 "locked": {192 "locked": {228 "lastModified": 1762828736,193 "lastModified": 1763778964,229 "narHash": "sha256-RxtFHWZpKwVcWHhx88E2NhWuBbgYVqIoIDynGs5FoJs=",230 "owner": "oxalica",194 "owner": "oxalica",231 "repo": "rust-overlay",195 "repo": "rust-overlay",232 "rev": "8d5baa5628f6dbd7ce6beca3c299bae27755204c",196 "rev": "7f3aa46dfa230ec2a4ca9281186a24771650ccd1",233 "type": "github"197 "type": "github"234 },198 },235 "original": {199 "original": {241 "shelly": {205 "shelly": {242 "locked": {206 "locked": {243 "lastModified": 1756323923,207 "lastModified": 1756323923,244 "narHash": "sha256-sKUaQrgnYVBmG5cGmGoFYXc61g74ufWpPYGPoJia58k=",245 "owner": "CertainLach",208 "owner": "CertainLach",246 "repo": "shelly",209 "repo": "shelly",247 "rev": "b5dd29a500db04f54a9f1c2bf81cdd84df8b0cd7",210 "rev": "b5dd29a500db04f54a9f1c2bf81cdd84df8b0cd7",260 ]223 ]261 },224 },262 "locked": {225 "locked": {263 "lastModified": 1762410071,226 "lastModified": 1762938485,264 "narHash": "sha256-aF5fvoZeoXNPxT0bejFUBXeUjXfHLSL7g+mjR/p5TEg=",265 "owner": "numtide",227 "owner": "numtide",266 "repo": "treefmt-nix",228 "repo": "treefmt-nix",267 "rev": "97a30861b13c3731a84e09405414398fbf3e109f",229 "rev": "5b4ee75aeefd1e2d5a1cc43cf6ba65eba75e83e4",268 "type": "github"230 "type": "github"269 },231 },270 "original": {232 "original": {flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -25,7 +25,11 @@
};
# DeterminateSystem's nix fork is controversial, but I don't mind it,
# and it has lazy-trees support which is useful for fleet.
- nix.url = "github:deltarocks/nix/fleet";
+ nix = {
+ url = "github:deltarocks/nix/fleet";
+ inputs.nixpkgs.follows = "nixpkgs";
+ inputs.flake-parts.follows = "flake-parts";
+ };
};
outputs =
inputs:
@@ -56,6 +60,7 @@
v = "Hello";
};
testString = "hello";
+ testPrimop = op: "PREFIX_" + (op "body" "_SUFFIX");
};
# To be used with https://github.com/NixOS/nix/pull/8892