Skip to main content

Context

Struct Context 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_verbosity(&self, level: Verbosity) -> Result<()>

Set the verbosity level for Nix log output.

Note: Nix’s verbosity is process-global; the &self receiver is for ergonomic API consistency. Calling on any Context affects every other context in the same process.

§Errors

Returns an error if the verbosity level cannot be set.

Source

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.

Source

pub fn init_plugins(&self) -> Result<()>

Load Nix’s configured plugins.

Reads the plugin-files setting and loads each entry. Must be called before any EvalState is built for plugins to be visible.

§Errors

Returns an error if plugin loading fails.

Source

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.

Source

pub fn gc_now()

Run a Nix GC cycle now.

Useful in tests and long-running workers that hold onto large value graphs. Cheap to call repeatedly; Nix’s GC is incremental.

Trait Implementations§

Source§

impl Drop for Context

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Context

Auto Trait Implementations§

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> 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, 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.