1use 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::mem::forget;6use std::ptr::null_mut;7use std::sync::LazyLock;8use std::{collections::HashMap, path::PathBuf};9use std::{fmt, slice};1011use anyhow::{Context, anyhow, bail};12use serde::Serialize;13use serde::de::DeserializeOwned;1415pub use anyhow::Result;16use tracing::{info, instrument};1718use self::logging::nix_logging_cxx;19use self::nix_cxx::set_fetcher_setting;20use self::nix_raw::{21 BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,22 GC_allow_register_threads, GC_free, GC_get_stack_base, GC_init, GC_malloc, GC_realloc,23 GC_register_my_thread, GC_stack_base, GC_thread_is_registered, GC_unregister_my_thread,24 Store as c_store, alloc_value, bindings_builder_free, bindings_builder_insert, c_context,25 c_context_create, c_context_free, clear_err, err_code, err_info_msg, err_msg, eval_state_build,26 eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,27 expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,28 flake_lock, flake_lock_flags, 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, get_attr_byname, get_attr_name_byidx, get_attrs_size, get_list_byidx,33 get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int, init_string,34 libexpr_init, libstore_init, libutil_init, locked_flake, locked_flake_free,35 locked_flake_get_output_attrs, make_attrs, make_bindings_builder, realised_string,36 realised_string_free, 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_path_name, string_realise, value, value_call, value_decref,39 value_incref,40};414243pub 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 { nix_raw::gc_now() };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 197 198 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 237 #[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 297 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 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 {}402403struct EvalState(*mut c_eval_state);404unsafe impl Send for EvalState {}405unsafe impl Sync for EvalState {}406407impl Drop for EvalState {408 fn drop(&mut self) {409 unsafe {410 state_free(self.0);411 }412 }413}414415pub struct FlakeReference(*mut flake_reference);416impl FlakeReference {417 #[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]418 pub fn new(419 s: &str,420 flake: &FlakeSettings,421 parse: &FlakeReferenceParseFlags,422 fetch: &FetchSettings,423 ) -> Result<(Self, String)> {424 let mut out = null_mut();425 let mut fragment = String::new();426 427 with_default_context(|c, _| unsafe {428 flake_reference_and_fragment_from_string(429 c,430 fetch.0,431 flake.0,432 parse.0,433 s.as_ptr().cast(),434 s.len(),435 &mut out,436 Some(copy_nix_str),437 (&raw mut fragment).cast(),438 )439 })?;440 assert!(!out.is_null());441442 Ok((Self(out), fragment))443 }444 #[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]445 pub fn lock(446 &mut self,447 fetch: &FetchSettings,448 flake: &FlakeSettings,449 lock: &FlakeLockFlags,450 ) -> Result<LockedFlake> {451 with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })452 .map(LockedFlake)453 }454}455unsafe impl Send for FlakeReference {}456unsafe impl Sync for FlakeReference {}457458pub struct LockedFlake(*mut locked_flake);459impl LockedFlake {460 pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {461 with_default_context(|c, es| unsafe {462 locked_flake_get_output_attrs(c, settings.0, es, self.0)463 })464 .map(Value)465 }466}467unsafe impl Send for LockedFlake {}468unsafe impl Sync for LockedFlake {}469impl Drop for LockedFlake {470 fn drop(&mut self) {471 unsafe {472 locked_flake_free(self.0);473 };474 }475}476477type FieldName = [u8; 64];478fn init_field_name(v: &str) -> FieldName {479 let mut f = [0; 64];480 assert!(v.len() < 64, "max field name is 63 chars");481 assert!(482 v.bytes().all(|v| v != 0),483 "nul bytes are unsupported in field name"484 );485 f[0..v.len()].copy_from_slice(v.as_bytes());486 f487}488489pub struct RealisedString(*mut realised_string);490impl fmt::Debug for RealisedString {491 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {492 self.as_str().fmt(f)493 }494}495496impl RealisedString {497 pub fn as_str(&self) -> &str {498 let len = unsafe { realised_string_get_buffer_size(self.0) };499 let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();500 let data = unsafe { slice::from_raw_parts(data, len) };501 std::str::from_utf8(data).expect("non-utf8 strings not supported")502 }503 pub fn path_count(&self) -> usize {504 unsafe { realised_string_get_store_path_count(self.0) }505 }506 pub fn path(&self, i: usize) -> String {507 assert!(i < self.path_count());508 let path = unsafe { realised_string_get_store_path(self.0, i) };509 let mut err_out = String::new();510 unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };511 err_out512 }513}514515unsafe impl Send for RealisedString {}516impl Drop for RealisedString {517 fn drop(&mut self) {518 unsafe { realised_string_free(self.0) }519 }520}521522pub struct Value(*mut value);523524unsafe impl Send for Value {}525unsafe impl Sync for Value {}526527pub trait AsFieldName {528 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;529 fn to_field_name(&self) -> Result<String>;530}531impl AsFieldName for Value {532 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {533 let f = self.to_string()?;534 v(init_field_name(&f))535 }536 fn to_field_name(&self) -> Result<String> {537 self.to_string()538 }539}540impl<E> AsFieldName for E541where542 E: AsRef<str>,543{544 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {545 let f = self.as_ref();546 v(init_field_name(f))547 }548 fn to_field_name(&self) -> Result<String> {549 Ok(self.as_ref().to_owned())550 }551}552553struct AttrsBuilder(*mut c_bindings_builder);554impl AttrsBuilder {555 fn new(capacity: usize) -> Self {556 with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })557 .map(Self)558 .expect("alloc should not fail")559 }560 fn insert(&mut self, k: &impl AsFieldName, v: Value) {561 k.as_field_name(|name| {562 with_default_context(|c, _| unsafe {563 bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);564 565 })566 })567 .expect("builder insert shouldn't fail");568 }569}570impl Drop for AttrsBuilder {571 fn drop(&mut self) {572 unsafe { bindings_builder_free(self.0) };573 }574}575576impl Value {577 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {578 let out = Self::new_uninit();579 let mut b = AttrsBuilder::new(v.len());580 for (k, v) in v {581 b.insert(&k, v);582 }583 with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })584 .expect("attrs initialization should not fail");585586 out587 }588 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {589 todo!()590 }591 fn new_uninit() -> Self {592 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })593 .expect("value allocation should not fail");594 Self(out)595 }596 pub fn new_str(v: &str) -> Self {597 let s = CString::new(v).expect("string should not contain NULs");598 let out = Self::new_uninit();599 600 with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })601 .expect("string initialization should not fail");602 out603 }604 pub fn new_int(i: i64) -> Self {605 let out = Self::new_uninit();606 with_default_context(|c, _| unsafe { init_int(c, out.0, i) })607 .expect("int initialization should not fail");608 out609 }610 pub fn new_bool(v: bool) -> Self {611 let out = Self::new_uninit();612 with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })613 .expect("bool initialization should not fail");614 out615 }616 617 618 619 620 621 pub fn type_of(&self) -> NixType {622 let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })623 .expect("get_type should not fail");624 NixType::from_int(ty)625 }626 fn builtin_to_string(&self) -> Result<Self> {627 let builtin = Self::eval("builtins.toString")?;628 builtin.call(self.clone())629 }630 pub fn to_string(&self) -> Result<String> {631 let mut str_out = String::new();632 with_default_context(|c, _| unsafe {633 get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())634 })?;635636 Ok(str_out)637 }638 pub fn to_realised_string(&self) -> Result<RealisedString> {639 with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })640 .map(RealisedString)641642 643 644 645 646 647 648 649 }650651 pub fn has_field(&self, field: &str) -> Result<bool> {652 let f = init_field_name(field);653 with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })654 }655 656 657 658 pub fn list_fields(&self) -> Result<Vec<String>> {659 if !matches!(self.type_of(), NixType::Attrs) {660 bail!("invalid type: expected attrs");661 }662663 let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;664 let mut out = Vec::with_capacity(len as usize);665666 for i in 0..len {667 let name =668 with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;669 let c = unsafe { CStr::from_ptr(name) };670 out.push(c.to_str().expect("nix field names are utf-8").to_owned());671 }672 Ok(out)673 }674 pub fn get_elem(&self, v: usize) -> Result<Self> {675 if !matches!(self.type_of(), NixType::List) {676 bail!("invalid type: expected list");677 }678 let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;679 if v >= len {680 bail!("oob list get: {v} >= {len}");681 }682683 with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)684 }685 pub fn attrs_update(self, other: Value) -> Result<Self> {686 let a_fields = self.list_fields()?;687 let b_fields = other.list_fields()?;688 match (a_fields.len(), b_fields.len()) {689 (_, 0) => return Ok(self),690 (0, _) => return Ok(other),691 _ => {}692 }693 let mut out = HashMap::new();694 for f in a_fields.iter() {695 if b_fields.contains(f) {696 break;697 }698 out.insert(f.as_str(), self.get_field(f)?);699 }700 if out.is_empty() {701 702 return Ok(other);703 }704 for f in b_fields.iter() {705 out.insert(f.as_str(), other.get_field(f)?);706 }707 Ok(Self::new_attrs(out))708 }709 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {710 if !matches!(self.type_of(), NixType::Attrs) {711 bail!("invalid type: expected attrs");712 }713714 name.as_field_name(|name| {715 with_default_context(|c, es| unsafe {716 get_attr_byname(c, self.0, es, name.as_ptr().cast())717 })718 .map(Self)719 })720 .with_context(|| format!("getting field {:?}", name.to_field_name()))721 }722 pub fn call(&self, v: Value) -> Result<Self> {723 let kind = self724 .functor_kind()725 .ok_or_else(|| anyhow!("can only call function or functor"))?;726727 let function = match kind {728 FunctorKind::Function => self.clone(),729 FunctorKind::Functor => {730 let f = self.get_field("__functor")?;731 assert_eq!(732 f.type_of(),733 NixType::Function,734 "invalid functor encountered"735 );736 f737 }738 };739740 let out = Value::new_uninit();741 with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;742743 Ok(out)744 }745 pub fn eval(v: &str) -> Result<Self> {746 let s = CString::new(v).expect("expression shouldn't have internal NULs");747 let out = Self::new_uninit();748 with_default_context(|c, es| unsafe {749 expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)750 })?;751 Ok(out)752 }753 pub fn build(&self, output: &str) -> Result<PathBuf> {754 if !self.is_derivation() {755 bail!("expected derivation to build")756 }757 let output_name = self.get_field("outputName")?.to_string()?;758 let v = if output_name != output {759 let out = self.get_field(output)?;760 if !out.is_derivation() {761 bail!("unknown output: {output}");762 }763 out764 } else {765 self.clone()766 };767 768 let s = v.builtin_to_string()?;769 let rs = s.to_realised_string()?;770 let drv_path = rs.as_str().to_owned();771 Ok(PathBuf::from(drv_path))772 }773 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {774 let to_json = Self::eval("builtins.toJSON")?;775 let s = to_json.call(self.clone())?.to_string()?;776 Ok(serde_json::from_str(&s)?)777 }778 pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {779 Self::eval(&nixlike::serialize(v)?)780 }781782 783 784 785 786 787788 fn is_derivation(&self) -> bool {789 if !matches!(self.type_of(), NixType::Attrs) {790 return false;791 }792 let Some(ty) = self.get_field("type").ok() else {793 return false;794 };795 matches!(ty.to_string().as_deref(), Ok("derivation"))796 }797 fn functor_kind(&self) -> Option<FunctorKind> {798 match self.type_of() {799 NixType::Attrs => self800 .has_field("__functor")801 .expect("has_field shouldn't fail for attrs")802 .then_some(FunctorKind::Functor),803 NixType::Function => Some(FunctorKind::Function),804 _ => None,805 }806 }807 pub fn is_function(&self) -> bool {808 self.functor_kind().is_some()809 }810}811812impl From<String> for Value {813 fn from(value: String) -> Self {814 Value::new_str(&value)815 }816}817impl From<bool> for Value {818 fn from(value: bool) -> Self {819 Value::new_bool(value)820 }821}822impl From<&str> for Value {823 fn from(value: &str) -> Self {824 Value::new_str(value)825 }826}827impl<T> From<Vec<T>> for Value828where829 T: Into<Value>,830{831 fn from(value: Vec<T>) -> Self {832 Value::new_list(value)833 }834}835836impl Clone for Value {837 fn clone(&self) -> Self {838 with_default_context(|c, _| unsafe { value_incref(c, self.0) })839 .expect("value incref should not fail");840 Self(self.0)841 }842}843impl Drop for Value {844 fn drop(&mut self) {845 with_default_context(|c, _| unsafe { value_decref(c, self.0) })846 .expect("value drop should not fail");847 }848}849850pub fn init_libraries() {851 unsafe { GC_allow_register_threads() };852853 let mut ctx = NixContext::new();854 ctx.run_in_context(|c| unsafe { libutil_init(c) })855 .expect("util init should not fail");856 ctx.run_in_context(|c| unsafe { libstore_init(c) })857 .expect("store init should not fail");858 ctx.run_in_context(|c| unsafe { libexpr_init(c) })859 .expect("expr init should not fail");860861 nix_logging_cxx::apply_tracing_logger();862}863864#[test_log::test]865fn test_native() -> Result<()> {866 init_libraries();867868 let mut fetch_settings = FetchSettings::new();869 fetch_settings.set(c"warn-dirty", c"false");870871 let manifest = format!("{}/../../", env!("CARGO_MANIFEST_DIR"));872 let flake = FlakeSettings::new()?;873 let parse = FlakeReferenceParseFlags::new(&flake)?;874 let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;875 let lock = FlakeLockFlags::new(&flake)?;876 let locked = r.lock(&fetch_settings, &flake, &lock)?;877 let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;878879 let builtins = Value::eval("builtins")?;880 assert_eq!(builtins.type_of(), NixType::Attrs);881882 assert_eq!(attrs.type_of(), NixType::Attrs);883 let test_data = nix_go!(attrs.testData);884885 let test_string: String = nix_go_json!(test_data.testString);886 assert_eq!(test_string, "hello");887888 Ok(())889}890891892893894895896897898899900901902903904905906907908