pub struct EvalState { /* private fields */ }Expand description
Nix evaluation state for evaluating expressions.
This provides the main interface for evaluating Nix expressions and creating values.
Implementations§
Source§impl EvalState
impl EvalState
Sourcepub fn eval_from_file(&self, path: impl AsRef<Path>) -> Result<Value<'_>>
pub fn eval_from_file(&self, path: impl AsRef<Path>) -> Result<Value<'_>>
Evaluate a Nix expression from a file.
Reads the file at path as UTF-8, then evaluates its contents using the
parent directory as the base path for relative imports. The base path is
passed to Nix as a UTF-8 string; non-UTF-8 components are replaced
lossily for the error-reporting label only.
§Errors
Returns an error if the file cannot be read as UTF-8 or if evaluation fails.
Sourcepub fn alloc_value(&self) -> Result<Value<'_>>
pub fn alloc_value(&self) -> Result<Value<'_>>
Sourcepub fn make_float(&self, f: f64) -> Result<Value<'_>>
pub fn make_float(&self, f: f64) -> Result<Value<'_>>
Sourcepub fn make_string(&self, s: &str) -> Result<Value<'_>>
pub fn make_string(&self, s: &str) -> Result<Value<'_>>
Create a Nix string value.
§Errors
Returns an error if value allocation, string conversion, or initialization fails.
Sourcepub fn make_path(&self, path: impl AsRef<Path>) -> Result<Value<'_>>
pub fn make_path(&self, path: impl AsRef<Path>) -> Result<Value<'_>>
Create a Nix path value.
§Pure Evaluation
In pure-eval mode (--pure-eval) the Nix evaluator wraps the
filesystem in an AllowListSourceAccessor that rejects any
unregistered absolute path. When the shim feature is enabled this
method automatically registers absolute paths via the shim’s
nix_eval_state_allow_path before constructing the value, mirroring
what Nix’s own fetch builtins do. Without shim you must arrange
for allowPath yourself, or is_pure_eval returns true.
§Errors
Returns an error if value allocation, path conversion, or initialization fails.
Sourcepub fn allow_store_path(&self, path: &str) -> Result<()>
pub fn allow_store_path(&self, path: &str) -> Result<()>
Allow a canonical Nix store path in this evaluation state.
This restores access to a previously resolved store path in a fresh
evaluator running with restrict-eval. It does not permit arbitrary
filesystem paths: Nix parses and validates the supplied store path.
§Errors
Returns an error if path is not a valid store path or Nix cannot add it
to this evaluation state’s allowlist.
Sourcepub fn make_list(&self, items: &[&Value<'_>]) -> Result<Value<'_>>
pub fn make_list(&self, items: &[&Value<'_>]) -> Result<Value<'_>>
Create a Nix list value from a slice of values.
§Errors
Returns an error if value allocation or list construction fails.
Sourcepub fn make_attrs<'s>(
&'s self,
pairs: &[(&str, &Value<'_>)],
) -> Result<Value<'s>>
pub fn make_attrs<'s>( &'s self, pairs: &[(&str, &Value<'_>)], ) -> Result<Value<'s>>
Create a Nix attribute set from key-value pairs.
§Errors
Returns an error if value allocation or attribute set construction fails.
Sourcepub fn get_derivation(&self, value: &Value<'_>) -> Result<Option<StorePath>>
pub fn get_derivation(&self, value: &Value<'_>) -> Result<Option<StorePath>>
Determine whether a value is a derivation and return its store path.
Forces value and, if it is a derivation, returns a newly allocated
StorePath for its .drvPath. Returns Ok(None) when the value is
not a derivation. An exception raised during forcing propagates as
Err(...).
§Errors
Returns an error if forcing the value raises a Nix exception.
Sourcepub fn auto_call_function<'s>(
&'s self,
auto_args: Option<&Value<'_>>,
fn_val: &Value<'_>,
) -> Result<Value<'s>>
pub fn auto_call_function<'s>( &'s self, auto_args: Option<&Value<'_>>, fn_val: &Value<'_>, ) -> Result<Value<'s>>
Call a function using an attribute set as its argument source.
Forces fn_val and writes the result into a newly allocated value:
- If
fn_valis a function with named formals, each formal is looked up inauto_args; formals with defaults that are absent fromauto_argsuse their defaults. - If
auto_argsisNone, empty bindings are supplied (every formal must then have a default). - If
fn_valis not a function, the value is copied to the result unchanged.
§Errors
Returns an error if the call fails.
Source§impl EvalState
impl EvalState
Sourcepub fn make_external<T: NixExternal>(
&self,
data: T,
) -> Result<ExternalValueHandle<'_>>
pub fn make_external<T: NixExternal>( &self, data: T, ) -> Result<ExternalValueHandle<'_>>
Wrap a NixExternal value and return an ExternalValueHandle.
The handle carries the Nix Value and the raw ExternalValue* needed
for downcasting via ExternalValueHandle::as_external. The value’s
lifetime is managed by the Nix GC.
§Errors
Returns an error if value allocation or external value creation fails.