git.delta.rocks / fleet / refs/commits / 7bc4be6bcef6

difftreelog

source

crates/nix-eval/src/lib.rs36.1 KiBsourcehistory
1use std::borrow::Cow;2use std::cell::RefCell;3use std::collections::HashMap;4use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};5use std::ptr::{null, null_mut};6use std::sync::{Arc, LazyLock, OnceLock};7use std::{array, fmt, slice};89use anyhow::{Context, anyhow, bail};10use camino::{Utf8Path, Utf8PathBuf};11use itertools::Itertools;12use serde::Serialize;13use serde::de::DeserializeOwned;14use std::mem::transmute;1516pub use anyhow::Result;17use tracing::{Span, instrument, warn};1819use self::logging::{ErrorInfoBuilder, nix_logging_cxx};20use self::nix_cxx::set_fetcher_setting;21use self::nix_raw::{22	BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,23	GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,24	GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder, PrimOp,25	PrimOpFun, Store as c_store, StorePath as c_store_path, alloc_primop, alloc_value,26	bindings_builder_free, bindings_builder_insert, c_context, c_context_create, c_context_free,27	clear_err, copy_value, err_NIX_ERR_KEY, err_NIX_ERR_NIX_ERROR, err_NIX_ERR_OVERFLOW,28	err_NIX_ERR_UNKNOWN, err_code, err_info_msg, err_msg, eval_state_build,29	eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,30	expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,31	flake_lock, flake_lock_flags, flake_lock_flags_free, flake_lock_flags_new, flake_reference,32	flake_reference_and_fragment_from_string, flake_reference_parse_flags,33	flake_reference_parse_flags_free, flake_reference_parse_flags_new,34	flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,35	flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,36	get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,37	init_primop, init_string, libexpr_init, libstore_init, libutil_init, list_builder_free,38	list_builder_insert, locked_flake, locked_flake_free, locked_flake_get_output_attrs,39	make_attrs, make_bindings_builder, make_list, make_list_builder, realised_string,40	realised_string_free, realised_string_get_buffer_size, realised_string_get_buffer_start,41	realised_string_get_store_path, realised_string_get_store_path_count, register_primop,42	set_err_msg, setting_set, state_free, store_copy_closure, store_free, store_open,43	store_parse_path, store_path_free, store_path_name, string_realise, value, value_call,44	value_decref, value_force, value_incref,45};4647// Contains macros helpers48pub mod drv;49pub mod logging;50#[doc(hidden)]51pub mod macros;52pub mod scheduler;5354#[doc(hidden)]55pub mod __macro_support {56	pub use std::collections::hash_map::HashMap;5758	pub use anyhow::Context;59	pub use tokio::task::block_in_place;60}61pub mod util;6263#[allow(64	non_upper_case_globals,65	non_camel_case_types,66	non_snake_case,67	dead_code68)]69mod nix_raw {70	include!(concat!(env!("OUT_DIR"), "/bindings.rs"));71}72#[cxx::bridge]73pub mod nix_cxx {74	struct AddFileToStoreResult {75		error: String,76		store_path: String,77		hash: String,78	}79	struct CxxProfileGeneration {80		id: u64,81		store_path: String,82		creation_time_unix: i64,83		current: bool,84	}85	struct CxxListGenerationsResult {86		error: String,87		generations: Vec<CxxProfileGeneration>,88	}89	struct CxxBuildResult {90		error: String,91		outputs: Vec<String>,92	}93	unsafe extern "C++" {94		type nix_fetchers_settings;95		type Store;96		include!("nix-eval/src/lib.hh");9798		#[allow(clippy::missing_safety_doc)]99		unsafe fn set_fetcher_setting(100			settings: *mut nix_fetchers_settings,101			setting: *const c_char,102			value: *const c_char,103		);104105		#[allow(clippy::missing_safety_doc)]106		unsafe fn switch_profile(store: *mut Store, profile: &str, store_path: &str) -> String;107108		#[allow(clippy::missing_safety_doc)]109		unsafe fn sign_closure(store: *mut Store, store_path: &str, key_file: &str) -> String;110111		#[allow(clippy::missing_safety_doc)]112		unsafe fn add_file_to_store(113			store: *mut Store,114			name: &str,115			path: &str,116		) -> AddFileToStoreResult;117118		fn list_generations(profile_path: &str) -> CxxListGenerationsResult;119120		#[allow(clippy::missing_safety_doc)]121		unsafe fn build_drv_outputs(122			store: *mut Store,123			drv_path: &str,124			output_names_joined: &str,125		) -> CxxBuildResult;126127		#[allow(clippy::missing_safety_doc)]128		unsafe fn substitute_paths(store: *mut Store, paths_joined: &str) -> CxxBuildResult;129130		#[allow(clippy::missing_safety_doc)]131		unsafe fn is_valid_path(store: *mut Store, path: &str) -> bool;132	}133}134135#[derive(Debug, PartialEq, Eq)]136pub enum NixType {137	Thunk,138	Int,139	Float,140	Bool,141	String,142	Path,143	Null,144	Attrs,145	List,146	Function,147	External,148}149impl NixType {150	fn from_int(c: c_uint) -> Self {151		match c {152			0 => Self::Thunk,153			1 => Self::Int,154			2 => Self::Float,155			3 => Self::Bool,156			4 => Self::String,157			5 => Self::Path,158			6 => Self::Null,159			7 => Self::Attrs,160			8 => Self::List,161			9 => Self::Function,162			10 => Self::External,163			_ => unreachable!("unknown nix type: {c}"),164		}165	}166}167168enum FunctorKind {169	Function,170	Functor,171}172173#[derive(Debug)]174#[repr(i32)]175pub enum NixErrorKind {176	Unknown = err_NIX_ERR_UNKNOWN,177	Overflow = err_NIX_ERR_OVERFLOW,178	Key = err_NIX_ERR_KEY,179	Generic = err_NIX_ERR_NIX_ERROR,180}181impl NixErrorKind {182	fn from_int(v: c_int) -> Option<Self> {183		Some(match v {184			0 => return None,185			nix_raw::err_NIX_ERR_UNKNOWN => Self::Unknown,186			nix_raw::err_NIX_ERR_OVERFLOW => Self::Overflow,187			nix_raw::err_NIX_ERR_KEY => Self::Key,188			nix_raw::err_NIX_ERR_NIX_ERROR => Self::Generic,189			_ => {190				debug_assert!(false, "unexpected nix error kind: {v}");191				Self::Unknown192			}193		})194	}195}196197pub fn gc_now() {198	unsafe { gc_now_raw() };199}200201pub fn gc_register_my_thread() {202	assert_eq!(unsafe { GC_thread_is_registered() }, 0);203204	let mut sb = GC_stack_base {205		mem_base: null_mut(),206	};207	let r = unsafe { GC_get_stack_base(&mut sb) };208	if r as u32 != GC_SUCCESS {209		panic!("failed to get thread stack base");210	}211	unsafe { GC_register_my_thread(&sb) };212}213pub fn gc_unregister_my_thread() {214	assert_eq!(unsafe { GC_thread_is_registered() }, 1);215216	unsafe { GC_unregister_my_thread() };217}218219pub struct ThreadRegisterGuard {}220impl ThreadRegisterGuard {221	#[allow(clippy::new_without_default)]222	pub fn new() -> Self {223		gc_register_my_thread();224		Self {}225	}226}227impl Drop for ThreadRegisterGuard {228	fn drop(&mut self) {229		gc_unregister_my_thread();230	}231}232233#[repr(transparent)]234pub struct NixContext(*mut c_context);235impl NixContext {236	pub fn set_err_raw(&mut self, err: NixErrorKind, msg: &CStr) {237		unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };238	}239	pub fn set_err(&mut self, err: anyhow::Error) {240		let fmt = format!("{err:?}").replace("\0", "\\0");241		self.set_err_raw(242			NixErrorKind::Generic,243			&CString::new(fmt).expect("NUL bytes were just replaced"),244		);245	}246	pub fn new() -> Self {247		let ctx = unsafe { c_context_create() };248		Self(ctx)249	}250	fn error_kind(&self) -> Option<NixErrorKind> {251		let code = unsafe { err_code(self.0) };252		NixErrorKind::from_int(code)253	}254	fn error<'t>(&self) -> Option<(Cow<'t, str>, Option<Box<ErrorInfoBuilder>>)> {255		if let NixErrorKind::Generic = self.error_kind()? {256			let ei = unsafe { logging::nix_logging_cxx::extract_error_info(self.0) };257			let mut err_out = String::new();258			unsafe {259				err_info_msg(260					null_mut(),261					self.0,262					Some(copy_nix_str),263					(&raw mut err_out).cast(),264				)265			};266			return Some((Cow::Owned(err_out), Some(ei)));267		};268269		// TODO: Can throw error (resulting in panic) if unable to retrieve error. Should be able to resolve by passing context as a first argument,270		// but it looks ugly271		let str = unsafe { err_msg(null_mut(), self.0, null_mut()) };272		Some((unsafe { CStr::from_ptr(str) }.to_string_lossy(), None))273	}274	fn clean_err(&mut self) {275		unsafe {276			clear_err(self.0);277		}278	}279280	fn bail_if_error(&self) -> Result<()> {281		if let Some((err, stack)) = self.error() {282			let mut e = Err(anyhow!("{err}"));283			if let Some(stack) = stack {284				for ele in stack.stack_frames {285					e = e.with_context(|| {286						if ele.pos.is_empty() {287							ele.msg288						} else {289							format!("{} at {}", ele.msg, ele.pos)290						}291					})292				}293			}294			return e.context("<nix frames>");295		};296		Ok(())297	}298299	fn run_in_context<T>(&mut self, f: impl FnOnce(*mut c_context) -> T) -> Result<T> {300		self.clean_err();301		let o = f(self.0);302		self.bail_if_error()?;303		self.clean_err();304		Ok(o)305	}306}307308impl Default for NixContext {309	fn default() -> Self {310		Self::new()311	}312}313impl Drop for NixContext {314	fn drop(&mut self) {315		unsafe {316			c_context_free(self.0);317		}318	}319}320struct GlobalState {321	// Store should be valid as long as EvalState is valid322	#[allow(dead_code)]323	store: Store,324	state: EvalState,325}326impl GlobalState {327	fn new() -> Result<Self> {328		let mut ctx = NixContext::new();329		let store = ctx330			.run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })331			.map(Store)?;332333		let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;334		ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;335		ctx.run_in_context(|c| unsafe {336			eval_state_builder_set_eval_setting(337				c,338				builder,339				c"lazy-trees".as_ptr(),340				c"true".as_ptr(),341			)342		})?;343		ctx.run_in_context(|c| unsafe {344			eval_state_builder_set_eval_setting(345				c,346				builder,347				c"lazy-locks".as_ptr(),348				c"true".as_ptr(),349			)350		})?;351		let state = ctx352			.run_in_context(|c| unsafe { eval_state_build(c, builder) })353			.map(EvalState)?;354355		Ok(Self { store, state })356	}357}358359struct ThreadState {360	ctx: NixContext,361}362impl ThreadState {363	fn new() -> Result<Self> {364		let ctx = NixContext::new();365366		Ok(Self { ctx })367	}368}369370static GLOBAL_STATE: LazyLock<GlobalState> =371	LazyLock::new(|| GlobalState::new().expect("global state init shouldn't fail"));372373thread_local! {374	static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));375}376pub(crate) fn with_default_context<T>(377	f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T,378) -> Result<T> {379	let global = &GLOBAL_STATE.state;380	let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));381	let mut ctx = NixContext(ctx);382	let v = ctx.run_in_context(|c| f(c, state));383	// It is reused for thread384	std::mem::forget(ctx);385	v386}387388/// Same as with_default_context, but also passes store...389/// Yep, this code is garbage and needs to be refactored.390pub(crate) fn with_store_context<T>(391	f: impl FnOnce(*mut c_context, *mut c_store, *mut c_eval_state) -> T,392) -> Result<T> {393	let global = &GLOBAL_STATE;394	let (ctx, store, state) =395		THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.store.0, global.state.0));396	let mut ctx = NixContext(ctx);397	let v = ctx.run_in_context(|c| f(c, store, state));398	std::mem::forget(ctx);399	v400}401402pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {403	with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())404}405406#[instrument(skip(dst))]407pub fn copy_closure_to(dst: &Store, path: &Utf8Path) -> Result<()> {408	let path_c = CString::new(path.as_str())?;409	with_store_context(|c, src_store, _state| -> Result<()> {410		let sp = unsafe { store_parse_path(c, src_store, path_c.as_ptr()) };411		if sp.is_null() {412			bail!("failed to parse store path {path}");413		}414		let rc = unsafe { store_copy_closure(c, src_store, dst.0, sp) };415		unsafe { store_path_free(sp) };416		if rc != nix_raw::err_NIX_OK {417			bail!("store_copy_closure failed (code {rc})");418		}419		Ok(())420	})?421}422423#[instrument]424pub fn switch_profile(profile: &str, store_path: &Utf8Path) -> Result<()> {425	let msg = with_store_context(|_c, store, _state| unsafe {426		nix_cxx::switch_profile(store.cast(), profile, store_path.as_str())427	})?428	.to_string();429	if msg.is_empty() {430		Ok(())431	} else {432		bail!("failed to switch profile {profile}: {msg}");433	}434}435436// TODO: fleet operator-managed key file437#[instrument]438pub fn sign_closure(store_path: &str, key_file: &str) -> Result<()> {439	let msg = with_store_context(|_c, store, _state| unsafe {440		nix_cxx::sign_closure(store.cast(), store_path, key_file)441	})?442	.to_string();443	if msg.is_empty() {444		Ok(())445	} else {446		bail!("failed to sign {store_path}: {msg}");447	}448}449450#[derive(Debug)]451pub struct AddedFile {452	pub store_path: Utf8PathBuf,453	pub hash: String,454}455456#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]457pub struct ProfileGeneration {458	pub id: u64,459	pub store_path: Utf8PathBuf,460	pub creation_time_unix: i64,461	pub current: bool,462}463464#[instrument]465pub fn list_generations(profile_path: &str) -> Result<Vec<ProfileGeneration>> {466	let res = nix_cxx::list_generations(profile_path);467	if !res.error.is_empty() {468		bail!(469			"failed to list generations at {profile_path}: {}",470			res.error471		);472	}473	Ok(res474		.generations475		.into_iter()476		.map(|g| ProfileGeneration {477			id: g.id,478			store_path: Utf8PathBuf::from(g.store_path),479			creation_time_unix: g.creation_time_unix,480			current: g.current,481		})482		.collect())483}484485#[instrument]486pub fn add_file_to_store(name: &str, path: &Utf8Path) -> Result<AddedFile> {487	let res = with_store_context(|_c, store, _state| unsafe {488		nix_cxx::add_file_to_store(store.cast(), name, path.as_str())489	})?;490	if !res.error.is_empty() {491		bail!("failed to add {path} to store: {}", res.error);492	}493	Ok(AddedFile {494		store_path: Utf8PathBuf::from(res.store_path),495		hash: res.hash,496	})497}498499pub fn build_drv_outputs(drv_path: &str, output_names: &[String]) -> Result<Vec<String>> {500	let joined = output_names.join("\n");501	let res = with_store_context(|_c, store, _state| unsafe {502		nix_cxx::build_drv_outputs(store.cast(), drv_path, &joined)503	})?;504	if !res.error.is_empty() {505		bail!("build of {drv_path} failed: {}", res.error);506	}507	Ok(res.outputs)508}509510pub fn substitute_paths(paths: &[String]) -> Result<Vec<String>> {511	let joined = paths.join("\n");512	let res = with_store_context(|_c, store, _state| unsafe {513		nix_cxx::substitute_paths(store.cast(), &joined)514	})?;515	if !res.error.is_empty() {516		warn!("substitute_paths reported: {}", res.error);517	}518	Ok(res.outputs)519}520521pub fn is_valid_path(path: &str) -> Result<bool> {522	with_store_context(|_c, store, _state| unsafe { nix_cxx::is_valid_path(store.cast(), path) })523}524525pub struct FetchSettings(*mut fetchers_settings);526impl FetchSettings {527	pub fn new() -> Self {528		Self::try_new().expect("allocation should not fail")529	}530	fn try_new() -> Result<Self> {531		with_default_context(|c, _| unsafe { fetchers_settings_new(c) }).map(Self)532	}533	pub fn set(&mut self, setting: &CStr, value: &CStr) {534		unsafe {535			set_fetcher_setting(self.0.cast(), setting.as_ptr(), value.as_ptr());536		};537	}538}539unsafe impl Send for FetchSettings {}540unsafe impl Sync for FetchSettings {}541542impl Default for FetchSettings {543	fn default() -> Self {544		Self::new()545	}546}547548impl Drop for FetchSettings {549	fn drop(&mut self) {550		unsafe { fetchers_settings_free(self.0) };551	}552}553pub struct FlakeSettings(*mut flake_settings);554impl FlakeSettings {555	pub fn new() -> Result<Self> {556		with_default_context(|c, _| unsafe { flake_settings_new(c) }).map(Self)557	}558}559unsafe impl Send for FlakeSettings {}560unsafe impl Sync for FlakeSettings {}561impl Drop for FlakeSettings {562	fn drop(&mut self) {563		unsafe {564			flake_settings_free(self.0);565		}566	}567}568569pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);570impl FlakeReferenceParseFlags {571	pub fn new(settings: &FlakeSettings) -> Result<Self> {572		with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })573			.map(Self)574	}575	pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {576		with_default_context(|c, _| {577			unsafe {578				flake_reference_parse_flags_set_base_directory(579					c,580					self.0,581					dir.as_ptr().cast(),582					dir.len(),583				)584			};585		})586	}587}588impl Drop for FlakeReferenceParseFlags {589	fn drop(&mut self) {590		unsafe {591			flake_reference_parse_flags_free(self.0);592		}593	}594}595pub struct FlakeLockFlags(*mut flake_lock_flags);596impl FlakeLockFlags {597	pub fn new(settings: &FlakeSettings) -> Result<Self> {598		let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) })599			.map(Self)?;600		// with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?;601602		Ok(o)603	}604}605impl Drop for FlakeLockFlags {606	fn drop(&mut self) {607		unsafe {608			flake_lock_flags_free(self.0);609		}610	}611}612613pub(crate) unsafe extern "C" fn copy_nix_str(614	start: *const c_char,615	n: c_uint,616	user_data: *mut c_void,617) {618	let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };619	let s = std::str::from_utf8(s).expect("c string has invalid utf-8");620	unsafe { *user_data.cast::<String>() = s.to_owned() };621}622623pub struct Store(*mut c_store);624unsafe impl Send for Store {}625unsafe impl Sync for Store {}626627impl Store {628	pub fn open(uri: &str) -> Result<Self> {629		let uri = CString::new(uri)?;630		let ptr = with_default_context(|c, _| unsafe { store_open(c, uri.as_ptr(), null_mut()) })?;631		if ptr.is_null() {632			bail!("failed to open store");633		}634		Ok(Store(ptr))635	}636637	fn parse_path(&self, path: &CStr) -> Result<StorePath> {638		with_default_context(|c, _| {639			StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })640		})641	}642}643impl Drop for Store {644	fn drop(&mut self) {645		unsafe { store_free(self.0) }646	}647}648649#[repr(transparent)]650pub struct EvalState(*mut c_eval_state);651unsafe impl Send for EvalState {}652unsafe impl Sync for EvalState {}653654impl Drop for EvalState {655	fn drop(&mut self) {656		unsafe {657			state_free(self.0);658		}659	}660}661662pub struct FlakeReference(*mut flake_reference);663impl FlakeReference {664	#[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]665	pub fn new(666		s: &str,667		flake: &FlakeSettings,668		parse: &FlakeReferenceParseFlags,669		fetch: &FetchSettings,670	) -> Result<(Self, String)> {671		let mut out = null_mut();672		let mut fragment = String::new();673		// let fetch_settings = fetcher_settings;674		with_default_context(|c, _| unsafe {675			flake_reference_and_fragment_from_string(676				c,677				fetch.0,678				flake.0,679				parse.0,680				s.as_ptr().cast(),681				s.len(),682				&mut out,683				Some(copy_nix_str),684				(&raw mut fragment).cast(),685			)686		})?;687		assert!(!out.is_null());688689		Ok((Self(out), fragment))690	}691	#[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]692	pub fn lock(693		&mut self,694		fetch: &FetchSettings,695		flake: &FlakeSettings,696		lock: &FlakeLockFlags,697	) -> Result<LockedFlake> {698		with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })699			.map(LockedFlake)700	}701}702unsafe impl Send for FlakeReference {}703unsafe impl Sync for FlakeReference {}704705pub struct LockedFlake(*mut locked_flake);706impl LockedFlake {707	pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {708		with_default_context(|c, es| unsafe {709			locked_flake_get_output_attrs(c, settings.0, es, self.0)710		})711		.map(Value)712	}713}714unsafe impl Send for LockedFlake {}715unsafe impl Sync for LockedFlake {}716impl Drop for LockedFlake {717	fn drop(&mut self) {718		unsafe {719			locked_flake_free(self.0);720		};721	}722}723724type FieldName = [u8; 64];725fn init_field_name(v: &str) -> FieldName {726	let mut f = [0; 64];727	assert!(v.len() < 64, "max field name is 63 chars");728	assert!(729		v.bytes().all(|v| v != 0),730		"nul bytes are unsupported in field name"731	);732	f[0..v.len()].copy_from_slice(v.as_bytes());733	f734}735736pub struct RealisedString(*mut realised_string);737impl fmt::Debug for RealisedString {738	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {739		self.as_str().fmt(f)740	}741}742743impl RealisedString {744	pub fn as_str(&self) -> &str {745		let len = unsafe { realised_string_get_buffer_size(self.0) };746		let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();747		let data = unsafe { slice::from_raw_parts(data, len) };748		std::str::from_utf8(data).expect("non-utf8 strings not supported")749	}750	pub fn path_count(&self) -> usize {751		unsafe { realised_string_get_store_path_count(self.0) }752	}753	pub fn path(&self, i: usize) -> String {754		assert!(i < self.path_count());755		let path = unsafe { realised_string_get_store_path(self.0, i) };756		let mut err_out = String::new();757		unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };758		err_out759	}760}761762unsafe impl Send for RealisedString {}763impl Drop for RealisedString {764	fn drop(&mut self) {765		unsafe { realised_string_free(self.0) }766	}767}768769#[repr(transparent)]770pub struct Value(*mut value);771772unsafe impl Send for Value {}773unsafe impl Sync for Value {}774775pub trait AsFieldName {776	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;777	fn to_field_name(&self) -> Result<String>;778}779impl AsFieldName for Value {780	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {781		let f = self.to_string()?;782		v(init_field_name(&f))783	}784	fn to_field_name(&self) -> Result<String> {785		self.to_string()786	}787}788impl<E> AsFieldName for E789where790	E: AsRef<str>,791{792	fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {793		let f = self.as_ref();794		v(init_field_name(f))795	}796	fn to_field_name(&self) -> Result<String> {797		Ok(self.as_ref().to_owned())798	}799}800801struct AttrsBuilder(*mut c_bindings_builder);802impl AttrsBuilder {803	fn new(capacity: usize) -> Self {804		with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })805			.map(Self)806			.expect("alloc should not fail")807	}808	fn insert(&mut self, k: &impl AsFieldName, v: Value) {809		k.as_field_name(|name| {810			with_default_context(|c, _| unsafe {811				bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);812				// bindings_builder_insert doesn't do incref813			})814		})815		.expect("builder insert shouldn't fail");816	}817}818impl Drop for AttrsBuilder {819	fn drop(&mut self) {820		unsafe { bindings_builder_free(self.0) };821	}822}823824struct ListBuilder(*mut c_list_builder, c_uint);825impl ListBuilder {826	fn new(capacity: usize) -> Self {827		with_default_context(|c, es| unsafe { make_list_builder(c, es, capacity) })828			.map(|l| Self(l, 0))829			.expect("alloc should not fail")830	}831}832impl ListBuilder {833	fn push(&mut self, v: Value) {834		with_default_context(|c, _| unsafe {835			list_builder_insert(836				c,837				self.0,838				{839					let v = self.1;840					self.1 += 1;841					v842				},843				v.0,844			)845		})846		.expect("list insert shouldn't fail");847	}848}849impl Drop for ListBuilder {850	fn drop(&mut self) {851		unsafe { list_builder_free(self.0) };852	}853}854855impl Value {856	pub fn new_primop(v: NativeFn) -> Self {857		let out = Self::new_uninit();858		with_default_context(|c, _| unsafe { init_primop(c, out.0, v.0) })859			.expect("primop initialization should not fail");860		out861	}862	pub fn new_attrs(v: HashMap<&str, Value>) -> Self {863		let out = Self::new_uninit();864		let mut b = AttrsBuilder::new(v.len());865		for (k, v) in v {866			b.insert(&k, v);867		}868		with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })869			.expect("attrs initialization should not fail");870871		out872	}873	fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {874		let out = Self::new_uninit();875		let mut b = ListBuilder::new(v.len());876		for v in v {877			b.push(v.into());878		}879		with_default_context(|c, _| unsafe { make_list(c, b.0, out.0) })880			.expect("list initialization should not fail");881882		out883	}884	fn new_uninit() -> Self {885		let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })886			.expect("value allocation should not fail");887		Self(out)888	}889	pub fn new_str(v: &str) -> Self {890		let s = CString::new(v).expect("string should not contain NULs");891		let out = Self::new_uninit();892		// String is copied, `s` is free to be dropped893		with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })894			.expect("string initialization should not fail");895		out896	}897	pub fn new_int(i: i64) -> Self {898		let out = Self::new_uninit();899		with_default_context(|c, _| unsafe { init_int(c, out.0, i) })900			.expect("int initialization should not fail");901		out902	}903	pub fn new_bool(v: bool) -> Self {904		let out = Self::new_uninit();905		with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })906			.expect("bool initialization should not fail");907		out908	}909	// TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless910	// fn force(&mut self, st: &mut EvalState) -> Result<()> {911	// 	with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;912	// 	Ok(())913	// }914	pub fn type_of(&self) -> NixType {915		let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })916			.expect("get_type should not fail");917		NixType::from_int(ty)918	}919	fn builtin_to_string(&self) -> Result<Self> {920		let builtin = Self::eval("builtins.toString")?;921		builtin.call(self.clone())922	}923	fn force(&mut self, s: *mut nix_raw::EvalState) -> Result<()> {924		with_default_context(|c, _| unsafe { value_force(c, s, self.0) })?;925		Ok(())926	}927	pub fn to_string(&self) -> Result<String> {928		let ty = self.type_of();929		if !matches!(ty, NixType::String) {930			bail!("unexpected type: {ty:?}, expected string");931		}932		let mut str_out = String::new();933		with_default_context(|c, _| unsafe {934			get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())935		})?;936937		Ok(str_out)938	}939	pub fn to_realised_string(&self) -> Result<RealisedString> {940		with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })941			.map(RealisedString)942943		// let store_paths = unsafe { nix_raw::realised_string_get_store_path_count(str) };944		// for i in 0..store_paths {945		// 	let store_path = unsafe { nix_raw::realised_string_get_store_path(str, i) };946		// 	nix_raw::store_path_name(store_path, callback, user_data);947		// }948		// dbg!(store_paths);949		// todo!();950	}951952	pub fn has_field(&self, field: &str) -> Result<bool> {953		if !matches!(self.type_of(), NixType::Attrs) {954			bail!("invalid type: expected attrs");955		}956957		let f = init_field_name(field);958		with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })959	}960	// pub fn derivation_path(&self) {961	// 	nix_raw::real962	// }963	pub fn list_fields(&self) -> Result<Vec<String>> {964		if !matches!(self.type_of(), NixType::Attrs) {965			bail!("invalid type: expected attrs");966		}967968		let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;969		let mut out = Vec::with_capacity(len as usize);970971		for i in 0..len {972			let name =973				with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;974			let c = unsafe { CStr::from_ptr(name) };975			out.push(c.to_str().expect("nix field names are utf-8").to_owned());976		}977		Ok(out)978	}979	pub fn get_elem(&self, v: usize) -> Result<Self> {980		if !matches!(self.type_of(), NixType::List) {981			bail!("invalid type: expected list");982		}983		let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;984		if v >= len {985			bail!("oob list get: {v} >= {len}");986		}987988		with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)989	}990	pub fn attrs_update(self, other: Value /*, ignore_errors: bool*/) -> Result<Self> {991		let attrs_update_fn = Self::eval("a: b: a // b")?;992993		attrs_update_fn994			.call(self)?995			.call(other)996			.context("attrs update")997	}998	pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {999		if !matches!(self.type_of(), NixType::Attrs) {1000			bail!("invalid type: expected attrs");1001		}10021003		name.as_field_name(|name| {1004			with_default_context(|c, es| unsafe {1005				get_attr_byname(c, self.0, es, name.as_ptr().cast())1006			})1007			.map(Self)1008		})1009		.with_context(|| format!("getting field {:?}", name.to_field_name()))1010	}1011	pub fn call(&self, v: Value) -> Result<Self> {1012		let kind = self1013			.functor_kind()1014			.ok_or_else(|| anyhow!("can only call function or functor"))?;10151016		let function = match kind {1017			FunctorKind::Function => self.clone(),1018			FunctorKind::Functor => {1019				let f = self1020					.get_field("__functor")1021					.context("getting functor value")?;1022				assert_eq!(1023					f.type_of(),1024					NixType::Function,1025					"invalid functor encountered"1026				);1027				f1028			}1029		};10301031		let out = Value::new_uninit();1032		with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;10331034		Ok(out)1035	}1036	pub fn eval(v: &str) -> Result<Self> {1037		let s = CString::new(v).expect("expression shouldn't have internal NULs");1038		let out = Self::new_uninit();1039		with_default_context(|c, es| unsafe {1040			expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)1041		})?;1042		Ok(out)1043	}1044	#[instrument(name = "build", skip(self), fields(output))]1045	pub fn build(&self, output: &str) -> Result<Utf8PathBuf> {1046		if !self.is_derivation() {1047			bail!("expected derivation to build")1048		}1049		let output_name = self1050			.get_field("outputName")1051			.context("getting output name field")?1052			.to_string()?;1053		let v = if output_name != output {1054			let out = self.get_field(output).context("getting target output")?;1055			if !out.is_derivation() {1056				bail!("unknown output: {output}");1057			}1058			out1059		} else {1060			self.clone()1061		};10621063		let drv_path = v1064			.get_field("drvPath")1065			.context("getting drvPath")?1066			.to_string()?;1067		let graph = Arc::new(drv::DrvGraph::resolve(&drv_path)?);1068		let _guard = logging::register_build_graph(&Span::current(), &graph);10691070		scheduler::build_graph_sync(graph.clone(), vec![output.to_owned()])?;10711072		let s = v.builtin_to_string()?;1073		let rs = s.to_realised_string()?;1074		let out_path = rs.as_str().to_owned();1075		Ok(Utf8PathBuf::from(out_path))1076	}1077	pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {1078		let to_json = Self::eval("builtins.toJSON")?;1079		let s = to_json.call(self.clone())?.to_string()?;1080		Ok(serde_json::from_str(&s)?)1081	}1082	pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {1083		Self::eval(&nixlike::serialize(v)?)1084	}10851086	// Convert to string/evaluate derivations/etc1087	// fn to_string_weak(&self) -> Result<String> {1088	// 	// TODO: For now, it works exactly like to_string, see the comment for fn force()1089	// 	self.to_string()1090	// }10911092	fn is_derivation(&self) -> bool {1093		if !matches!(self.type_of(), NixType::Attrs) {1094			return false;1095		}1096		let Some(ty) = self.get_field("type").ok() else {1097			return false;1098		};1099		matches!(ty.to_string().as_deref(), Ok("derivation"))1100	}1101	fn functor_kind(&self) -> Option<FunctorKind> {1102		match self.type_of() {1103			NixType::Attrs => self1104				.has_field("__functor")1105				.expect("has_field shouldn't fail for attrs")1106				.then_some(FunctorKind::Functor),1107			NixType::Function => Some(FunctorKind::Function),1108			_ => None,1109		}1110	}1111	pub fn is_function(&self) -> bool {1112		self.functor_kind().is_some()1113	}1114	pub fn is_null(&self) -> bool {1115		matches!(self.type_of(), NixType::Null)1116	}1117	pub fn is_string(&self) -> bool {1118		matches!(self.type_of(), NixType::String)1119	}1120	pub fn is_attrs(&self) -> bool {1121		matches!(self.type_of(), NixType::Attrs)1122	}1123}11241125impl From<String> for Value {1126	fn from(value: String) -> Self {1127		Value::new_str(&value)1128	}1129}1130impl From<bool> for Value {1131	fn from(value: bool) -> Self {1132		Value::new_bool(value)1133	}1134}1135impl From<&str> for Value {1136	fn from(value: &str) -> Self {1137		Value::new_str(value)1138	}1139}1140impl<T> From<Vec<T>> for Value1141where1142	T: Into<Value>,1143{1144	fn from(value: Vec<T>) -> Self {1145		Value::new_list(value)1146	}1147}11481149impl Clone for Value {1150	fn clone(&self) -> Self {1151		with_default_context(|c, _| unsafe { value_incref(c, self.0) })1152			.expect("value incref should not fail");1153		Self(self.0)1154	}1155}1156impl Drop for Value {1157	fn drop(&mut self) {1158		with_default_context(|c, _| unsafe { value_decref(c, self.0) })1159			.expect("value drop should not fail");1160	}1161}11621163static TOKIO_FOR_NIX: OnceLock<Arc<tokio::runtime::Runtime>> = OnceLock::new();11641165pub fn init_libraries() {1166	unsafe { GC_allow_register_threads() };11671168	let mut ctx = NixContext::new();1169	ctx.run_in_context(|c| unsafe { libutil_init(c) })1170		.expect("util init should not fail");1171	ctx.run_in_context(|c| unsafe { libstore_init(c) })1172		.expect("store init should not fail");1173	ctx.run_in_context(|c| unsafe { libexpr_init(c) })1174		.expect("expr init should not fail");11751176	nix_logging_cxx::apply_tracing_logger();1177}11781179pub fn init_tokio_for_nix(tokio: Arc<tokio::runtime::Runtime>) {1180	TOKIO_FOR_NIX1181		.set(tokio)1182		.expect("tokio for nix should only be initialized once");1183}11841185pub fn await_in_nix<F: Send + 'static>(f: impl Future<Output = F> + Send + 'static) -> F {1186	// It should be possible to do Handle::current(), but some of the planned features don't work well with that1187	let runtime = TOKIO_FOR_NIX1188		.get()1189		.expect("init_tokio_for_nix was not called");1190	std::thread::spawn(move || runtime.block_on(f))1191		.join()1192		.expect("await_in_nix inner thread panicked")1193}11941195unsafe extern "C" fn nix_primop_closure_adapter<const N: usize>(1196	user_data: *mut c_void,1197	mut context: *mut c_context,1198	state: *mut nix_raw::EvalState,1199	args: *mut *mut value,1200	ret: *mut value,1201) {1202	let user_closure: &UserClosure<N> = unsafe { &*user_data.cast_const().cast() };1203	let args: [&Value; N] = array::from_fn(|i| {1204		let v: &mut Value = unsafe { &mut *args.add(i).cast() };1205		v as &Value1206	});1207	let ctx: &mut NixContext = unsafe { transmute(&mut context) };12081209	let state: &EvalState = unsafe { std::mem::transmute(&state) };12101211	match user_closure(state, args) {1212		Ok(v) => {1213			unsafe { copy_value(context, ret, v.0) };1214		}1215		Err(e) => {1216			ctx.set_err(e);1217		}1218	}1219}12201221type UserClosure<const N: usize> = Box<dyn Fn(&EvalState, [&Value; N]) -> Result<Value>>;12221223pub struct NativeFn(*mut PrimOp);1224impl NativeFn {1225	pub fn new<const N: usize>(1226		name: &'static CStr,1227		doc: &'static CStr,1228		args: [&'static CStr; N],1229		f: impl Fn(&EvalState, [&Value; N]) -> Result<Value> + 'static,1230	) -> Self {1231		// Double-boxing to make it thin pointer, as vtable gets outside of first Box1232		let closure: Box<UserClosure<N>> = Box::new(Box::new(f));1233		let f: PrimOpFun = Some(nix_primop_closure_adapter::<N>);1234		let mut args = args.into_iter().map(|v| v.as_ptr()).collect_vec();1235		args.push(null());1236		let args = args.as_mut_ptr();1237		let primop = unsafe {1238			alloc_primop(1239				null_mut(),1240				f,1241				N as i32,1242				name.as_ptr(),1243				args,1244				doc.as_ptr(),1245				Box::into_raw(closure).cast(),1246			)1247		};12481249		assert!(!primop.is_null(), "primop allocation should not fail");12501251		Self(primop)1252	}1253	pub fn register(self) {1254		unsafe { register_primop(null_mut(), self.0) };1255	}1256}12571258struct StorePath(*mut c_store_path);1259impl StorePath {}12601261impl Drop for StorePath {1262	fn drop(&mut self) {1263		unsafe { store_path_free(self.0) }1264	}1265}12661267#[test_log::test]1268fn test_native() -> Result<()> {1269	init_libraries();1270	NativeFn::new(1271		c"__uppercaseSuffix2",1272		c"make string uppercase and add suffix",1273		[c"str", c"suffix"],1274		|_, [str, suffix]: [&Value; 2]| {1275			let str = str.to_string()?;1276			let suffix = suffix.to_string()?;1277			Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))1278		},1279	)1280	.register();12811282	let mut fetch_settings = FetchSettings::new();1283	fetch_settings.set(c"warn-dirty", c"false");12841285	let manifest = format!("git+file://{}/../../", env!("CARGO_MANIFEST_DIR"));1286	let flake = FlakeSettings::new()?;1287	let parse = FlakeReferenceParseFlags::new(&flake)?;1288	let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;1289	let lock = FlakeLockFlags::new(&flake)?;1290	let locked = r.lock(&fetch_settings, &flake, &lock)?;1291	let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;12921293	let builtins = Value::eval("builtins")?;1294	assert_eq!(builtins.type_of(), NixType::Attrs);12951296	assert_eq!(attrs.type_of(), NixType::Attrs);1297	let test_data = nix_go!(attrs.testData);12981299	let test_string: String = nix_go_json!(test_data.testString);1300	assert_eq!(test_string, "hello");13011302	let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);1303	let s = CString::new(s.to_string()?).expect("path str is cstring");13041305	let uppercase_suffix = Value::new_primop(NativeFn::new(1306		c"uppercase_suffix",1307		c"make string uppercase and add suffix",1308		[c"str", c"suffix"],1309		|es, [str, suffix]: [&Value; 2]| {1310			let str = str.to_string()?;1311			let suffix = suffix.to_string()?;1312			Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))1313		},1314	));13151316	let test_result: String = nix_go_json!(test_data.testPrimop(uppercase_suffix));1317	assert_eq!(test_result, "PREFIX_BODY_SUFFIX");1318	let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));1319	assert_eq!(test_result, "TESTsuffix");13201321	let drv_path =1322		nix_go!(attrs.packages["x86_64-linux"]["fleet-install-secrets"].drvPath).to_string()?;1323	let graph = drv::DrvGraph::resolve(&drv_path)?;1324	eprintln!(1325		"fleet-install-secrets dependency graph: {} nodes",1326		graph.nodes.len()1327	);1328	for (path, node) in &graph.nodes {1329		if !node.input_drvs.is_empty() {1330			eprintln!("  {} ({} deps)", node.name, node.input_drvs.len());1331		}1332	}13331334	Ok(())1335}13361337// pub struct GcAlloc;1338// unsafe impl GlobalAlloc for GcAlloc {1339// 	unsafe fn alloc(&self, l: Layout) -> *mut u8 {1340// 		let ptr = unsafe { GC_malloc(l.size()) };1341// 		ptr.cast()1342// 	}1343// 	unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {1344// 		// unsafe { GC_free(ptr.cast()) };1345// 	}1346//1347// 	unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {1348// 		let ptr = unsafe { GC_realloc(ptr.cast(), new_size) };1349// 		ptr.cast()1350// 	}1351// }1352//1353// #[global_allocator]1354// static GC: GcAlloc = GcAlloc;