difftreelog
feat list building
in: trunk
1 file changed
crates/nix-eval/src/lib.rsdiffbeforeafterboth1use std::alloc::{GlobalAlloc, Layout};2use std::borrow::Cow;3use std::cell::RefCell;4use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};5use std::ptr::null_mut;6use std::sync::LazyLock;7use std::{collections::HashMap, path::PathBuf};8use std::{fmt, slice};910use anyhow::{Context, anyhow, bail};11use serde::Serialize;12use serde::de::DeserializeOwned;1314pub use anyhow::Result;15use tracing::instrument;1617use self::logging::nix_logging_cxx;18use self::nix_cxx::set_fetcher_setting;19use self::nix_raw::{20 BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,21 GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,22 GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder,23 Store as c_store, StorePath as c_store_path, alloc_value, bindings_builder_free,24 bindings_builder_insert, c_context, c_context_create, c_context_free, clear_err, err_code,25 err_info_msg, err_msg, eval_state_build, eval_state_builder_load, eval_state_builder_new,26 eval_state_builder_set_eval_setting, expr_eval_from_string, fetchers_settings,27 fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags,28 flake_lock_flags_free, flake_lock_flags_new, flake_reference,29 flake_reference_and_fragment_from_string, flake_reference_parse_flags,30 flake_reference_parse_flags_free, flake_reference_parse_flags_new,31 flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,32 flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,33 get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,34 init_string, libexpr_init, libstore_init, libutil_init, list_builder_free, list_builder_insert,35 locked_flake, locked_flake_free, locked_flake_get_output_attrs, make_attrs,36 make_bindings_builder, make_list, make_list_builder, realised_string, realised_string_free,37 realised_string_get_buffer_size, realised_string_get_buffer_start,38 realised_string_get_store_path, realised_string_get_store_path_count, set_err_msg, setting_set,39 state_free, store_open, store_parse_path, store_path_free, store_path_name, string_realise,40 value, value_call, value_decref, value_incref,41};4243// Contains macros helpers44pub mod logging;45#[doc(hidden)]46pub mod macros;47pub mod util;4849#[allow(50 non_upper_case_globals,51 non_camel_case_types,52 non_snake_case,53 dead_code54)]55mod nix_raw {56 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));57}58#[cxx::bridge]59pub mod nix_cxx {60 unsafe extern "C++" {61 type nix_fetchers_settings;62 include!("nix-eval/src/lib.hh");6364 unsafe fn set_fetcher_setting(65 settings: *mut nix_fetchers_settings,66 setting: *const c_char,67 value: *const c_char,68 );69 }70}7172#[derive(Debug, PartialEq, Eq)]73pub enum NixType {74 Thunk,75 Int,76 Float,77 Bool,78 String,79 Path,80 Null,81 Attrs,82 List,83 Function,84 External,85}86impl NixType {87 fn from_int(c: c_uint) -> Self {88 match c {89 0 => Self::Thunk,90 1 => Self::Int,91 2 => Self::Float,92 3 => Self::Bool,93 4 => Self::String,94 5 => Self::Path,95 6 => Self::Null,96 7 => Self::Attrs,97 8 => Self::List,98 9 => Self::Function,99 10 => Self::External,100 _ => unreachable!("unknown nix type: {c}"),101 }102 }103}104105enum FunctorKind {106 Function,107 Functor,108}109110#[derive(Debug)]111#[repr(i32)]112pub enum NixErrorKind {113 Unknown = 1,114 Overflow = 2,115 Key = 3,116 Generic = 4,117}118impl NixErrorKind {119 fn from_int(v: c_int) -> Option<Self> {120 Some(match v {121 0 => return None,122 -1 => Self::Unknown,123 -2 => Self::Overflow,124 -3 => Self::Key,125 -4 => Self::Generic,126 _ => {127 debug_assert!(false, "unexpected nix error kind: {v}");128 Self::Unknown129 }130 })131 }132}133134pub fn gc_now() {135 unsafe { gc_now_raw() };136}137138pub fn gc_register_my_thread() {139 assert_eq!(unsafe { GC_thread_is_registered() }, 0);140141 let mut sb = GC_stack_base {142 mem_base: null_mut(),143 };144 let r = unsafe { GC_get_stack_base(&mut sb) };145 if r as u32 != GC_SUCCESS {146 panic!("failed to get thread stack base");147 }148 unsafe { GC_register_my_thread(&sb) };149}150pub fn gc_unregister_my_thread() {151 assert_eq!(unsafe { GC_thread_is_registered() }, 1);152153 unsafe { GC_unregister_my_thread() };154}155156pub struct ThreadRegisterGuard {}157impl ThreadRegisterGuard {158 #[allow(clippy::new_without_default)]159 pub fn new() -> Self {160 gc_register_my_thread();161 Self {}162 }163}164impl Drop for ThreadRegisterGuard {165 fn drop(&mut self) {166 gc_unregister_my_thread();167 }168}169170pub struct NixContext(*mut c_context);171impl NixContext {172 pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {173 unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };174 }175 pub fn new() -> Self {176 let ctx = unsafe { c_context_create() };177 Self(ctx)178 }179 fn error_kind(&self) -> Option<NixErrorKind> {180 let code = unsafe { err_code(self.0) };181 NixErrorKind::from_int(code)182 }183 fn error<'t>(&self) -> Option<Cow<'t, str>> {184 if let NixErrorKind::Generic = self.error_kind()? {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));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())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) = self.error() {210 bail!("{err}");211 };212 Ok(())213 }214215 fn run_in_context<T>(&mut self, f: impl FnOnce(*mut c_context) -> T) -> Result<T> {216 self.clean_err();217 let o = f(self.0);218 self.bail_if_error()?;219 self.clean_err();220 Ok(o)221 }222}223224impl Default for NixContext {225 fn default() -> Self {226 Self::new()227 }228}229impl Drop for NixContext {230 fn drop(&mut self) {231 unsafe {232 c_context_free(self.0);233 }234 }235}236struct GlobalState {237 // Store should be valid as long as EvalState is valid238 #[allow(dead_code)]239 store: Store,240 state: EvalState,241}242impl GlobalState {243 fn new() -> Result<Self> {244 let mut ctx = NixContext::new();245 let store = ctx246 .run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })247 .map(Store)?;248249 let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;250 ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;251 ctx.run_in_context(|c| unsafe {252 eval_state_builder_set_eval_setting(253 c,254 builder,255 c"lazy-trees".as_ptr(),256 c"true".as_ptr(),257 )258 })?;259 ctx.run_in_context(|c| unsafe {260 eval_state_builder_set_eval_setting(261 c,262 builder,263 c"lazy-locks".as_ptr(),264 c"true".as_ptr(),265 )266 })?;267 let state = ctx268 .run_in_context(|c| unsafe { eval_state_build(c, builder) })269 .map(EvalState)?;270271 Ok(Self { store, state })272 }273}274275struct ThreadState {276 ctx: NixContext,277}278impl ThreadState {279 fn new() -> Result<Self> {280 let ctx = NixContext::new();281282 Ok(Self { ctx })283 }284}285286static GLOBAL_STATE: LazyLock<GlobalState> =287 LazyLock::new(|| GlobalState::new().expect("global state init shouldn't fail"));288289thread_local! {290 static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));291}292fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {293 let global = &GLOBAL_STATE.state;294 let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));295 let mut ctx = NixContext(ctx);296 let v = ctx.run_in_context(|c| f(c, state));297 // It is reused for thread298 std::mem::forget(ctx);299 v300}301302pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {303 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())304}305306pub struct FetchSettings(*mut fetchers_settings);307impl FetchSettings {308 pub fn new() -> Self {309 Self::try_new().expect("allocation should not fail")310 }311 fn try_new() -> Result<Self> {312 with_default_context(|c, _| unsafe { fetchers_settings_new(c) }).map(Self)313 }314 pub fn set(&mut self, setting: &CStr, value: &CStr) {315 unsafe {316 set_fetcher_setting(self.0.cast(), setting.as_ptr(), value.as_ptr());317 };318 }319}320unsafe impl Send for FetchSettings {}321unsafe impl Sync for FetchSettings {}322323impl Default for FetchSettings {324 fn default() -> Self {325 Self::new()326 }327}328329impl Drop for FetchSettings {330 fn drop(&mut self) {331 unsafe { fetchers_settings_free(self.0) };332 }333}334pub struct FlakeSettings(*mut flake_settings);335impl FlakeSettings {336 pub fn new() -> Result<Self> {337 with_default_context(|c, _| unsafe { flake_settings_new(c) }).map(Self)338 }339}340unsafe impl Send for FlakeSettings {}341unsafe impl Sync for FlakeSettings {}342impl Drop for FlakeSettings {343 fn drop(&mut self) {344 unsafe {345 flake_settings_free(self.0);346 }347 }348}349350pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);351impl FlakeReferenceParseFlags {352 pub fn new(settings: &FlakeSettings) -> Result<Self> {353 with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })354 .map(Self)355 }356 pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {357 with_default_context(|c, _| {358 unsafe {359 flake_reference_parse_flags_set_base_directory(360 c,361 self.0,362 dir.as_ptr().cast(),363 dir.len(),364 )365 };366 })367 }368}369impl Drop for FlakeReferenceParseFlags {370 fn drop(&mut self) {371 unsafe {372 flake_reference_parse_flags_free(self.0);373 }374 }375}376pub struct FlakeLockFlags(*mut flake_lock_flags);377impl FlakeLockFlags {378 pub fn new(settings: &FlakeSettings) -> Result<Self> {379 let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) })380 .map(Self)?;381 // with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?;382383 Ok(o)384 }385}386impl Drop for FlakeLockFlags {387 fn drop(&mut self) {388 unsafe {389 flake_lock_flags_free(self.0);390 }391 }392}393394unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {395 let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };396 let s = std::str::from_utf8(s).expect("c string has invalid utf-8");397 unsafe { *user_data.cast::<String>() = s.to_owned() };398}399400struct Store(*mut c_store);401unsafe impl Send for Store {}402unsafe impl Sync for Store {}403404impl Store {405 fn parse_path(&self, path: &CStr) -> Result<StorePath> {406 with_default_context(|c, _| {407 StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })408 })409 }410}411412struct EvalState(*mut c_eval_state);413unsafe impl Send for EvalState {}414unsafe impl Sync for EvalState {}415416impl Drop for EvalState {417 fn drop(&mut self) {418 unsafe {419 state_free(self.0);420 }421 }422}423424pub struct FlakeReference(*mut flake_reference);425impl FlakeReference {426 #[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]427 pub fn new(428 s: &str,429 flake: &FlakeSettings,430 parse: &FlakeReferenceParseFlags,431 fetch: &FetchSettings,432 ) -> Result<(Self, String)> {433 let mut out = null_mut();434 let mut fragment = String::new();435 // let fetch_settings = fetcher_settings;436 with_default_context(|c, _| unsafe {437 flake_reference_and_fragment_from_string(438 c,439 fetch.0,440 flake.0,441 parse.0,442 s.as_ptr().cast(),443 s.len(),444 &mut out,445 Some(copy_nix_str),446 (&raw mut fragment).cast(),447 )448 })?;449 assert!(!out.is_null());450451 Ok((Self(out), fragment))452 }453 #[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]454 pub fn lock(455 &mut self,456 fetch: &FetchSettings,457 flake: &FlakeSettings,458 lock: &FlakeLockFlags,459 ) -> Result<LockedFlake> {460 with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })461 .map(LockedFlake)462 }463}464unsafe impl Send for FlakeReference {}465unsafe impl Sync for FlakeReference {}466467pub struct LockedFlake(*mut locked_flake);468impl LockedFlake {469 pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {470 with_default_context(|c, es| unsafe {471 locked_flake_get_output_attrs(c, settings.0, es, self.0)472 })473 .map(Value)474 }475}476unsafe impl Send for LockedFlake {}477unsafe impl Sync for LockedFlake {}478impl Drop for LockedFlake {479 fn drop(&mut self) {480 unsafe {481 locked_flake_free(self.0);482 };483 }484}485486type FieldName = [u8; 64];487fn init_field_name(v: &str) -> FieldName {488 let mut f = [0; 64];489 assert!(v.len() < 64, "max field name is 63 chars");490 assert!(491 v.bytes().all(|v| v != 0),492 "nul bytes are unsupported in field name"493 );494 f[0..v.len()].copy_from_slice(v.as_bytes());495 f496}497498pub struct RealisedString(*mut realised_string);499impl fmt::Debug for RealisedString {500 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {501 self.as_str().fmt(f)502 }503}504505impl RealisedString {506 pub fn as_str(&self) -> &str {507 let len = unsafe { realised_string_get_buffer_size(self.0) };508 let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();509 let data = unsafe { slice::from_raw_parts(data, len) };510 std::str::from_utf8(data).expect("non-utf8 strings not supported")511 }512 pub fn path_count(&self) -> usize {513 unsafe { realised_string_get_store_path_count(self.0) }514 }515 pub fn path(&self, i: usize) -> String {516 assert!(i < self.path_count());517 let path = unsafe { realised_string_get_store_path(self.0, i) };518 let mut err_out = String::new();519 unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };520 err_out521 }522}523524unsafe impl Send for RealisedString {}525impl Drop for RealisedString {526 fn drop(&mut self) {527 unsafe { realised_string_free(self.0) }528 }529}530531pub struct Value(*mut value);532533unsafe impl Send for Value {}534unsafe impl Sync for Value {}535536pub trait AsFieldName {537 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;538 fn to_field_name(&self) -> Result<String>;539}540impl AsFieldName for Value {541 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {542 let f = self.to_string()?;543 v(init_field_name(&f))544 }545 fn to_field_name(&self) -> Result<String> {546 self.to_string()547 }548}549impl<E> AsFieldName for E550where551 E: AsRef<str>,552{553 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {554 let f = self.as_ref();555 v(init_field_name(f))556 }557 fn to_field_name(&self) -> Result<String> {558 Ok(self.as_ref().to_owned())559 }560}561562struct AttrsBuilder(*mut c_bindings_builder);563impl AttrsBuilder {564 fn new(capacity: usize) -> Self {565 with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })566 .map(Self)567 .expect("alloc should not fail")568 }569 fn insert(&mut self, k: &impl AsFieldName, v: Value) {570 k.as_field_name(|name| {571 with_default_context(|c, _| unsafe {572 bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);573 // bindings_builder_insert doesn't do incref574 })575 })576 .expect("builder insert shouldn't fail");577 }578}579impl Drop for AttrsBuilder {580 fn drop(&mut self) {581 unsafe { bindings_builder_free(self.0) };582 }583}584585impl Value {586 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {587 let out = Self::new_uninit();588 let mut b = AttrsBuilder::new(v.len());589 for (k, v) in v {590 b.insert(&k, v);591 }592 with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })593 .expect("attrs initialization should not fail");594595 out596 }597 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {598 todo!()599 }600 fn new_uninit() -> Self {601 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })602 .expect("value allocation should not fail");603 Self(out)604 }605 pub fn new_str(v: &str) -> Self {606 let s = CString::new(v).expect("string should not contain NULs");607 let out = Self::new_uninit();608 // String is copied, `s` is free to be dropped609 with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })610 .expect("string initialization should not fail");611 out612 }613 pub fn new_int(i: i64) -> Self {614 let out = Self::new_uninit();615 with_default_context(|c, _| unsafe { init_int(c, out.0, i) })616 .expect("int initialization should not fail");617 out618 }619 pub fn new_bool(v: bool) -> Self {620 let out = Self::new_uninit();621 with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })622 .expect("bool initialization should not fail");623 out624 }625 // TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless626 // fn force(&mut self, st: &mut EvalState) -> Result<()> {627 // with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;628 // Ok(())629 // }630 pub fn type_of(&self) -> NixType {631 let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })632 .expect("get_type should not fail");633 NixType::from_int(ty)634 }635 fn builtin_to_string(&self) -> Result<Self> {636 let builtin = Self::eval("builtins.toString")?;637 builtin.call(self.clone())638 }639 pub fn to_string(&self) -> Result<String> {640 let mut str_out = String::new();641 with_default_context(|c, _| unsafe {642 get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())643 })?;644645 Ok(str_out)646 }647 pub fn to_realised_string(&self) -> Result<RealisedString> {648 with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })649 .map(RealisedString)650651 // let store_paths = unsafe { nix_raw::realised_string_get_store_path_count(str) };652 // for i in 0..store_paths {653 // let store_path = unsafe { nix_raw::realised_string_get_store_path(str, i) };654 // nix_raw::store_path_name(store_path, callback, user_data);655 // }656 // dbg!(store_paths);657 // todo!();658 }659660 pub fn has_field(&self, field: &str) -> Result<bool> {661 let f = init_field_name(field);662 with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })663 }664 // pub fn derivation_path(&self) {665 // nix_raw::real666 // }667 pub fn list_fields(&self) -> Result<Vec<String>> {668 if !matches!(self.type_of(), NixType::Attrs) {669 bail!("invalid type: expected attrs");670 }671672 let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;673 let mut out = Vec::with_capacity(len as usize);674675 for i in 0..len {676 let name =677 with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;678 let c = unsafe { CStr::from_ptr(name) };679 out.push(c.to_str().expect("nix field names are utf-8").to_owned());680 }681 Ok(out)682 }683 pub fn get_elem(&self, v: usize) -> Result<Self> {684 if !matches!(self.type_of(), NixType::List) {685 bail!("invalid type: expected list");686 }687 let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;688 if v >= len {689 bail!("oob list get: {v} >= {len}");690 }691692 with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)693 }694 pub fn attrs_update(self, other: Value) -> Result<Self> {695 let a_fields = self.list_fields()?;696 let b_fields = other.list_fields()?;697 match (a_fields.len(), b_fields.len()) {698 (_, 0) => return Ok(self),699 (0, _) => return Ok(other),700 _ => {}701 }702 let mut out = HashMap::new();703 for f in a_fields.iter() {704 if b_fields.contains(f) {705 break;706 }707 out.insert(f.as_str(), self.get_field(f)?);708 }709 if out.is_empty() {710 // All fields from lhs are overriden by rhs711 return Ok(other);712 }713 for f in b_fields.iter() {714 out.insert(f.as_str(), other.get_field(f)?);715 }716 Ok(Self::new_attrs(out))717 }718 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {719 if !matches!(self.type_of(), NixType::Attrs) {720 bail!("invalid type: expected attrs");721 }722723 name.as_field_name(|name| {724 with_default_context(|c, es| unsafe {725 get_attr_byname(c, self.0, es, name.as_ptr().cast())726 })727 .map(Self)728 })729 .with_context(|| format!("getting field {:?}", name.to_field_name()))730 }731 pub fn call(&self, v: Value) -> Result<Self> {732 let kind = self733 .functor_kind()734 .ok_or_else(|| anyhow!("can only call function or functor"))?;735736 let function = match kind {737 FunctorKind::Function => self.clone(),738 FunctorKind::Functor => {739 let f = self740 .get_field("__functor")741 .context("getting functor value")?;742 assert_eq!(743 f.type_of(),744 NixType::Function,745 "invalid functor encountered"746 );747 f748 }749 };750751 let out = Value::new_uninit();752 with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;753754 Ok(out)755 }756 pub fn eval(v: &str) -> Result<Self> {757 let s = CString::new(v).expect("expression shouldn't have internal NULs");758 let out = Self::new_uninit();759 with_default_context(|c, es| unsafe {760 expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)761 })?;762 Ok(out)763 }764 pub fn build(&self, output: &str) -> Result<PathBuf> {765 if !self.is_derivation() {766 bail!("expected derivation to build")767 }768 let output_name = self769 .get_field("outputName")770 .context("getting output name field")?771 .to_string()?;772 let v = if output_name != output {773 let out = self.get_field(output).context("getting target output")?;774 if !out.is_derivation() {775 bail!("unknown output: {output}");776 }777 out778 } else {779 self.clone()780 };781 // to_string here blocks until the path is built782 let s = v.builtin_to_string()?;783 let rs = s.to_realised_string()?;784 let drv_path = rs.as_str().to_owned();785 Ok(PathBuf::from(drv_path))786 }787 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {788 let to_json = Self::eval("builtins.toJSON")?;789 let s = to_json.call(self.clone())?.to_string()?;790 Ok(serde_json::from_str(&s)?)791 }792 pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {793 Self::eval(&nixlike::serialize(v)?)794 }795796 // Convert to string/evaluate derivations/etc797 // fn to_string_weak(&self) -> Result<String> {798 // // TODO: For now, it works exactly like to_string, see the comment for fn force()799 // self.to_string()800 // }801802 fn is_derivation(&self) -> bool {803 if !matches!(self.type_of(), NixType::Attrs) {804 return false;805 }806 let Some(ty) = self.get_field("type").ok() else {807 return false;808 };809 matches!(ty.to_string().as_deref(), Ok("derivation"))810 }811 fn functor_kind(&self) -> Option<FunctorKind> {812 match self.type_of() {813 NixType::Attrs => self814 .has_field("__functor")815 .expect("has_field shouldn't fail for attrs")816 .then_some(FunctorKind::Functor),817 NixType::Function => Some(FunctorKind::Function),818 _ => None,819 }820 }821 pub fn is_function(&self) -> bool {822 self.functor_kind().is_some()823 }824}825826impl From<String> for Value {827 fn from(value: String) -> Self {828 Value::new_str(&value)829 }830}831impl From<bool> for Value {832 fn from(value: bool) -> Self {833 Value::new_bool(value)834 }835}836impl From<&str> for Value {837 fn from(value: &str) -> Self {838 Value::new_str(value)839 }840}841impl<T> From<Vec<T>> for Value842where843 T: Into<Value>,844{845 fn from(value: Vec<T>) -> Self {846 Value::new_list(value)847 }848}849850impl Clone for Value {851 fn clone(&self) -> Self {852 with_default_context(|c, _| unsafe { value_incref(c, self.0) })853 .expect("value incref should not fail");854 Self(self.0)855 }856}857impl Drop for Value {858 fn drop(&mut self) {859 with_default_context(|c, _| unsafe { value_decref(c, self.0) })860 .expect("value drop should not fail");861 }862}863864pub fn init_libraries() {865 unsafe { GC_allow_register_threads() };866867 let mut ctx = NixContext::new();868 ctx.run_in_context(|c| unsafe { libutil_init(c) })869 .expect("util init should not fail");870 ctx.run_in_context(|c| unsafe { libstore_init(c) })871 .expect("store init should not fail");872 ctx.run_in_context(|c| unsafe { libexpr_init(c) })873 .expect("expr init should not fail");874875 nix_logging_cxx::apply_tracing_logger();876}877878struct StorePath(*mut c_store_path);879impl StorePath {}880881impl Drop for StorePath {882 fn drop(&mut self) {883 unsafe { store_path_free(self.0) }884 }885}886887#[test_log::test]888fn test_native() -> Result<()> {889 init_libraries();890891 let mut fetch_settings = FetchSettings::new();892 fetch_settings.set(c"warn-dirty", c"false");893894 let manifest = format!("git+file://{}/../../", env!("CARGO_MANIFEST_DIR"));895 let flake = FlakeSettings::new()?;896 let parse = FlakeReferenceParseFlags::new(&flake)?;897 let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;898 let lock = FlakeLockFlags::new(&flake)?;899 let locked = r.lock(&fetch_settings, &flake, &lock)?;900 let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;901902 let builtins = Value::eval("builtins")?;903 assert_eq!(builtins.type_of(), NixType::Attrs);904905 assert_eq!(attrs.type_of(), NixType::Attrs);906 let test_data = nix_go!(attrs.testData);907908 let test_string: String = nix_go_json!(test_data.testString);909 assert_eq!(test_string, "hello");910911 let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);912 let s = CString::new(s.to_string()?).expect("path str is cstring");913914 let nix_ctx = NixContext::new();915 let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;916917 nix_raw::store_get_fs_closure(1);918919 Ok(())920}921922// pub struct GcAlloc;923// unsafe impl GlobalAlloc for GcAlloc {924// unsafe fn alloc(&self, l: Layout) -> *mut u8 {925// let ptr = unsafe { GC_malloc(l.size()) };926// ptr.cast()927// }928// unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {929// // unsafe { GC_free(ptr.cast()) };930// }931//932// unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {933// let ptr = unsafe { GC_realloc(ptr.cast(), new_size) };934// ptr.cast()935// }936// }937//938// #[global_allocator]939// static GC: GcAlloc = GcAlloc;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) -> Result<Self> {733 let a_fields = self.list_fields()?;734 let b_fields = other.list_fields()?;735 match (a_fields.len(), b_fields.len()) {736 (_, 0) => return Ok(self),737 (0, _) => return Ok(other),738 _ => {}739 }740 let mut out = HashMap::new();741 for f in a_fields.iter() {742 if b_fields.contains(f) {743 break;744 }745 out.insert(f.as_str(), self.get_field(f)?);746 }747 if out.is_empty() {748 // All fields from lhs are overriden by rhs749 return Ok(other);750 }751 for f in b_fields.iter() {752 out.insert(f.as_str(), other.get_field(f)?);753 }754 Ok(Self::new_attrs(out))755 }756 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {757 if !matches!(self.type_of(), NixType::Attrs) {758 bail!("invalid type: expected attrs");759 }760761 name.as_field_name(|name| {762 with_default_context(|c, es| unsafe {763 get_attr_byname(c, self.0, es, name.as_ptr().cast())764 })765 .map(Self)766 })767 .with_context(|| format!("getting field {:?}", name.to_field_name()))768 }769 pub fn call(&self, v: Value) -> Result<Self> {770 let kind = self771 .functor_kind()772 .ok_or_else(|| anyhow!("can only call function or functor"))?;773774 let function = match kind {775 FunctorKind::Function => self.clone(),776 FunctorKind::Functor => {777 let f = self778 .get_field("__functor")779 .context("getting functor value")?;780 assert_eq!(781 f.type_of(),782 NixType::Function,783 "invalid functor encountered"784 );785 f786 }787 };788789 let out = Value::new_uninit();790 with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;791792 Ok(out)793 }794 pub fn eval(v: &str) -> Result<Self> {795 let s = CString::new(v).expect("expression shouldn't have internal NULs");796 let out = Self::new_uninit();797 with_default_context(|c, es| unsafe {798 expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)799 })?;800 Ok(out)801 }802 pub fn build(&self, output: &str) -> Result<PathBuf> {803 if !self.is_derivation() {804 bail!("expected derivation to build")805 }806 let output_name = self807 .get_field("outputName")808 .context("getting output name field")?809 .to_string()?;810 let v = if output_name != output {811 let out = self.get_field(output).context("getting target output")?;812 if !out.is_derivation() {813 bail!("unknown output: {output}");814 }815 out816 } else {817 self.clone()818 };819 // to_string here blocks until the path is built820 let s = v.builtin_to_string()?;821 let rs = s.to_realised_string()?;822 let drv_path = rs.as_str().to_owned();823 Ok(PathBuf::from(drv_path))824 }825 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {826 let to_json = Self::eval("builtins.toJSON")?;827 let s = to_json.call(self.clone())?.to_string()?;828 Ok(serde_json::from_str(&s)?)829 }830 pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {831 Self::eval(&nixlike::serialize(v)?)832 }833834 // Convert to string/evaluate derivations/etc835 // fn to_string_weak(&self) -> Result<String> {836 // // TODO: For now, it works exactly like to_string, see the comment for fn force()837 // self.to_string()838 // }839840 fn is_derivation(&self) -> bool {841 if !matches!(self.type_of(), NixType::Attrs) {842 return false;843 }844 let Some(ty) = self.get_field("type").ok() else {845 return false;846 };847 matches!(ty.to_string().as_deref(), Ok("derivation"))848 }849 fn functor_kind(&self) -> Option<FunctorKind> {850 match self.type_of() {851 NixType::Attrs => self852 .has_field("__functor")853 .expect("has_field shouldn't fail for attrs")854 .then_some(FunctorKind::Functor),855 NixType::Function => Some(FunctorKind::Function),856 _ => None,857 }858 }859 pub fn is_function(&self) -> bool {860 self.functor_kind().is_some()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;