From 802aebf606571874fafe811d29a6b4b93af9e609 Mon Sep 17 00:00:00 2001 From: _cry64 Date: Thu, 19 Mar 2026 03:53:09 +1000 Subject: [PATCH] fix FlakeLockFlags + FlakeSettings --- nixide/src/flake/flake_lock_flags.rs | 164 ++++++++++++++++++++------- nixide/src/flake/flake_settings.rs | 2 +- 2 files changed, 121 insertions(+), 45 deletions(-) diff --git a/nixide/src/flake/flake_lock_flags.rs b/nixide/src/flake/flake_lock_flags.rs index 464cc41..d006fc5 100644 --- a/nixide/src/flake/flake_lock_flags.rs +++ b/nixide/src/flake/flake_lock_flags.rs @@ -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. pub struct FlakeLockFlags { - pub(crate) ptr: *mut raw::flake_lock_flags, + pub(crate) inner: NonNull, } impl Drop for FlakeLockFlags { fn drop(&mut self) { unsafe { - raw::flake_lock_flags_free(self.ptr); + sys::nix_flake_lock_flags_free(self.as_ptr()); } } } impl FlakeLockFlags { - pub fn new(settings: &FlakeSettings) -> Result { - let mut ctx = Context::new(); - let s = unsafe { context::check_call!(raw::flake_lock_flags_new(&mut ctx, settings.ptr)) }?; - Ok(FlakeLockFlags { ptr: s }) + // XXX: TODO: what is the default FlakeLockMode? + pub fn new(settings: &FlakeSettings) -> Result { + ErrorContext::new().and_then(|ctx| { + 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. - pub fn set_mode_write_as_needed(&mut self) -> Result<()> { - let mut ctx = Context::new(); - unsafe { - context::check_call!(raw::flake_lock_flags_set_mode_write_as_needed( - &mut ctx, self.ptr - )) - }?; - Ok(()) - } + // pub fn set_mode_write_as_needed(&mut self) -> Result<(), NixErrorCode> { + // ErrorContext::new().and_then(|ctx| { + // NixErrorCode::from( + // unsafe { + // 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", + // ) + // }) + // } + /// 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<()> { - let mut ctx = Context::new(); - unsafe { context::check_call!(raw::flake_lock_flags_set_mode_check(&mut ctx, self.ptr)) }?; - Ok(()) - } + // pub fn set_mode_check(&mut self) -> Result<(), NixErrorCode> { + // ErrorContext::new().and_then(|ctx| { + // NixErrorCode::from( + // 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. - pub fn set_mode_virtual(&mut self) -> Result<()> { - let mut ctx = Context::new(); - unsafe { - context::check_call!(raw::flake_lock_flags_set_mode_virtual(&mut ctx, self.ptr)) - }?; - 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( + // pub fn set_mode_virtual(&mut self) -> Result<(), NixErrorCode> { + // ErrorContext::new().and_then(|ctx| { + // NixErrorCode::from( + // unsafe { sys::nix_flake_lock_flags_set_mode_virtual(ctx.as_ptr(), self.as_ptr()) }, + // "nix_flake_lock_flags_set_mode_virtual", + // ) + // }) + // } + + /// 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, - override_path: &str, - override_ref: &FlakeReference, - ) -> Result<()> { - let mut ctx = Context::new(); - unsafe { - context::check_call!(raw::flake_lock_flags_add_input_override( - &mut ctx, - self.ptr, - CString::new(override_path) - .context("Failed to create CString for override_path")? - .as_ptr(), - override_ref.ptr.as_ptr() - )) - }?; - Ok(()) + path: &str, + flakeref: &FlakeReference, + ) -> Result<(), NixErrorCode> { + let input_path = NixErrorCode::from_nulerror( + CString::new(path), + "nixide::FlakeLockArgs::override_input", + )?; + + ErrorContext::new().and_then(|ctx| unsafe { + NixErrorCode::from( + unsafe { + sys::nix_flake_lock_flags_add_input_override( + ctx.as_ptr(), + self.as_ptr(), + input_path.as_ptr(), + flakeref.as_ptr(), + ) + }, + "nix_flake_lock_flags_add_input_override", + ) + }) } } diff --git a/nixide/src/flake/flake_settings.rs b/nixide/src/flake/flake_settings.rs index 3cc2082..687b56d 100644 --- a/nixide/src/flake/flake_settings.rs +++ b/nixide/src/flake/flake_settings.rs @@ -19,7 +19,7 @@ impl FlakeSettings { Ok(FlakeSettings { inner }) } - fn add_to_eval_state_builder( + pub(super) fn add_to_eval_state_builder( &self, builder: &mut EvalStateBuilder, ) -> Result<(), NixErrorCode> {