git.delta.rocks / jrsonnet / refs/commits / 3bff084b3bc8

difftreelog

feat display nix stacktraces

urvrmlrlYaroslav Bolyukin2025-10-26parent: #f5a8281.patch.diff
in: trunk

6 files changed

modifiedcmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -118,7 +118,7 @@
 					{
 						Ok(path) => path,
 						Err(e) => {
-							error!("failed to build host system closure: {:#}", e);
+							error!("failed to build host system closure: {:?}", e);
 							return;
 						}
 					};
modifiedcrates/nix-eval/build.rsdiffbeforeafterboth
--- a/crates/nix-eval/build.rs
+++ b/crates/nix-eval/build.rs
@@ -16,6 +16,7 @@
 	// Link nix C++ libraries for cxx
 	for lib in &[
 		"nix-util",
+		"nix-util-c",
 		"nix-store",
 		"nix-expr",
 		"nix-flake",
@@ -34,12 +35,12 @@
 
 	cxx_build::bridge("src/logging.rs")
 		.file("src/logging.cc")
-		.std("c++20")
+		.std("c++23")
 		.shared_flag(true)
 		.compile("nix-eval-logging");
 	cxx_build::bridge("src/lib.rs")
 		.file("src/lib.cc")
-		.std("c++20")
+		.std("c++23")
 		.shared_flag(true)
 		.compile("nix-eval");
 
modifiedcrates/nix-eval/src/lib.rsdiffbeforeafterboth
before · crates/nix-eval/src/lib.rs
1use std::borrow::Cow;2use std::cell::RefCell;3use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};4use std::ptr::null_mut;5use std::sync::LazyLock;6use std::{collections::HashMap, path::PathBuf};7use std::{fmt, slice};89use anyhow::{Context, anyhow, bail};10use serde::Serialize;11use serde::de::DeserializeOwned;1213pub use anyhow::Result;14use tracing::instrument;1516use self::logging::nix_logging_cxx;17use self::nix_cxx::set_fetcher_setting;18use self::nix_raw::{19	BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,20	GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,21	GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder,22	Store as c_store, StorePath as c_store_path, alloc_value, bindings_builder_free,23	bindings_builder_insert, c_context, c_context_create, c_context_free, clear_err, err_code,24	err_info_msg, err_msg, eval_state_build, eval_state_builder_load, eval_state_builder_new,25	eval_state_builder_set_eval_setting, expr_eval_from_string, fetchers_settings,26	fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags,27	flake_lock_flags_free, flake_lock_flags_new, flake_reference,28	flake_reference_and_fragment_from_string, flake_reference_parse_flags,29	flake_reference_parse_flags_free, flake_reference_parse_flags_new,30	flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,31	flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,32	get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,33	init_string, libexpr_init, libstore_init, libutil_init, list_builder_free, list_builder_insert,34	locked_flake, locked_flake_free, locked_flake_get_output_attrs, make_attrs,35	make_bindings_builder, make_list, make_list_builder, realised_string, realised_string_free,36	realised_string_get_buffer_size, realised_string_get_buffer_start,37	realised_string_get_store_path, realised_string_get_store_path_count, set_err_msg, setting_set,38	state_free, store_open, store_parse_path, store_path_free, store_path_name, string_realise,39	value, value_call, value_decref, value_incref,40};4142// Contains macros helpers43pub mod logging;44#[doc(hidden)]45pub mod macros;46pub mod util;4748#[allow(49	non_upper_case_globals,50	non_camel_case_types,51	non_snake_case,52	dead_code53)]54mod nix_raw {55	include!(concat!(env!("OUT_DIR"), "/bindings.rs"));56}57#[cxx::bridge]58pub mod nix_cxx {59	unsafe extern "C++" {60		type nix_fetchers_settings;61		include!("nix-eval/src/lib.hh");6263		unsafe fn set_fetcher_setting(64			settings: *mut nix_fetchers_settings,65			setting: *const c_char,66			value: *const c_char,67		);68	}69}7071#[derive(Debug, PartialEq, Eq)]72pub enum NixType {73	Thunk,74	Int,75	Float,76	Bool,77	String,78	Path,79	Null,80	Attrs,81	List,82	Function,83	External,84}85impl NixType {86	fn from_int(c: c_uint) -> Self {87		match c {88			0 => Self::Thunk,89			1 => Self::Int,90			2 => Self::Float,91			3 => Self::Bool,92			4 => Self::String,93			5 => Self::Path,94			6 => Self::Null,95			7 => Self::Attrs,96			8 => Self::List,97			9 => Self::Function,98			10 => Self::External,99			_ => unreachable!("unknown nix type: {c}"),100		}101	}102}103104enum FunctorKind {105	Function,106	Functor,107}108109#[derive(Debug)]110#[repr(i32)]111pub enum NixErrorKind {112	Unknown = 1,113	Overflow = 2,114	Key = 3,115	Generic = 4,116}117impl NixErrorKind {118	fn from_int(v: c_int) -> Option<Self> {119		Some(match v {120			0 => return None,121			-1 => Self::Unknown,122			-2 => Self::Overflow,123			-3 => Self::Key,124			-4 => Self::Generic,125			_ => {126				debug_assert!(false, "unexpected nix error kind: {v}");127				Self::Unknown128			}129		})130	}131}132133pub fn gc_now() {134	unsafe { gc_now_raw() };135}136137pub fn gc_register_my_thread() {138	assert_eq!(unsafe { GC_thread_is_registered() }, 0);139140	let mut sb = GC_stack_base {141		mem_base: null_mut(),142	};143	let r = unsafe { GC_get_stack_base(&mut sb) };144	if r as u32 != GC_SUCCESS {145		panic!("failed to get thread stack base");146	}147	unsafe { GC_register_my_thread(&sb) };148}149pub fn gc_unregister_my_thread() {150	assert_eq!(unsafe { GC_thread_is_registered() }, 1);151152	unsafe { GC_unregister_my_thread() };153}154155pub struct ThreadRegisterGuard {}156impl ThreadRegisterGuard {157	#[allow(clippy::new_without_default)]158	pub fn new() -> Self {159		gc_register_my_thread();160		Self {}161	}162}163impl Drop for ThreadRegisterGuard {164	fn drop(&mut self) {165		gc_unregister_my_thread();166	}167}168169pub struct NixContext(*mut c_context);170impl NixContext {171	pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {172		unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };173	}174	pub fn new() -> Self {175		let ctx = unsafe { c_context_create() };176		Self(ctx)177	}178	fn error_kind(&self) -> Option<NixErrorKind> {179		let code = unsafe { err_code(self.0) };180		NixErrorKind::from_int(code)181	}182	fn error<'t>(&self) -> Option<Cow<'t, str>> {183		if let NixErrorKind::Generic = self.error_kind()? {184			let mut err_out = String::new();185			unsafe {186				err_info_msg(187					null_mut(),188					self.0,189					Some(copy_nix_str),190					(&raw mut err_out).cast(),191				)192			};193			return Some(Cow::Owned(err_out));194		};195196		// TODO: Can throw error (resulting in panic) if unable to retrieve error. Should be able to resolve by passing context as a first argument,197		// but it looks ugly198		let str = unsafe { err_msg(null_mut(), self.0, null_mut()) };199		Some(unsafe { CStr::from_ptr(str) }.to_string_lossy())200	}201	fn clean_err(&mut self) {202		unsafe {203			clear_err(self.0);204		}205	}206207	fn bail_if_error(&self) -> Result<()> {208		if let Some(err) = self.error() {209			bail!("{err}");210		};211		Ok(())212	}213214	fn run_in_context<T>(&mut self, f: impl FnOnce(*mut c_context) -> T) -> Result<T> {215		self.clean_err();216		let o = f(self.0);217		self.bail_if_error()?;218		self.clean_err();219		Ok(o)220	}221}222223impl Default for NixContext {224	fn default() -> Self {225		Self::new()226	}227}228impl Drop for NixContext {229	fn drop(&mut self) {230		unsafe {231			c_context_free(self.0);232		}233	}234}235struct GlobalState {236	// Store should be valid as long as EvalState is valid237	#[allow(dead_code)]238	store: Store,239	state: EvalState,240}241impl GlobalState {242	fn new() -> Result<Self> {243		let mut ctx = NixContext::new();244		let store = ctx245			.run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })246			.map(Store)?;247248		let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;249		ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;250		ctx.run_in_context(|c| unsafe {251			eval_state_builder_set_eval_setting(252				c,253				builder,254				c"lazy-trees".as_ptr(),255				c"true".as_ptr(),256			)257		})?;258		ctx.run_in_context(|c| unsafe {259			eval_state_builder_set_eval_setting(260				c,261				builder,262				c"lazy-locks".as_ptr(),263				c"true".as_ptr(),264			)265		})?;266		let state = ctx267			.run_in_context(|c| unsafe { eval_state_build(c, builder) })268			.map(EvalState)?;269270		Ok(Self { store, state })271	}272}273274struct ThreadState {275	ctx: NixContext,276}277impl ThreadState {278	fn new() -> Result<Self> {279		let ctx = NixContext::new();280281		Ok(Self { ctx })282	}283}284285static GLOBAL_STATE: LazyLock<GlobalState> =286	LazyLock::new(|| GlobalState::new().expect("global state init shouldn't fail"));287288thread_local! {289	static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));290}291fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {292	let global = &GLOBAL_STATE.state;293	let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));294	let mut ctx = NixContext(ctx);295	let v = ctx.run_in_context(|c| f(c, state));296	// It is reused for thread297	std::mem::forget(ctx);298	v299}300301pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {302	with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())303}304305pub struct FetchSettings(*mut fetchers_settings);306impl FetchSettings {307	pub fn new() -> Self {308		Self::try_new().expect("allocation should not fail")309	}310	fn try_new() -> Result<Self> {311		with_default_context(|c, _| unsafe { fetchers_settings_new(c) }).map(Self)312	}313	pub fn set(&mut self, setting: &CStr, value: &CStr) {314		unsafe {315			set_fetcher_setting(self.0.cast(), setting.as_ptr(), value.as_ptr());316		};317	}318}319unsafe impl Send for FetchSettings {}320unsafe impl Sync for FetchSettings {}321322impl Default for FetchSettings {323	fn default() -> Self {324		Self::new()325	}326}327328impl Drop for FetchSettings {329	fn drop(&mut self) {330		unsafe { fetchers_settings_free(self.0) };331	}332}333pub struct FlakeSettings(*mut flake_settings);334impl FlakeSettings {335	pub fn new() -> Result<Self> {336		with_default_context(|c, _| unsafe { flake_settings_new(c) }).map(Self)337	}338}339unsafe impl Send for FlakeSettings {}340unsafe impl Sync for FlakeSettings {}341impl Drop for FlakeSettings {342	fn drop(&mut self) {343		unsafe {344			flake_settings_free(self.0);345		}346	}347}348349pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);350impl FlakeReferenceParseFlags {351	pub fn new(settings: &FlakeSettings) -> Result<Self> {352		with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })353			.map(Self)354	}355	pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {356		with_default_context(|c, _| {357			unsafe {358				flake_reference_parse_flags_set_base_directory(359					c,360					self.0,361					dir.as_ptr().cast(),362					dir.len(),363				)364			};365		})366	}367}368impl Drop for FlakeReferenceParseFlags {369	fn drop(&mut self) {370		unsafe {371			flake_reference_parse_flags_free(self.0);372		}373	}374}375pub struct FlakeLockFlags(*mut flake_lock_flags);376impl FlakeLockFlags {377	pub fn new(settings: &FlakeSettings) -> Result<Self> {378		let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) })379			.map(Self)?;380		// with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?;381382		Ok(o)383	}384}385impl Drop for FlakeLockFlags {386	fn drop(&mut self) {387		unsafe {388			flake_lock_flags_free(self.0);389		}390	}391}392393unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {394	let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };395	let s = std::str::from_utf8(s).expect("c string has invalid utf-8");396	unsafe { *user_data.cast::<String>() = s.to_owned() };397}398399struct Store(*mut c_store);400unsafe impl Send for Store {}401unsafe impl Sync for Store {}402403impl Store {404	fn parse_path(&self, path: &CStr) -> Result<StorePath> {405		with_default_context(|c, _| {406			StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })407		})408	}409}410411struct EvalState(*mut c_eval_state);412unsafe impl Send for EvalState {}413unsafe impl Sync for EvalState {}414415impl Drop for EvalState {416	fn drop(&mut self) {417		unsafe {418			state_free(self.0);419		}420	}421}422423pub struct FlakeReference(*mut flake_reference);424impl FlakeReference {425	#[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]426	pub fn new(427		s: &str,428		flake: &FlakeSettings,429		parse: &FlakeReferenceParseFlags,430		fetch: &FetchSettings,431	) -> Result<(Self, String)> {432		let mut out = null_mut();433		let mut fragment = String::new();434		// let fetch_settings = fetcher_settings;435		with_default_context(|c, _| unsafe {436			flake_reference_and_fragment_from_string(437				c,438				fetch.0,439				flake.0,440				parse.0,441				s.as_ptr().cast(),442				s.len(),443				&mut out,444				Some(copy_nix_str),445				(&raw mut fragment).cast(),446			)447		})?;448		assert!(!out.is_null());449450		Ok((Self(out), fragment))451	}452	#[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]453	pub fn lock(454		&mut self,455		fetch: &FetchSettings,456		flake: &FlakeSettings,457		lock: &FlakeLockFlags,458	) -> Result<LockedFlake> {459		with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })460			.map(LockedFlake)461	}462}463unsafe impl Send for FlakeReference {}464unsafe impl Sync for FlakeReference {}465466pub struct LockedFlake(*mut locked_flake);467impl LockedFlake {468	pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {469		with_default_context(|c, es| unsafe {470			locked_flake_get_output_attrs(c, settings.0, es, self.0)471		})472		.map(Value)473	}474}475unsafe impl Send for LockedFlake {}476unsafe impl Sync for LockedFlake {}477impl Drop for LockedFlake {478	fn drop(&mut self) {479		unsafe {480			locked_flake_free(self.0);481		};482	}483}484485type FieldName = [u8; 64];486fn init_field_name(v: &str) -> FieldName {487	let mut f = [0; 64];488	assert!(v.len() < 64, "max field name is 63 chars");489	assert!(490		v.bytes().all(|v| v != 0),491		"nul bytes are unsupported in field name"492	);493	f[0..v.len()].copy_from_slice(v.as_bytes());494	f495}496497pub struct RealisedString(*mut realised_string);498impl fmt::Debug for RealisedString {499	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {500		self.as_str().fmt(f)501	}502}503504impl RealisedString {505	pub fn as_str(&self) -> &str {506		let len = unsafe { realised_string_get_buffer_size(self.0) };507		let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();508		let data = unsafe { slice::from_raw_parts(data, len) };509		std::str::from_utf8(data).expect("non-utf8 strings not supported")510	}511	pub fn path_count(&self) -> usize {512		unsafe { realised_string_get_store_path_count(self.0) }513	}514	pub fn path(&self, i: usize) -> String {515		assert!(i < self.path_count());516		let path = unsafe { realised_string_get_store_path(self.0, i) };517		let mut err_out = String::new();518		unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };519		err_out520	}521}522523unsafe impl Send for RealisedString {}524impl Drop for RealisedString {525	fn drop(&mut self) {526		unsafe { realised_string_free(self.0) }527	}528}529530pub struct Value(*mut value);531532unsafe impl Send for Value {}533unsafe impl Sync for Value {}534535pub trait AsFieldName {536	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;537	fn to_field_name(&self) -> Result<String>;538}539impl AsFieldName for Value {540	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {541		let f = self.to_string()?;542		v(init_field_name(&f))543	}544	fn to_field_name(&self) -> Result<String> {545		self.to_string()546	}547}548impl<E> AsFieldName for E549where550	E: AsRef<str>,551{552	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {553		let f = self.as_ref();554		v(init_field_name(f))555	}556	fn to_field_name(&self) -> Result<String> {557		Ok(self.as_ref().to_owned())558	}559}560561struct AttrsBuilder(*mut c_bindings_builder);562impl AttrsBuilder {563	fn new(capacity: usize) -> Self {564		with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })565			.map(Self)566			.expect("alloc should not fail")567	}568	fn insert(&mut self, k: &impl AsFieldName, v: Value) {569		k.as_field_name(|name| {570			with_default_context(|c, _| unsafe {571				bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);572				// bindings_builder_insert doesn't do incref573			})574		})575		.expect("builder insert shouldn't fail");576	}577}578impl Drop for AttrsBuilder {579	fn drop(&mut self) {580		unsafe { bindings_builder_free(self.0) };581	}582}583584struct ListBuilder(*mut c_list_builder, c_uint);585impl ListBuilder {586	fn new(capacity: usize) -> Self {587		with_default_context(|c, es| unsafe { make_list_builder(c, es, capacity) })588			.map(|l| Self(l, 0))589			.expect("alloc should not fail")590	}591}592impl ListBuilder {593	fn push(&mut self, v: Value) {594		with_default_context(|c, _| unsafe {595			list_builder_insert(596				c,597				self.0,598				{599					let v = self.1;600					self.1 += 1;601					v602				},603				v.0,604			)605		})606		.expect("list insert shouldn't fail");607	}608}609impl Drop for ListBuilder {610	fn drop(&mut self) {611		unsafe { list_builder_free(self.0) };612	}613}614615impl Value {616	pub fn new_attrs(v: HashMap<&str, Value>) -> Self {617		let out = Self::new_uninit();618		let mut b = AttrsBuilder::new(v.len());619		for (k, v) in v {620			b.insert(&k, v);621		}622		with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })623			.expect("attrs initialization should not fail");624625		out626	}627	fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {628		let out = Self::new_uninit();629		let mut b = ListBuilder::new(v.len());630		for v in v {631			b.push(v.into());632		}633		with_default_context(|c, _| unsafe { make_list(c, b.0, out.0) })634			.expect("list initialization should not fail");635636		out637	}638	fn new_uninit() -> Self {639		let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })640			.expect("value allocation should not fail");641		Self(out)642	}643	pub fn new_str(v: &str) -> Self {644		let s = CString::new(v).expect("string should not contain NULs");645		let out = Self::new_uninit();646		// String is copied, `s` is free to be dropped647		with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })648			.expect("string initialization should not fail");649		out650	}651	pub fn new_int(i: i64) -> Self {652		let out = Self::new_uninit();653		with_default_context(|c, _| unsafe { init_int(c, out.0, i) })654			.expect("int initialization should not fail");655		out656	}657	pub fn new_bool(v: bool) -> Self {658		let out = Self::new_uninit();659		with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })660			.expect("bool initialization should not fail");661		out662	}663	// TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless664	// fn force(&mut self, st: &mut EvalState) -> Result<()> {665	// 	with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;666	// 	Ok(())667	// }668	pub fn type_of(&self) -> NixType {669		let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })670			.expect("get_type should not fail");671		NixType::from_int(ty)672	}673	fn builtin_to_string(&self) -> Result<Self> {674		let builtin = Self::eval("builtins.toString")?;675		builtin.call(self.clone())676	}677	pub fn to_string(&self) -> Result<String> {678		let mut str_out = String::new();679		with_default_context(|c, _| unsafe {680			get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())681		})?;682683		Ok(str_out)684	}685	pub fn to_realised_string(&self) -> Result<RealisedString> {686		with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })687			.map(RealisedString)688689		// let store_paths = unsafe { nix_raw::realised_string_get_store_path_count(str) };690		// for i in 0..store_paths {691		// 	let store_path = unsafe { nix_raw::realised_string_get_store_path(str, i) };692		// 	nix_raw::store_path_name(store_path, callback, user_data);693		// }694		// dbg!(store_paths);695		// todo!();696	}697698	pub fn has_field(&self, field: &str) -> Result<bool> {699		let f = init_field_name(field);700		with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })701	}702	// pub fn derivation_path(&self) {703	// 	nix_raw::real704	// }705	pub fn list_fields(&self) -> Result<Vec<String>> {706		if !matches!(self.type_of(), NixType::Attrs) {707			bail!("invalid type: expected attrs");708		}709710		let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;711		let mut out = Vec::with_capacity(len as usize);712713		for i in 0..len {714			let name =715				with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;716			let c = unsafe { CStr::from_ptr(name) };717			out.push(c.to_str().expect("nix field names are utf-8").to_owned());718		}719		Ok(out)720	}721	pub fn get_elem(&self, v: usize) -> Result<Self> {722		if !matches!(self.type_of(), NixType::List) {723			bail!("invalid type: expected list");724		}725		let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;726		if v >= len {727			bail!("oob list get: {v} >= {len}");728		}729730		with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)731	}732	pub fn attrs_update(self, other: Value /*, ignore_errors: bool*/) -> Result<Self> {733		let attrs_update_fn = Self::eval("a: b: a // b")?;734735		attrs_update_fn736			.call(self)?737			.call(other)738			.context("attrs update")739	}740	pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {741		if !matches!(self.type_of(), NixType::Attrs) {742			bail!("invalid type: expected attrs");743		}744745		name.as_field_name(|name| {746			with_default_context(|c, es| unsafe {747				get_attr_byname(c, self.0, es, name.as_ptr().cast())748			})749			.map(Self)750		})751		.with_context(|| format!("getting field {:?}", name.to_field_name()))752	}753	pub fn call(&self, v: Value) -> Result<Self> {754		let kind = self755			.functor_kind()756			.ok_or_else(|| anyhow!("can only call function or functor"))?;757758		let function = match kind {759			FunctorKind::Function => self.clone(),760			FunctorKind::Functor => {761				let f = self762					.get_field("__functor")763					.context("getting functor value")?;764				assert_eq!(765					f.type_of(),766					NixType::Function,767					"invalid functor encountered"768				);769				f770			}771		};772773		let out = Value::new_uninit();774		with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;775776		Ok(out)777	}778	pub fn eval(v: &str) -> Result<Self> {779		let s = CString::new(v).expect("expression shouldn't have internal NULs");780		let out = Self::new_uninit();781		with_default_context(|c, es| unsafe {782			expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)783		})?;784		Ok(out)785	}786	pub fn build(&self, output: &str) -> Result<PathBuf> {787		if !self.is_derivation() {788			bail!("expected derivation to build")789		}790		let output_name = self791			.get_field("outputName")792			.context("getting output name field")?793			.to_string()?;794		let v = if output_name != output {795			let out = self.get_field(output).context("getting target output")?;796			if !out.is_derivation() {797				bail!("unknown output: {output}");798			}799			out800		} else {801			self.clone()802		};803		// to_string here blocks until the path is built804		let s = v.builtin_to_string()?;805		let rs = s.to_realised_string()?;806		let drv_path = rs.as_str().to_owned();807		Ok(PathBuf::from(drv_path))808	}809	pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {810		let to_json = Self::eval("builtins.toJSON")?;811		let s = to_json.call(self.clone())?.to_string()?;812		Ok(serde_json::from_str(&s)?)813	}814	pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {815		Self::eval(&nixlike::serialize(v)?)816	}817818	// Convert to string/evaluate derivations/etc819	// fn to_string_weak(&self) -> Result<String> {820	// 	// TODO: For now, it works exactly like to_string, see the comment for fn force()821	// 	self.to_string()822	// }823824	fn is_derivation(&self) -> bool {825		if !matches!(self.type_of(), NixType::Attrs) {826			return false;827		}828		let Some(ty) = self.get_field("type").ok() else {829			return false;830		};831		matches!(ty.to_string().as_deref(), Ok("derivation"))832	}833	fn functor_kind(&self) -> Option<FunctorKind> {834		match self.type_of() {835			NixType::Attrs => self836				.has_field("__functor")837				.expect("has_field shouldn't fail for attrs")838				.then_some(FunctorKind::Functor),839			NixType::Function => Some(FunctorKind::Function),840			_ => None,841		}842	}843	pub fn is_function(&self) -> bool {844		self.functor_kind().is_some()845	}846	pub fn is_null(&self) -> bool {847		matches!(self.type_of(), NixType::Null)848	}849}850851impl From<String> for Value {852	fn from(value: String) -> Self {853		Value::new_str(&value)854	}855}856impl From<bool> for Value {857	fn from(value: bool) -> Self {858		Value::new_bool(value)859	}860}861impl From<&str> for Value {862	fn from(value: &str) -> Self {863		Value::new_str(value)864	}865}866impl<T> From<Vec<T>> for Value867where868	T: Into<Value>,869{870	fn from(value: Vec<T>) -> Self {871		Value::new_list(value)872	}873}874875impl Clone for Value {876	fn clone(&self) -> Self {877		with_default_context(|c, _| unsafe { value_incref(c, self.0) })878			.expect("value incref should not fail");879		Self(self.0)880	}881}882impl Drop for Value {883	fn drop(&mut self) {884		with_default_context(|c, _| unsafe { value_decref(c, self.0) })885			.expect("value drop should not fail");886	}887}888889pub fn init_libraries() {890	unsafe { GC_allow_register_threads() };891892	let mut ctx = NixContext::new();893	ctx.run_in_context(|c| unsafe { libutil_init(c) })894		.expect("util init should not fail");895	ctx.run_in_context(|c| unsafe { libstore_init(c) })896		.expect("store init should not fail");897	ctx.run_in_context(|c| unsafe { libexpr_init(c) })898		.expect("expr init should not fail");899900	nix_logging_cxx::apply_tracing_logger();901}902903struct StorePath(*mut c_store_path);904impl StorePath {}905906impl Drop for StorePath {907	fn drop(&mut self) {908		unsafe { store_path_free(self.0) }909	}910}911912#[test_log::test]913fn test_native() -> Result<()> {914	init_libraries();915916	let mut fetch_settings = FetchSettings::new();917	fetch_settings.set(c"warn-dirty", c"false");918919	let manifest = format!("git+file://{}/../../", env!("CARGO_MANIFEST_DIR"));920	let flake = FlakeSettings::new()?;921	let parse = FlakeReferenceParseFlags::new(&flake)?;922	let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;923	let lock = FlakeLockFlags::new(&flake)?;924	let locked = r.lock(&fetch_settings, &flake, &lock)?;925	let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;926927	let builtins = Value::eval("builtins")?;928	assert_eq!(builtins.type_of(), NixType::Attrs);929930	assert_eq!(attrs.type_of(), NixType::Attrs);931	let test_data = nix_go!(attrs.testData);932933	let test_string: String = nix_go_json!(test_data.testString);934	assert_eq!(test_string, "hello");935936	let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);937	let s = CString::new(s.to_string()?).expect("path str is cstring");938939	let nix_ctx = NixContext::new();940	let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;941942	// nix_raw::store_get_fs_closure(1);943944	Ok(())945}946947// pub struct GcAlloc;948// unsafe impl GlobalAlloc for GcAlloc {949// 	unsafe fn alloc(&self, l: Layout) -> *mut u8 {950// 		let ptr = unsafe { GC_malloc(l.size()) };951// 		ptr.cast()952// 	}953// 	unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {954// 		// unsafe { GC_free(ptr.cast()) };955// 	}956//957// 	unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {958// 		let ptr = unsafe { GC_realloc(ptr.cast(), new_size) };959// 		ptr.cast()960// 	}961// }962//963// #[global_allocator]964// static GC: GcAlloc = GcAlloc;
after · crates/nix-eval/src/lib.rs
1use std::borrow::Cow;2use std::cell::RefCell;3use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};4use std::ptr::null_mut;5use std::sync::LazyLock;6use std::{collections::HashMap, path::PathBuf};7use std::{fmt, slice};89use anyhow::{Context, anyhow, bail};10use serde::Serialize;11use serde::de::DeserializeOwned;1213pub use anyhow::Result;14use tracing::instrument;1516use self::logging::{ErrorInfoBuilder, nix_logging_cxx};17use self::nix_cxx::set_fetcher_setting;18use self::nix_raw::{19	BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,20	GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,21	GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder,22	Store as c_store, StorePath as c_store_path, alloc_value, bindings_builder_free,23	bindings_builder_insert, c_context, c_context_create, c_context_free, clear_err, err_code,24	err_info_msg, err_msg, eval_state_build, eval_state_builder_load, eval_state_builder_new,25	eval_state_builder_set_eval_setting, expr_eval_from_string, fetchers_settings,26	fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags,27	flake_lock_flags_free, flake_lock_flags_new, flake_reference,28	flake_reference_and_fragment_from_string, flake_reference_parse_flags,29	flake_reference_parse_flags_free, flake_reference_parse_flags_new,30	flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,31	flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,32	get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,33	init_string, libexpr_init, libstore_init, libutil_init, list_builder_free, list_builder_insert,34	locked_flake, locked_flake_free, locked_flake_get_output_attrs, make_attrs,35	make_bindings_builder, make_list, make_list_builder, realised_string, realised_string_free,36	realised_string_get_buffer_size, realised_string_get_buffer_start,37	realised_string_get_store_path, realised_string_get_store_path_count, set_err_msg, setting_set,38	state_free, store_open, store_parse_path, store_path_free, store_path_name, string_realise,39	value, value_call, value_decref, value_incref,40};4142// Contains macros helpers43pub mod logging;44#[doc(hidden)]45pub mod macros;46pub mod util;4748#[allow(49	non_upper_case_globals,50	non_camel_case_types,51	non_snake_case,52	dead_code53)]54mod nix_raw {55	include!(concat!(env!("OUT_DIR"), "/bindings.rs"));56}57#[cxx::bridge]58pub mod nix_cxx {59	unsafe extern "C++" {60		type nix_fetchers_settings;61		include!("nix-eval/src/lib.hh");6263		unsafe fn set_fetcher_setting(64			settings: *mut nix_fetchers_settings,65			setting: *const c_char,66			value: *const c_char,67		);68	}69}7071#[derive(Debug, PartialEq, Eq)]72pub enum NixType {73	Thunk,74	Int,75	Float,76	Bool,77	String,78	Path,79	Null,80	Attrs,81	List,82	Function,83	External,84}85impl NixType {86	fn from_int(c: c_uint) -> Self {87		match c {88			0 => Self::Thunk,89			1 => Self::Int,90			2 => Self::Float,91			3 => Self::Bool,92			4 => Self::String,93			5 => Self::Path,94			6 => Self::Null,95			7 => Self::Attrs,96			8 => Self::List,97			9 => Self::Function,98			10 => Self::External,99			_ => unreachable!("unknown nix type: {c}"),100		}101	}102}103104enum FunctorKind {105	Function,106	Functor,107}108109#[derive(Debug)]110#[repr(i32)]111pub enum NixErrorKind {112	Unknown = 1,113	Overflow = 2,114	Key = 3,115	Generic = 4,116}117impl NixErrorKind {118	fn from_int(v: c_int) -> Option<Self> {119		Some(match v {120			0 => return None,121			-1 => Self::Unknown,122			-2 => Self::Overflow,123			-3 => Self::Key,124			-4 => Self::Generic,125			_ => {126				debug_assert!(false, "unexpected nix error kind: {v}");127				Self::Unknown128			}129		})130	}131}132133pub fn gc_now() {134	unsafe { gc_now_raw() };135}136137pub fn gc_register_my_thread() {138	assert_eq!(unsafe { GC_thread_is_registered() }, 0);139140	let mut sb = GC_stack_base {141		mem_base: null_mut(),142	};143	let r = unsafe { GC_get_stack_base(&mut sb) };144	if r as u32 != GC_SUCCESS {145		panic!("failed to get thread stack base");146	}147	unsafe { GC_register_my_thread(&sb) };148}149pub fn gc_unregister_my_thread() {150	assert_eq!(unsafe { GC_thread_is_registered() }, 1);151152	unsafe { GC_unregister_my_thread() };153}154155pub struct ThreadRegisterGuard {}156impl ThreadRegisterGuard {157	#[allow(clippy::new_without_default)]158	pub fn new() -> Self {159		gc_register_my_thread();160		Self {}161	}162}163impl Drop for ThreadRegisterGuard {164	fn drop(&mut self) {165		gc_unregister_my_thread();166	}167}168169pub struct NixContext(*mut c_context);170impl NixContext {171	pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {172		unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };173	}174	pub fn new() -> Self {175		let ctx = unsafe { c_context_create() };176		Self(ctx)177	}178	fn error_kind(&self) -> Option<NixErrorKind> {179		let code = unsafe { err_code(self.0) };180		NixErrorKind::from_int(code)181	}182	fn error<'t>(&self) -> Option<(Cow<'t, str>, Option<Box<ErrorInfoBuilder>>)> {183		if let NixErrorKind::Generic = self.error_kind()? {184			let ei = unsafe { logging::nix_logging_cxx::extract_error_info(self.0) };185			let mut err_out = String::new();186			unsafe {187				err_info_msg(188					null_mut(),189					self.0,190					Some(copy_nix_str),191					(&raw mut err_out).cast(),192				)193			};194			return Some((Cow::Owned(err_out), Some(ei)));195		};196197		// TODO: Can throw error (resulting in panic) if unable to retrieve error. Should be able to resolve by passing context as a first argument,198		// but it looks ugly199		let str = unsafe { err_msg(null_mut(), self.0, null_mut()) };200		Some((unsafe { CStr::from_ptr(str) }.to_string_lossy(), None))201	}202	fn clean_err(&mut self) {203		unsafe {204			clear_err(self.0);205		}206	}207208	fn bail_if_error(&self) -> Result<()> {209		if let Some((err, stack)) = self.error() {210			let mut e = Err(anyhow!("{err}"));211			if let Some(stack) = stack {212				for ele in stack.stack_frames {213					e = e.with_context(|| {214						if ele.pos.is_empty() {215							ele.msg216						} else {217							format!("{} at {}", ele.msg, ele.pos)218						}219					})220				}221			}222			return e.context("<nix frames>");223		};224		Ok(())225	}226227	fn run_in_context<T>(&mut self, f: impl FnOnce(*mut c_context) -> T) -> Result<T> {228		self.clean_err();229		let o = f(self.0);230		self.bail_if_error()?;231		self.clean_err();232		Ok(o)233	}234}235236impl Default for NixContext {237	fn default() -> Self {238		Self::new()239	}240}241impl Drop for NixContext {242	fn drop(&mut self) {243		unsafe {244			c_context_free(self.0);245		}246	}247}248struct GlobalState {249	// Store should be valid as long as EvalState is valid250	#[allow(dead_code)]251	store: Store,252	state: EvalState,253}254impl GlobalState {255	fn new() -> Result<Self> {256		let mut ctx = NixContext::new();257		let store = ctx258			.run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })259			.map(Store)?;260261		let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;262		ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;263		ctx.run_in_context(|c| unsafe {264			eval_state_builder_set_eval_setting(265				c,266				builder,267				c"lazy-trees".as_ptr(),268				c"true".as_ptr(),269			)270		})?;271		ctx.run_in_context(|c| unsafe {272			eval_state_builder_set_eval_setting(273				c,274				builder,275				c"lazy-locks".as_ptr(),276				c"true".as_ptr(),277			)278		})?;279		let state = ctx280			.run_in_context(|c| unsafe { eval_state_build(c, builder) })281			.map(EvalState)?;282283		Ok(Self { store, state })284	}285}286287struct ThreadState {288	ctx: NixContext,289}290impl ThreadState {291	fn new() -> Result<Self> {292		let ctx = NixContext::new();293294		Ok(Self { ctx })295	}296}297298static GLOBAL_STATE: LazyLock<GlobalState> =299	LazyLock::new(|| GlobalState::new().expect("global state init shouldn't fail"));300301thread_local! {302	static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));303}304fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {305	let global = &GLOBAL_STATE.state;306	let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));307	let mut ctx = NixContext(ctx);308	let v = ctx.run_in_context(|c| f(c, state));309	// It is reused for thread310	std::mem::forget(ctx);311	v312}313314pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {315	with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())316}317318pub struct FetchSettings(*mut fetchers_settings);319impl FetchSettings {320	pub fn new() -> Self {321		Self::try_new().expect("allocation should not fail")322	}323	fn try_new() -> Result<Self> {324		with_default_context(|c, _| unsafe { fetchers_settings_new(c) }).map(Self)325	}326	pub fn set(&mut self, setting: &CStr, value: &CStr) {327		unsafe {328			set_fetcher_setting(self.0.cast(), setting.as_ptr(), value.as_ptr());329		};330	}331}332unsafe impl Send for FetchSettings {}333unsafe impl Sync for FetchSettings {}334335impl Default for FetchSettings {336	fn default() -> Self {337		Self::new()338	}339}340341impl Drop for FetchSettings {342	fn drop(&mut self) {343		unsafe { fetchers_settings_free(self.0) };344	}345}346pub struct FlakeSettings(*mut flake_settings);347impl FlakeSettings {348	pub fn new() -> Result<Self> {349		with_default_context(|c, _| unsafe { flake_settings_new(c) }).map(Self)350	}351}352unsafe impl Send for FlakeSettings {}353unsafe impl Sync for FlakeSettings {}354impl Drop for FlakeSettings {355	fn drop(&mut self) {356		unsafe {357			flake_settings_free(self.0);358		}359	}360}361362pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);363impl FlakeReferenceParseFlags {364	pub fn new(settings: &FlakeSettings) -> Result<Self> {365		with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })366			.map(Self)367	}368	pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {369		with_default_context(|c, _| {370			unsafe {371				flake_reference_parse_flags_set_base_directory(372					c,373					self.0,374					dir.as_ptr().cast(),375					dir.len(),376				)377			};378		})379	}380}381impl Drop for FlakeReferenceParseFlags {382	fn drop(&mut self) {383		unsafe {384			flake_reference_parse_flags_free(self.0);385		}386	}387}388pub struct FlakeLockFlags(*mut flake_lock_flags);389impl FlakeLockFlags {390	pub fn new(settings: &FlakeSettings) -> Result<Self> {391		let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) })392			.map(Self)?;393		// with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?;394395		Ok(o)396	}397}398impl Drop for FlakeLockFlags {399	fn drop(&mut self) {400		unsafe {401			flake_lock_flags_free(self.0);402		}403	}404}405406unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {407	let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };408	let s = std::str::from_utf8(s).expect("c string has invalid utf-8");409	unsafe { *user_data.cast::<String>() = s.to_owned() };410}411412struct Store(*mut c_store);413unsafe impl Send for Store {}414unsafe impl Sync for Store {}415416impl Store {417	fn parse_path(&self, path: &CStr) -> Result<StorePath> {418		with_default_context(|c, _| {419			StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })420		})421	}422}423424struct EvalState(*mut c_eval_state);425unsafe impl Send for EvalState {}426unsafe impl Sync for EvalState {}427428impl Drop for EvalState {429	fn drop(&mut self) {430		unsafe {431			state_free(self.0);432		}433	}434}435436pub struct FlakeReference(*mut flake_reference);437impl FlakeReference {438	#[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]439	pub fn new(440		s: &str,441		flake: &FlakeSettings,442		parse: &FlakeReferenceParseFlags,443		fetch: &FetchSettings,444	) -> Result<(Self, String)> {445		let mut out = null_mut();446		let mut fragment = String::new();447		// let fetch_settings = fetcher_settings;448		with_default_context(|c, _| unsafe {449			flake_reference_and_fragment_from_string(450				c,451				fetch.0,452				flake.0,453				parse.0,454				s.as_ptr().cast(),455				s.len(),456				&mut out,457				Some(copy_nix_str),458				(&raw mut fragment).cast(),459			)460		})?;461		assert!(!out.is_null());462463		Ok((Self(out), fragment))464	}465	#[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]466	pub fn lock(467		&mut self,468		fetch: &FetchSettings,469		flake: &FlakeSettings,470		lock: &FlakeLockFlags,471	) -> Result<LockedFlake> {472		with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })473			.map(LockedFlake)474	}475}476unsafe impl Send for FlakeReference {}477unsafe impl Sync for FlakeReference {}478479pub struct LockedFlake(*mut locked_flake);480impl LockedFlake {481	pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {482		with_default_context(|c, es| unsafe {483			locked_flake_get_output_attrs(c, settings.0, es, self.0)484		})485		.map(Value)486	}487}488unsafe impl Send for LockedFlake {}489unsafe impl Sync for LockedFlake {}490impl Drop for LockedFlake {491	fn drop(&mut self) {492		unsafe {493			locked_flake_free(self.0);494		};495	}496}497498type FieldName = [u8; 64];499fn init_field_name(v: &str) -> FieldName {500	let mut f = [0; 64];501	assert!(v.len() < 64, "max field name is 63 chars");502	assert!(503		v.bytes().all(|v| v != 0),504		"nul bytes are unsupported in field name"505	);506	f[0..v.len()].copy_from_slice(v.as_bytes());507	f508}509510pub struct RealisedString(*mut realised_string);511impl fmt::Debug for RealisedString {512	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {513		self.as_str().fmt(f)514	}515}516517impl RealisedString {518	pub fn as_str(&self) -> &str {519		let len = unsafe { realised_string_get_buffer_size(self.0) };520		let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();521		let data = unsafe { slice::from_raw_parts(data, len) };522		std::str::from_utf8(data).expect("non-utf8 strings not supported")523	}524	pub fn path_count(&self) -> usize {525		unsafe { realised_string_get_store_path_count(self.0) }526	}527	pub fn path(&self, i: usize) -> String {528		assert!(i < self.path_count());529		let path = unsafe { realised_string_get_store_path(self.0, i) };530		let mut err_out = String::new();531		unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };532		err_out533	}534}535536unsafe impl Send for RealisedString {}537impl Drop for RealisedString {538	fn drop(&mut self) {539		unsafe { realised_string_free(self.0) }540	}541}542543pub struct Value(*mut value);544545unsafe impl Send for Value {}546unsafe impl Sync for Value {}547548pub trait AsFieldName {549	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;550	fn to_field_name(&self) -> Result<String>;551}552impl AsFieldName for Value {553	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {554		let f = self.to_string()?;555		v(init_field_name(&f))556	}557	fn to_field_name(&self) -> Result<String> {558		self.to_string()559	}560}561impl<E> AsFieldName for E562where563	E: AsRef<str>,564{565	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {566		let f = self.as_ref();567		v(init_field_name(f))568	}569	fn to_field_name(&self) -> Result<String> {570		Ok(self.as_ref().to_owned())571	}572}573574struct AttrsBuilder(*mut c_bindings_builder);575impl AttrsBuilder {576	fn new(capacity: usize) -> Self {577		with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })578			.map(Self)579			.expect("alloc should not fail")580	}581	fn insert(&mut self, k: &impl AsFieldName, v: Value) {582		k.as_field_name(|name| {583			with_default_context(|c, _| unsafe {584				bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);585				// bindings_builder_insert doesn't do incref586			})587		})588		.expect("builder insert shouldn't fail");589	}590}591impl Drop for AttrsBuilder {592	fn drop(&mut self) {593		unsafe { bindings_builder_free(self.0) };594	}595}596597struct ListBuilder(*mut c_list_builder, c_uint);598impl ListBuilder {599	fn new(capacity: usize) -> Self {600		with_default_context(|c, es| unsafe { make_list_builder(c, es, capacity) })601			.map(|l| Self(l, 0))602			.expect("alloc should not fail")603	}604}605impl ListBuilder {606	fn push(&mut self, v: Value) {607		with_default_context(|c, _| unsafe {608			list_builder_insert(609				c,610				self.0,611				{612					let v = self.1;613					self.1 += 1;614					v615				},616				v.0,617			)618		})619		.expect("list insert shouldn't fail");620	}621}622impl Drop for ListBuilder {623	fn drop(&mut self) {624		unsafe { list_builder_free(self.0) };625	}626}627628impl Value {629	pub fn new_attrs(v: HashMap<&str, Value>) -> Self {630		let out = Self::new_uninit();631		let mut b = AttrsBuilder::new(v.len());632		for (k, v) in v {633			b.insert(&k, v);634		}635		with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })636			.expect("attrs initialization should not fail");637638		out639	}640	fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {641		let out = Self::new_uninit();642		let mut b = ListBuilder::new(v.len());643		for v in v {644			b.push(v.into());645		}646		with_default_context(|c, _| unsafe { make_list(c, b.0, out.0) })647			.expect("list initialization should not fail");648649		out650	}651	fn new_uninit() -> Self {652		let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })653			.expect("value allocation should not fail");654		Self(out)655	}656	pub fn new_str(v: &str) -> Self {657		let s = CString::new(v).expect("string should not contain NULs");658		let out = Self::new_uninit();659		// String is copied, `s` is free to be dropped660		with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })661			.expect("string initialization should not fail");662		out663	}664	pub fn new_int(i: i64) -> Self {665		let out = Self::new_uninit();666		with_default_context(|c, _| unsafe { init_int(c, out.0, i) })667			.expect("int initialization should not fail");668		out669	}670	pub fn new_bool(v: bool) -> Self {671		let out = Self::new_uninit();672		with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })673			.expect("bool initialization should not fail");674		out675	}676	// TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless677	// fn force(&mut self, st: &mut EvalState) -> Result<()> {678	// 	with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;679	// 	Ok(())680	// }681	pub fn type_of(&self) -> NixType {682		let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })683			.expect("get_type should not fail");684		NixType::from_int(ty)685	}686	fn builtin_to_string(&self) -> Result<Self> {687		let builtin = Self::eval("builtins.toString")?;688		builtin.call(self.clone())689	}690	pub fn to_string(&self) -> Result<String> {691		let mut str_out = String::new();692		with_default_context(|c, _| unsafe {693			get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())694		})?;695696		Ok(str_out)697	}698	pub fn to_realised_string(&self) -> Result<RealisedString> {699		with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })700			.map(RealisedString)701702		// let store_paths = unsafe { nix_raw::realised_string_get_store_path_count(str) };703		// for i in 0..store_paths {704		// 	let store_path = unsafe { nix_raw::realised_string_get_store_path(str, i) };705		// 	nix_raw::store_path_name(store_path, callback, user_data);706		// }707		// dbg!(store_paths);708		// todo!();709	}710711	pub fn has_field(&self, field: &str) -> Result<bool> {712		let f = init_field_name(field);713		with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })714	}715	// pub fn derivation_path(&self) {716	// 	nix_raw::real717	// }718	pub fn list_fields(&self) -> Result<Vec<String>> {719		if !matches!(self.type_of(), NixType::Attrs) {720			bail!("invalid type: expected attrs");721		}722723		let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;724		let mut out = Vec::with_capacity(len as usize);725726		for i in 0..len {727			let name =728				with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;729			let c = unsafe { CStr::from_ptr(name) };730			out.push(c.to_str().expect("nix field names are utf-8").to_owned());731		}732		Ok(out)733	}734	pub fn get_elem(&self, v: usize) -> Result<Self> {735		if !matches!(self.type_of(), NixType::List) {736			bail!("invalid type: expected list");737		}738		let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;739		if v >= len {740			bail!("oob list get: {v} >= {len}");741		}742743		with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)744	}745	pub fn attrs_update(self, other: Value /*, ignore_errors: bool*/) -> Result<Self> {746		let attrs_update_fn = Self::eval("a: b: a // b")?;747748		attrs_update_fn749			.call(self)?750			.call(other)751			.context("attrs update")752	}753	pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {754		if !matches!(self.type_of(), NixType::Attrs) {755			bail!("invalid type: expected attrs");756		}757758		name.as_field_name(|name| {759			with_default_context(|c, es| unsafe {760				get_attr_byname(c, self.0, es, name.as_ptr().cast())761			})762			.map(Self)763		})764		.with_context(|| format!("getting field {:?}", name.to_field_name()))765	}766	pub fn call(&self, v: Value) -> Result<Self> {767		let kind = self768			.functor_kind()769			.ok_or_else(|| anyhow!("can only call function or functor"))?;770771		let function = match kind {772			FunctorKind::Function => self.clone(),773			FunctorKind::Functor => {774				let f = self775					.get_field("__functor")776					.context("getting functor value")?;777				assert_eq!(778					f.type_of(),779					NixType::Function,780					"invalid functor encountered"781				);782				f783			}784		};785786		let out = Value::new_uninit();787		with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;788789		Ok(out)790	}791	pub fn eval(v: &str) -> Result<Self> {792		let s = CString::new(v).expect("expression shouldn't have internal NULs");793		let out = Self::new_uninit();794		with_default_context(|c, es| unsafe {795			expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)796		})?;797		Ok(out)798	}799	pub fn build(&self, output: &str) -> Result<PathBuf> {800		if !self.is_derivation() {801			bail!("expected derivation to build")802		}803		let output_name = self804			.get_field("outputName")805			.context("getting output name field")?806			.to_string()?;807		let v = if output_name != output {808			let out = self.get_field(output).context("getting target output")?;809			if !out.is_derivation() {810				bail!("unknown output: {output}");811			}812			out813		} else {814			self.clone()815		};816		// to_string here blocks until the path is built817		let s = v.builtin_to_string()?;818		let rs = s.to_realised_string()?;819		let drv_path = rs.as_str().to_owned();820		Ok(PathBuf::from(drv_path))821	}822	pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {823		let to_json = Self::eval("builtins.toJSON")?;824		let s = to_json.call(self.clone())?.to_string()?;825		Ok(serde_json::from_str(&s)?)826	}827	pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {828		Self::eval(&nixlike::serialize(v)?)829	}830831	// Convert to string/evaluate derivations/etc832	// fn to_string_weak(&self) -> Result<String> {833	// 	// TODO: For now, it works exactly like to_string, see the comment for fn force()834	// 	self.to_string()835	// }836837	fn is_derivation(&self) -> bool {838		if !matches!(self.type_of(), NixType::Attrs) {839			return false;840		}841		let Some(ty) = self.get_field("type").ok() else {842			return false;843		};844		matches!(ty.to_string().as_deref(), Ok("derivation"))845	}846	fn functor_kind(&self) -> Option<FunctorKind> {847		match self.type_of() {848			NixType::Attrs => self849				.has_field("__functor")850				.expect("has_field shouldn't fail for attrs")851				.then_some(FunctorKind::Functor),852			NixType::Function => Some(FunctorKind::Function),853			_ => None,854		}855	}856	pub fn is_function(&self) -> bool {857		self.functor_kind().is_some()858	}859	pub fn is_null(&self) -> bool {860		matches!(self.type_of(), NixType::Null)861	}862}863864impl From<String> for Value {865	fn from(value: String) -> Self {866		Value::new_str(&value)867	}868}869impl From<bool> for Value {870	fn from(value: bool) -> Self {871		Value::new_bool(value)872	}873}874impl From<&str> for Value {875	fn from(value: &str) -> Self {876		Value::new_str(value)877	}878}879impl<T> From<Vec<T>> for Value880where881	T: Into<Value>,882{883	fn from(value: Vec<T>) -> Self {884		Value::new_list(value)885	}886}887888impl Clone for Value {889	fn clone(&self) -> Self {890		with_default_context(|c, _| unsafe { value_incref(c, self.0) })891			.expect("value incref should not fail");892		Self(self.0)893	}894}895impl Drop for Value {896	fn drop(&mut self) {897		with_default_context(|c, _| unsafe { value_decref(c, self.0) })898			.expect("value drop should not fail");899	}900}901902pub fn init_libraries() {903	unsafe { GC_allow_register_threads() };904905	let mut ctx = NixContext::new();906	ctx.run_in_context(|c| unsafe { libutil_init(c) })907		.expect("util init should not fail");908	ctx.run_in_context(|c| unsafe { libstore_init(c) })909		.expect("store init should not fail");910	ctx.run_in_context(|c| unsafe { libexpr_init(c) })911		.expect("expr init should not fail");912913	nix_logging_cxx::apply_tracing_logger();914}915916struct StorePath(*mut c_store_path);917impl StorePath {}918919impl Drop for StorePath {920	fn drop(&mut self) {921		unsafe { store_path_free(self.0) }922	}923}924925#[test_log::test]926fn test_native() -> Result<()> {927	init_libraries();928929	let mut fetch_settings = FetchSettings::new();930	fetch_settings.set(c"warn-dirty", c"false");931932	let manifest = format!("git+file://{}/../../", env!("CARGO_MANIFEST_DIR"));933	let flake = FlakeSettings::new()?;934	let parse = FlakeReferenceParseFlags::new(&flake)?;935	let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;936	let lock = FlakeLockFlags::new(&flake)?;937	let locked = r.lock(&fetch_settings, &flake, &lock)?;938	let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;939940	let builtins = Value::eval("builtins")?;941	assert_eq!(builtins.type_of(), NixType::Attrs);942943	assert_eq!(attrs.type_of(), NixType::Attrs);944	let test_data = nix_go!(attrs.testData);945946	let test_string: String = nix_go_json!(test_data.testString);947	assert_eq!(test_string, "hello");948949	let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);950	let s = CString::new(s.to_string()?).expect("path str is cstring");951952	let nix_ctx = NixContext::new();953	let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;954955	// nix_raw::store_get_fs_closure(1);956957	Ok(())958}959960// pub struct GcAlloc;961// unsafe impl GlobalAlloc for GcAlloc {962// 	unsafe fn alloc(&self, l: Layout) -> *mut u8 {963// 		let ptr = unsafe { GC_malloc(l.size()) };964// 		ptr.cast()965// 	}966// 	unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {967// 		// unsafe { GC_free(ptr.cast()) };968// 	}969//970// 	unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {971// 		let ptr = unsafe { GC_realloc(ptr.cast(), new_size) };972// 		ptr.cast()973// 	}974// }975//976// #[global_allocator]977// static GC: GcAlloc = GcAlloc;
modifiedcrates/nix-eval/src/logging.ccdiffbeforeafterboth
--- a/crates/nix-eval/src/logging.cc
+++ b/crates/nix-eval/src/logging.cc
@@ -1,9 +1,36 @@
-#include "nix-eval/src/logging.rs"
 #include "logging.hh"
 #include <nix/util/logging.hh>
+#include <nix/util/position.hh>
 
 using namespace nix;
 
+rust::Box<ErrorInfoBuilder> copy_error_info(const ErrorInfo &ei) {
+  auto s = ei.msg.str();
+  rust::Slice<const unsigned char> str(
+      reinterpret_cast<const unsigned char *>(s.data()), s.size());
+  auto b = new_error_info(ei.level, str);
+  if (!ei.traces.empty()) {
+    for (auto iter = ei.traces.rbegin(); iter != ei.traces.rend(); ++iter) {
+      auto msg = iter->hint.str();
+
+      rust::Slice<const unsigned char> msgv(
+          reinterpret_cast<const unsigned char *>(msg.data()), msg.size());
+
+      std::ostringstream oss;
+      if (iter->pos) {
+        iter->pos->print(oss, true);
+      }
+      std::string pos = oss.str();
+
+      rust::Slice<const unsigned char> posv(
+          reinterpret_cast<const unsigned char *>(pos.data()), pos.size());
+
+      b->push_stack_frame(msgv, posv);
+    }
+  }
+  return b;
+}
+
 struct TracingLogger : Logger {
   TracingLogger() {}
 
@@ -14,10 +41,8 @@
     emit_log(lvl, str);
   }
   void logEI(const ErrorInfo &ei) override {
-    auto s = ei.msg.str();
-    rust::Slice<const unsigned char> str(
-        reinterpret_cast<const unsigned char *>(s.data()), s.size());
-    emit_log(ei.level, str);
+    auto b = copy_error_info(ei);
+    b->emit_error_info();
   }
 
   void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
@@ -74,4 +99,8 @@
   logger = std::make_unique<TracingLogger>();
   // verbosity = lvlVomit;
 }
+rust::Box<ErrorInfoBuilder>
+extract_error_info(const nix_c_context *read_context) {
+  return copy_error_info(read_context->info.value());
+}
 }
modifiedcrates/nix-eval/src/logging.hhdiffbeforeafterboth
--- a/crates/nix-eval/src/logging.hh
+++ b/crates/nix-eval/src/logging.hh
@@ -1,5 +1,12 @@
 #pragma once
+#include "nix-eval/src/logging.rs"
+#include "rust/cxx.h"
+#include <nix_api_util.h>
+#include <nix_api_util_internal.h>
+
+struct ErrorInfoBuilder;
 
 extern "C" {
 void apply_tracing_logger();
+rust::Box<ErrorInfoBuilder> extract_error_info(const nix_c_context *ctx);
 }
modifiedcrates/nix-eval/src/logging.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/logging.rs
+++ b/crates/nix-eval/src/logging.rs
@@ -2,6 +2,7 @@
 use std::fmt::Arguments;
 use std::sync::{LazyLock, Mutex};
 
+use cxx::ExternType;
 use tracing::{
 	Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,
 	warn_span,
@@ -535,6 +536,45 @@
 	out.output
 }
 
+#[derive(Debug)]
+pub struct StackFrame {
+	pub msg: String,
+	pub pos: String,
+}
+
+#[derive(Debug)]
+pub struct ErrorInfoBuilder {
+	level: Level,
+	msg: String,
+	pub stack_frames: Vec<StackFrame>,
+}
+fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder> {
+	let verbosity = Verbosity::from_int(lvl);
+	let level: Level = verbosity.into();
+	let v = String::from_utf8_lossy(v);
+	Box::new(ErrorInfoBuilder {
+		level,
+		msg: v.to_string(),
+		stack_frames: Vec::new(),
+	})
+}
+impl ErrorInfoBuilder {
+	fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]) {
+		let v = String::from_utf8_lossy(v);
+		let pos = String::from_utf8_lossy(pos);
+		self.stack_frames.push(StackFrame {
+			msg: v.to_string(),
+			pos: pos.to_string(),
+		});
+	}
+	fn emit_error_info(&mut self) {
+		error!("{}", self.msg);
+		for frame in &self.stack_frames {
+			error!("  {} at {}", frame.msg, frame.pos)
+		}
+	}
+}
+
 #[cxx::bridge]
 pub mod nix_logging_cxx {
 	extern "Rust" {
@@ -544,7 +584,14 @@
 		fn add_string_field(&mut self, v: &[u8]);
 		fn emit(&mut self, parent: u64, s: &str);
 		fn emit_result(&mut self, ty: u32);
-
+	}
+	extern "Rust" {
+		type ErrorInfoBuilder;
+		fn new_error_info(lvl: u32, v: &[u8]) -> Box<ErrorInfoBuilder>;
+		fn push_stack_frame(&mut self, v: &[u8], pos: &[u8]);
+		fn emit_error_info(&mut self);
+	}
+	extern "Rust" {
 		fn emit_warn(v: &str);
 		fn emit_stop(id: u64);
 		fn emit_log(lvl: u32, v: &[u8]);
@@ -552,6 +599,15 @@
 	unsafe extern "C++" {
 		include!("nix-eval/src/logging.hh");
 
+		type nix_c_context = crate::nix_raw::c_context;
+
 		fn apply_tracing_logger();
+		unsafe fn extract_error_info(ctx: *const nix_c_context) -> Box<ErrorInfoBuilder>;
 	}
 }
+
+unsafe impl ExternType for crate::nix_raw::c_context {
+	type Id = cxx::type_id!("nix_c_context");
+
+	type Kind = cxx::kind::Opaque;
+}