Skip to main content

Value

Struct Value 

Source
pub struct Value<'a> { /* private fields */ }
Expand description

A Nix value.

This represents any value in the Nix language, including primitives, collections, and functions. Values are GC-managed; this struct holds a reference count that is released on drop.

Implementations§

Source§

impl Value<'_>

Source

pub fn get_attr(&self, key: &str) -> Result<Value<'_>>

Get an attribute by name.

Returns the value associated with the given attribute name.

§Errors

Returns an error if the value is not an attribute set or the key does not exist.

Source

pub fn attr_keys(&self) -> Result<Vec<String>>

Get all attribute keys.

Returns a vector of all attribute names in this attribute set.

§Errors

Returns an error if the value is not an attribute set.

Source

pub fn has_attr(&self, key: &str) -> Result<bool>

Check if an attribute exists.

Returns true if the attribute set contains the given key.

§Errors

Returns an error if the value is not an attribute set.

Source

pub fn attrs(&self) -> Result<AttrIterator<'_>>

Create an iterator over key-value pairs.

§Returns

An iterator over all key-value pairs in the attribute set.

§Errors

Returns an error if the value is not an attribute set.

Source§

impl Value<'_>

Source

pub fn is_list(&self) -> bool

Check if this value is a list.

§Example
use std::sync::Arc;

use nix_bindings::{Context, EvalStateBuilder, Store};
fn main() -> Result<(), Box<dyn std::error::Error>> {
  let ctx = Arc::new(Context::new()?);
  let store = Arc::new(Store::open(&ctx, None)?);
  let state = EvalStateBuilder::new(&store)?.build()?;
  let list = state.eval_from_string("[1 2 3]", "<eval>")?;
  assert!(list.is_list());
  Ok(())
}
Source

pub fn list_len(&self) -> Result<usize>

Get the length of this list.

§Errors

Returns an error if this value is not a list.

§Example
let list = state.eval_from_string("[1 2 3]", "<eval>")?;
assert_eq!(list.list_len()?, 3);
Source

pub fn list_get(&self, idx: usize) -> Result<Value<'_>>

Get an element from this list by index.

§Arguments
  • idx - The index of the element to retrieve (0-based)
§Errors

Returns an error if this value is not a list or the index is out of bounds.

§Example
let list = state.eval_from_string("[1 2 3]", "<eval>")?;
let first = list.list_get(0)?;
assert_eq!(first.as_int()?, 1);
Source

pub fn list_iter(&self) -> Result<ListIterator<'_>>

Create an iterator over the elements of this list.

§Errors

Returns an error if this value is not a list.

Source§

impl Value<'_>

Source

pub fn force(&mut self) -> Result<()>

Force evaluation of this value.

If the value is a thunk, this will evaluate it to its final form.

§Errors

Returns an error if evaluation fails.

Source

pub fn force_deep(&mut self) -> Result<()>

Force deep evaluation of this value.

Forces evaluation of the value and all its nested components.

§Errors

Returns an error if evaluation fails.

Source

pub fn value_type(&self) -> ValueType

Get the type of this value.

Does not force; a lazy attribute or list element reports as ValueType::Thunk until forced. Use force or any as_* accessor (which force implicitly) first.

Source

pub fn type_name(&self) -> String

Get the Nix type name of this value as reported by the C API.

Wraps nix_get_typename. Returns Nix’s source-of-truth string (e.g. "int", "thunk", "lambda"). Falls back to the local [ValueType::to_string] if the C call returns null.

Source

pub fn as_int(&self) -> Result<i64>

Convert this value to an integer. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not an integer.

Source

pub fn as_float(&self) -> Result<f64>

Convert this value to a float. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not a float.

Source

pub fn as_bool(&self) -> Result<bool>

Convert this value to a boolean. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not a boolean.

Source

pub fn as_string(&self) -> Result<String>

Convert this value to a string.

Realises any string context. Forces the value first.

§Errors

Returns an error if forcing fails or the value is not a string.

Source

pub fn as_string_with_context(&self) -> Result<(String, Vec<StorePath>)>

Convert this value to a string and return its store-path context.

Extended form of as_string returning both content and any store paths embedded in the string’s context. For ordinary strings the context vector is empty.

§Errors

Returns an error if the value is not a string.

Source

pub fn as_path(&self) -> Result<PathBuf>

Convert this value to a filesystem path. Forces the value first.

§Errors

Returns an error if forcing fails or the value is not a path.

Source

pub fn call(&self, arg: &Value<'_>) -> Result<Value<'_>>

Call this value as a function with a single argument.

§Errors

Returns an error if this value is not a function or the call fails.

Source

pub fn call_multi(&self, args: &[&Value<'_>]) -> Result<Value<'_>>

Call this value as a curried function with multiple arguments.

§Errors

Returns an error if this value is not a function or the call fails.

Source

pub fn make_thunk<'a>( fn_val: &'a Value<'a>, arg: &'a Value<'a>, ) -> Result<Value<'a>>

Create a lazy thunk that applies a function to an argument.

Unlike call, this does not perform the call immediately; it stores it as a thunk. Useful for lazy attribute sets and lists.

§Errors

Returns an error if the thunk cannot be created.

Source

pub fn copy(&self) -> Result<Value<'_>>

Copy this value into a new owned value slot.

§Errors

Returns an error if the copy fails.

Source

pub fn to_nix_string(&self) -> Result<String>

Format this value as Nix syntax.

Forces the value first and recursively renders attribute sets and lists. Functions, thunks, and external values render as opaque placeholders (<lambda>, <thunk>, <external>) the same way nix-instantiate --eval does.

§Errors

Returns an error if forcing fails or any nested value cannot be rendered.

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

Source§

fn clone(&self) -> Self

Clone a Value by incrementing its GC reference count.

Both clones reference the same underlying Nix value; they are not deep copies. Use copy when you need an independent value slot.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Value<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Value<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Value<'a>

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> !Send for Value<'a>

§

impl<'a> !Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnsafeUnpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> NixValueOps for T
where T: NixValueRaw,

Source§

fn value_type(&self) -> ValueType

Return the ValueType of this value. Read more
Source§

fn force(&self) -> Result<()>

Force evaluation (resolves thunks). Read more
Source§

fn as_int(&self) -> Result<i64>

Extract as an integer. Forces the value first. Read more
Source§

fn as_float(&self) -> Result<f64>

Extract as a float. Forces the value first. Read more
Source§

fn as_bool(&self) -> Result<bool>

Extract as a boolean. Forces the value first. Read more
Source§

fn as_string(&self) -> Result<String>

Extract as a UTF-8 string, realising any string context. Forces the value first. Read more
Source§

fn as_path(&self) -> Result<String>

Extract as a filesystem-path string. Forces the value first. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.