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<'_>
impl Value<'_>
Sourcepub fn get_attr(&self, key: &str) -> Result<Value<'_>>
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.
Sourcepub fn attr_keys(&self) -> Result<Vec<String>>
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§impl Value<'_>
impl Value<'_>
Sourcepub fn is_list(&self) -> bool
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(())
}Sourcepub fn list_get(&self, idx: usize) -> Result<Value<'_>>
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§impl Value<'_>
impl Value<'_>
Sourcepub fn force(&mut self) -> Result<()>
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.
Sourcepub fn force_deep(&mut self) -> Result<()>
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.
Sourcepub fn value_type(&self) -> ValueType
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.
Sourcepub fn type_name(&self) -> String
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.
Sourcepub fn as_int(&self) -> Result<i64>
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.
Sourcepub fn as_float(&self) -> Result<f64>
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.
Sourcepub fn as_bool(&self) -> Result<bool>
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.
Sourcepub fn as_string(&self) -> Result<String>
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.
Sourcepub fn as_path(&self) -> Result<PathBuf>
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.
Sourcepub fn call(&self, arg: &Value<'_>) -> Result<Value<'_>>
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.
Sourcepub fn call_multi(&self, args: &[&Value<'_>]) -> Result<Value<'_>>
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.
Sourcepub fn to_nix_string(&self) -> Result<String>
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.