fix FlakeLockFlags + FlakeSettings

This commit is contained in:
do butterflies cry? 2026-03-19 03:53:09 +10:00
parent 3cdfae01b9
commit 802aebf606
Signed by: cry
GPG key ID: F68745A836CA0412
2 changed files with 121 additions and 45 deletions

View file

@ -1,61 +1,137 @@
use std::ffi::CString;
use std::ptr::NonNull;
use super::{FlakeReference, FlakeSettings};
use crate::sys;
use crate::{ErrorContext, NixErrorCode};
#[derive(Debug, Clone)]
pub enum FlakeLockMode {
/// Configures [LockedFlake::lock] to make incremental changes to the lock file as needed. Changes are written to file.
WriteAsNeeded,
/// Like [FlakeLockMode::WriteAsNeeded], but does not write to the lock file.
Virtual,
/// Make [LockedFlake::lock] check if the lock file is up to date. If not, an error is returned.
Check,
}
/// Parameters that affect the locking of a flake. /// Parameters that affect the locking of a flake.
pub struct FlakeLockFlags { pub struct FlakeLockFlags {
pub(crate) ptr: *mut raw::flake_lock_flags, pub(crate) inner: NonNull<sys::nix_flake_lock_flags>,
} }
impl Drop for FlakeLockFlags { impl Drop for FlakeLockFlags {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
raw::flake_lock_flags_free(self.ptr); sys::nix_flake_lock_flags_free(self.as_ptr());
} }
} }
} }
impl FlakeLockFlags { impl FlakeLockFlags {
pub fn new(settings: &FlakeSettings) -> Result<Self> { // XXX: TODO: what is the default FlakeLockMode?
let mut ctx = Context::new(); pub fn new(settings: &FlakeSettings) -> Result<Self, NixErrorCode> {
let s = unsafe { context::check_call!(raw::flake_lock_flags_new(&mut ctx, settings.ptr)) }?; ErrorContext::new().and_then(|ctx| {
Ok(FlakeLockFlags { ptr: s }) NonNull::new(unsafe { sys::nix_flake_lock_flags_new(ctx.as_ptr(), settings.as_ptr()) })
.ok_or(NixErrorCode::NulError {
location: "nix_flake_lock_flags_new",
})
.map(|inner| FlakeLockFlags { inner })
})
} }
pub(crate) fn as_ptr(&self) -> *mut sys::nix_flake_lock_flags {
self.inner.as_ptr()
}
pub fn set_lock_mode(&mut self, mode: &FlakeLockMode) -> Result<(), NixErrorCode> {
ErrorContext::new().and_then(|ctx| unsafe {
NixErrorCode::from(
match mode {
FlakeLockMode::WriteAsNeeded => {
sys::nix_flake_lock_flags_set_mode_write_as_needed(
ctx.as_ptr(),
self.as_ptr(),
)
}
FlakeLockMode::Virtual => {
sys::nix_flake_lock_flags_set_mode_virtual(ctx.as_ptr(), self.as_ptr())
}
FlakeLockMode::Check => {
sys::nix_flake_lock_flags_set_mode_check(ctx.as_ptr(), self.as_ptr())
}
},
"nix_flake_lock_flags_set_mode_check",
)
})
}
/// Configures [LockedFlake::lock] to make incremental changes to the lock file as needed. Changes are written to file. /// Configures [LockedFlake::lock] to make incremental changes to the lock file as needed. Changes are written to file.
pub fn set_mode_write_as_needed(&mut self) -> Result<()> { // pub fn set_mode_write_as_needed(&mut self) -> Result<(), NixErrorCode> {
let mut ctx = Context::new(); // ErrorContext::new().and_then(|ctx| {
unsafe { // NixErrorCode::from(
context::check_call!(raw::flake_lock_flags_set_mode_write_as_needed( // unsafe {
&mut ctx, self.ptr // sys::nix_flake_lock_flags_set_mode_write_as_needed(ctx.as_ptr(), self.as_ptr())
)) // },
}?; // "nix_flake_lock_flags_set_mode_write_as_needed",
Ok(()) // )
} // })
// }
/// Make [LockedFlake::lock] check if the lock file is up to date. If not, an error is returned. /// Make [LockedFlake::lock] check if the lock file is up to date. If not, an error is returned.
pub fn set_mode_check(&mut self) -> Result<()> { // pub fn set_mode_check(&mut self) -> Result<(), NixErrorCode> {
let mut ctx = Context::new(); // ErrorContext::new().and_then(|ctx| {
unsafe { context::check_call!(raw::flake_lock_flags_set_mode_check(&mut ctx, self.ptr)) }?; // NixErrorCode::from(
Ok(()) // unsafe { sys::nix_flake_lock_flags_set_mode_check(ctx.as_ptr(), self.as_ptr()) },
} // "nix_flake_lock_flags_set_mode_check",
// )
// })
// }
/// Like `set_mode_write_as_needed`, but does not write to the lock file. /// Like `set_mode_write_as_needed`, but does not write to the lock file.
pub fn set_mode_virtual(&mut self) -> Result<()> { // pub fn set_mode_virtual(&mut self) -> Result<(), NixErrorCode> {
let mut ctx = Context::new(); // ErrorContext::new().and_then(|ctx| {
unsafe { // NixErrorCode::from(
context::check_call!(raw::flake_lock_flags_set_mode_virtual(&mut ctx, self.ptr)) // unsafe { sys::nix_flake_lock_flags_set_mode_virtual(ctx.as_ptr(), self.as_ptr()) },
}?; // "nix_flake_lock_flags_set_mode_virtual",
Ok(()) // )
} // })
/// Adds an input override to the lock file that will be produced. The [LockedFlake::lock] operation will not write to the lock file. // }
pub fn add_input_override(
/// Adds an input override to the lock file that will be produced.
/// The [LockedFlake::lock] operation will not write to the lock file.
///
/// # Warning
///
/// Calling this function will implicitly set the [FlakeLockMode] to
/// [FlakeLockMode::Virtual] if `self.mode` is not [FlakeLockMode::Check].
///
/// # Arguments
///
/// * `path` - The input name/path to override (must not be empty)
/// * `flake_ref` - The flake reference to use as the override
pub fn override_input(
&mut self, &mut self,
override_path: &str, path: &str,
override_ref: &FlakeReference, flakeref: &FlakeReference,
) -> Result<()> { ) -> Result<(), NixErrorCode> {
let mut ctx = Context::new(); let input_path = NixErrorCode::from_nulerror(
unsafe { CString::new(path),
context::check_call!(raw::flake_lock_flags_add_input_override( "nixide::FlakeLockArgs::override_input",
&mut ctx, )?;
self.ptr,
CString::new(override_path) ErrorContext::new().and_then(|ctx| unsafe {
.context("Failed to create CString for override_path")? NixErrorCode::from(
.as_ptr(), unsafe {
override_ref.ptr.as_ptr() sys::nix_flake_lock_flags_add_input_override(
)) ctx.as_ptr(),
}?; self.as_ptr(),
Ok(()) input_path.as_ptr(),
flakeref.as_ptr(),
)
},
"nix_flake_lock_flags_add_input_override",
)
})
} }
} }

View file

@ -19,7 +19,7 @@ impl FlakeSettings {
Ok(FlakeSettings { inner }) Ok(FlakeSettings { inner })
} }
fn add_to_eval_state_builder( pub(super) fn add_to_eval_state_builder(
&self, &self,
builder: &mut EvalStateBuilder, builder: &mut EvalStateBuilder,
) -> Result<(), NixErrorCode> { ) -> Result<(), NixErrorCode> {