pub struct Context { /* private fields */ }Expand description
Nix context for managing library state.
This is the root object for all Nix operations. It manages the lifetime of the Nix C API context and provides automatic cleanup.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new Nix context.
This initializes the Nix C API context and the required libraries.
§Errors
Returns an error if context creation or library initialization fails.
Sourcepub fn new_no_load_config() -> Result<Self>
pub fn new_no_load_config() -> Result<Self>
Create a new Nix context without loading user/environment
configuration (nix.conf, NIX_* env vars).
Equivalent to new but uses
nix_libstore_init_no_load_config so the store is initialized in a
deterministic state. Useful in tests and sandboxed callers.
Note: this affects only the first context created in the process;
subsequent calls (including new) hit the one-shot
init gate and reuse whatever mode was selected first.
§Errors
Returns an error if context creation or library initialization fails.
Sourcepub fn set_setting(&self, key: &str, value: &str) -> Result<()>
pub fn set_setting(&self, key: &str, value: &str) -> Result<()>
Set a global Nix configuration setting.
Settings take effect for new EvalState instances.
Use "extra-<name>" to append to an existing setting’s value.
§Errors
Returns Error::KeyNotFound if the setting key is unknown.
Sourcepub fn get_setting(&self, key: &str) -> Result<String>
pub fn get_setting(&self, key: &str) -> Result<String>
Get the value of a global Nix configuration setting.
§Errors
Returns Error::KeyNotFound if the setting key is unknown.
Sourcepub fn set_verbosity(&self, level: Verbosity) -> Result<()>
pub fn set_verbosity(&self, level: Verbosity) -> Result<()>
Sourcepub fn clear_error(&self)
pub fn clear_error(&self)
Clear any error state currently stored on the context.
Nix’s C API parks the last error message and code on the context.
Code paths that signal failure by returning a null pointer (e.g.
Value::get_attr) inspect that buffer to
decide whether the null was a genuine failure or just an absent
value, so a stale message from an earlier unrelated call can confuse
them. Call this between recoverable operations to reset the buffer.
Sourcepub fn init_plugins(&self) -> Result<()>
pub fn init_plugins(&self) -> Result<()>
Sourcepub fn set_log_format(&self, format: &str) -> Result<()>
pub fn set_log_format(&self, format: &str) -> Result<()>
Set the log format Nix uses when writing log messages.
Format strings recognised by Nix include "raw", "internal-json",
"bar", and "bar-with-logs". Setting is process-global, see
set_verbosity.
§Errors
Returns an error if the format is unrecognised or the underlying call fails.