doc: Basic doc comments for nix-flake

(cherry picked from commit c8f7d9ba5860114c4d602d089c26c05283d312a6)
This commit is contained in:
Robert Hensing 2025-04-09 15:10:49 +02:00
parent 30bcd71527
commit 0514ad3433

View file

@ -10,6 +10,7 @@ use nix_util::{
string_return::{callback_get_result_string, callback_get_result_string_data}, string_return::{callback_get_result_string, callback_get_result_string_data},
}; };
/// Store settings for the flakes feature.
pub struct FlakeSettings { pub struct FlakeSettings {
pub(crate) ptr: *mut raw::flake_settings, pub(crate) ptr: *mut raw::flake_settings,
} }
@ -43,9 +44,11 @@ impl FlakeSettings {
} }
pub trait EvalStateBuilderExt { pub trait EvalStateBuilderExt {
/// Configures the eval state to provide flakes features such as `builtins.getFlake`.
fn flakes(self, settings: &FlakeSettings) -> Result<nix_expr::eval_state::EvalStateBuilder>; fn flakes(self, settings: &FlakeSettings) -> Result<nix_expr::eval_state::EvalStateBuilder>;
} }
impl EvalStateBuilderExt for nix_expr::eval_state::EvalStateBuilder { impl EvalStateBuilderExt for nix_expr::eval_state::EvalStateBuilder {
/// Configures the eval state to provide flakes features such as `builtins.getFlake`.
fn flakes( fn flakes(
mut self, mut self,
settings: &FlakeSettings, settings: &FlakeSettings,
@ -55,6 +58,7 @@ impl EvalStateBuilderExt for nix_expr::eval_state::EvalStateBuilder {
} }
} }
/// Parameters for parsing a flake reference.
pub struct FlakeReferenceParseFlags { pub struct FlakeReferenceParseFlags {
pub(crate) ptr: NonNull<raw::flake_reference_parse_flags>, pub(crate) ptr: NonNull<raw::flake_reference_parse_flags>,
} }
@ -75,6 +79,8 @@ impl FlakeReferenceParseFlags {
.context("flake_reference_parse_flags_new unexpectedly returned null")?; .context("flake_reference_parse_flags_new unexpectedly returned null")?;
Ok(FlakeReferenceParseFlags { ptr }) Ok(FlakeReferenceParseFlags { ptr })
} }
/// Sets the [base directory](https://nix.dev/manual/nix/latest/glossary#gloss-base-directory)
/// for resolving local flake references.
pub fn set_base_directory(&mut self, base_directory: &str) -> Result<()> { pub fn set_base_directory(&mut self, base_directory: &str) -> Result<()> {
let mut ctx = Context::new(); let mut ctx = Context::new();
unsafe { unsafe {
@ -100,6 +106,10 @@ impl Drop for FlakeReference {
} }
} }
impl FlakeReference { impl FlakeReference {
/// Parse a flake reference from a string.
/// The string must be a valid flake reference, such as `github:owner/repo`.
/// It may also be suffixed with a `#` and a fragment, such as `github:owner/repo#something`,
/// in which case, the returned string will contain the fragment.
pub fn parse_with_fragment( pub fn parse_with_fragment(
fetch_settings: &FetchersSettings, fetch_settings: &FetchersSettings,
flake_settings: &FlakeSettings, flake_settings: &FlakeSettings,
@ -129,6 +139,7 @@ impl FlakeReference {
} }
} }
/// Parameters that affect the locking of a flake.
pub struct FlakeLockFlags { pub struct FlakeLockFlags {
pub(crate) ptr: *mut raw::flake_lock_flags, pub(crate) ptr: *mut raw::flake_lock_flags,
} }
@ -145,6 +156,7 @@ impl FlakeLockFlags {
let s = unsafe { context::check_call!(raw::flake_lock_flags_new(&mut ctx, settings.ptr)) }?; let s = unsafe { context::check_call!(raw::flake_lock_flags_new(&mut ctx, settings.ptr)) }?;
Ok(FlakeLockFlags { ptr: s }) Ok(FlakeLockFlags { ptr: s })
} }
/// 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<()> {
let mut ctx = Context::new(); let mut ctx = Context::new();
unsafe { unsafe {
@ -154,11 +166,13 @@ impl FlakeLockFlags {
}?; }?;
Ok(()) Ok(())
} }
/// 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<()> {
let mut ctx = Context::new(); let mut ctx = Context::new();
unsafe { context::check_call!(raw::flake_lock_flags_set_mode_check(&mut ctx, self.ptr)) }?; unsafe { context::check_call!(raw::flake_lock_flags_set_mode_check(&mut ctx, self.ptr)) }?;
Ok(()) Ok(())
} }
/// 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<()> {
let mut ctx = Context::new(); let mut ctx = Context::new();
unsafe { unsafe {
@ -166,6 +180,7 @@ impl FlakeLockFlags {
}?; }?;
Ok(()) 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 add_input_override(
&mut self, &mut self,
override_path: &str, override_path: &str,
@ -219,6 +234,7 @@ impl LockedFlake {
Ok(LockedFlake { ptr }) Ok(LockedFlake { ptr })
} }
/// Returns the outputs of the flake - the result of calling the `outputs` attribute.
pub fn outputs( pub fn outputs(
&self, &self,
flake_settings: &FlakeSettings, flake_settings: &FlakeSettings,