Skip to content

Commit ccd2608

Browse files
committed
treewide: add rustdoc to platform-specific modules
1 parent 32dffaa commit ccd2608

File tree

4 files changed

+173
-4
lines changed

4 files changed

+173
-4
lines changed

src/darwin.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ use crate::util::get_hostname;
99
use crate::util::platform;
1010
use crate::Result;
1111

12+
/// System profile path for darwin configurations
1213
const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
14+
/// Current system profile path for darwin
1315
const CURRENT_PROFILE: &str = "/run/current-system";
1416

1517
impl DarwinArgs {
18+
/// Entry point for processing darwin commands
19+
///
20+
/// Handles the different subcommands for darwin configurations:
21+
/// - Switch: Builds and activates the configuration
22+
/// - Build: Only builds the configuration
23+
/// - Repl: Opens a REPL for exploring the configuration
1624
pub fn run(self) -> Result<()> {
1725
use DarwinRebuildVariant::{Build, Switch};
1826
match self.subcommand {
@@ -28,12 +36,25 @@ impl DarwinArgs {
2836
}
2937
}
3038

39+
/// Variants of the darwin rebuild operation
40+
///
41+
/// Each variant represents a different mode of operation:
42+
/// - Switch: Build and activate the configuration
43+
/// - Build: Only build the configuration without activation
3144
enum DarwinRebuildVariant {
3245
Switch,
3346
Build,
3447
}
3548

3649
impl DarwinRebuildArgs {
50+
/// Performs the rebuild operation for darwin configurations
51+
///
52+
/// This function handles building and potentially activating darwin configurations.
53+
/// It first builds the configuration, then shows a diff of changes compared to the
54+
/// current system, and finally activates the configuration if needed.
55+
///
56+
/// The darwin activation process is unique and requires special handling compared
57+
/// to `NixOS`, particularly around determining whether root privileges are needed.
3758
fn rebuild(self, variant: DarwinRebuildVariant) -> Result<()> {
3859
use DarwinRebuildVariant::{Build, Switch};
3960

@@ -117,6 +138,10 @@ impl DarwinRebuildArgs {
117138
}
118139

119140
impl DarwinReplArgs {
141+
/// Opens a Nix REPL for exploring darwin configurations
142+
///
143+
/// Provides an interactive environment to explore and evaluate
144+
/// components of a darwin configuration.
120145
fn run(self) -> Result<()> {
121146
if let Installable::Store { .. } = self.installable {
122147
bail!("Nix doesn't support nix store installables.");

src/home.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ use crate::update::update;
1010
use crate::util::platform;
1111

1212
impl interface::HomeArgs {
13+
/// Entry point for processing home-manager commands
14+
///
15+
/// Handles the different subcommands for home-manager configurations:
16+
/// - Switch: Builds and activates the configuration
17+
/// - Build: Only builds the configuration
18+
/// - Repl: Opens a REPL for exploring the configuration
1319
pub fn run(self) -> Result<()> {
1420
use HomeRebuildVariant::{Build, Switch};
1521
match self.subcommand {
@@ -25,13 +31,30 @@ impl interface::HomeArgs {
2531
}
2632
}
2733

34+
/// Variants of the home-manager rebuild operation
35+
///
36+
/// Represents different actions that can be taken with a home-manager configuration:
37+
/// - Build: Only build the configuration without activating it
38+
/// - Switch: Build and activate the configuration
2839
#[derive(Debug)]
2940
enum HomeRebuildVariant {
3041
Build,
3142
Switch,
3243
}
3344

3445
impl HomeRebuildArgs {
46+
/// Performs the rebuild operation for home-manager configurations
47+
///
48+
/// This function handles building and potentially activating home-manager configurations.
49+
/// The workflow:
50+
/// 1. Updates the flake if requested
51+
/// 2. Creates a temporary output path
52+
/// 3. Builds the configuration with proper specialisation handling
53+
/// 4. Compares with the previous generation if it exists
54+
/// 5. Activates the configuration if needed
55+
///
56+
/// Home Manager has its own specialisation mechanism which this function handles
57+
/// by looking in ~/.local/share/home-manager/specialisation.
3558
fn rebuild(self, variant: HomeRebuildVariant) -> Result<()> {
3659
use HomeRebuildVariant::Build;
3760

@@ -138,6 +161,11 @@ impl HomeRebuildArgs {
138161
}
139162

140163
impl HomeReplArgs {
164+
/// Opens a Nix REPL for exploring home-manager configurations
165+
///
166+
/// Provides an interactive environment to explore and evaluate
167+
/// components of a home-manager configuration. This is useful for
168+
/// debugging or exploring available options.
141169
fn run(self) -> Result<()> {
142170
// Use NH_HOME_FLAKE if available, otherwise use the provided installable
143171
let installable = platform::resolve_env_installable("NH_HOME_FLAKE", self.installable);

src/nixos.rs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ use crate::util::ensure_ssh_key_login;
1515
use crate::util::get_hostname;
1616
use crate::util::platform;
1717

18+
/// Path to the system profile on `NixOS`
1819
const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
20+
/// Path to the current system profile on `NixOS`
1921
const CURRENT_PROFILE: &str = "/run/current-system";
20-
22+
/// Path where `NixOS` stores specialisation information
2123
const SPEC_LOCATION: &str = "/etc/specialisation";
2224

2325
impl interface::OsArgs {
26+
/// Entry point for processing `NixOS` commands
27+
///
28+
/// Handles the various subcommands for `NixOS` configurations:
29+
/// - Switch: Builds, activates, and makes the configuration the boot default
30+
/// - Boot: Builds and makes the configuration the boot default
31+
/// - Test: Builds and activates the configuration
32+
/// - Build: Only builds the configuration
33+
/// - Repl: Opens a REPL for exploring the configuration
34+
/// - Info: Lists available generations
35+
/// - Rollback: Reverts to a previous generation
36+
/// - `BuildVm`: Builds a `NixOS` VM image
2437
pub fn run(self) -> Result<()> {
2538
use OsRebuildVariant::{Boot, Build, Switch, Test};
2639
// Always resolve installable from env var at the top
@@ -67,6 +80,15 @@ impl interface::OsArgs {
6780
}
6881
}
6982

83+
/// Variants of the `NixOS` rebuild operation
84+
///
85+
/// Each variant represents a different mode of operation with distinct
86+
/// activation behaviors:
87+
/// - Build: Only build the configuration
88+
/// - Switch: Build, activate, and make it the boot default
89+
/// - Boot: Build and make it the boot default
90+
/// - Test: Build and activate
91+
/// - `BuildVm`: Build a VM image for testing
7092
#[derive(Debug)]
7193
enum OsRebuildVariant {
7294
Build,
@@ -77,7 +99,25 @@ enum OsRebuildVariant {
7799
}
78100

79101
impl OsRebuildArgs {
80-
// Accept the resolved installable as a parameter
102+
/// Rebuilds a `NixOS` configuration with the given variant and installable
103+
///
104+
/// This is the core function for building and deploying `NixOS` configurations.
105+
/// It handles:
106+
/// 1. SSH key login for remote operations
107+
/// 2. Root privilege management
108+
/// 3. Flake updates if requested
109+
/// 4. Hostname resolution and validation
110+
/// 5. Building the configuration
111+
/// 6. Specialisation handling
112+
/// 7. Remote deployment if `target_host` is specified
113+
/// 8. Configuration activation based on variant
114+
///
115+
/// The different variants determine which aspects of deployment are executed:
116+
/// - Build: Only build the configuration
117+
/// - Switch: Build, activate, and make boot default
118+
/// - Boot: Build and make boot default
119+
/// - Test: Build and activate
120+
/// - `BuildVm`: Build a VM image
81121
fn rebuild_with_installable(
82122
self,
83123
variant: OsRebuildVariant,
@@ -196,6 +236,16 @@ impl OsRebuildArgs {
196236
}
197237

198238
impl OsRollbackArgs {
239+
/// Rolls back the system to a previous generation
240+
///
241+
/// This function:
242+
/// 1. Finds the generation to roll back to (previous or specified)
243+
/// 2. Shows a diff between current and target generations
244+
/// 3. Sets the system profile to point to the target generation
245+
/// 4. Activates the configuration
246+
/// 5. Handles failures by rolling back the profile symlink if activation fails
247+
///
248+
/// Generation specialisations are properly handled during rollback.
199249
fn rollback(&self) -> Result<()> {
200250
// Check if we need root permissions
201251
let elevate = platform::check_not_root(self.bypass_root_check)?;
@@ -311,6 +361,15 @@ impl OsRollbackArgs {
311361
}
312362
}
313363

364+
/// Finds the previous generation in the system profile
365+
///
366+
/// This function:
367+
/// 1. Searches for available system generations
368+
/// 2. Identifies which one is currently active
369+
/// 3. Returns the generation immediately before the current one
370+
///
371+
/// Returns an error if there are no generations or if the current
372+
/// generation is already the oldest one.
314373
fn find_previous_generation() -> Result<generations::GenerationInfo> {
315374
let profile_path = PathBuf::from(SYSTEM_PROFILE);
316375

@@ -357,6 +416,10 @@ fn find_previous_generation() -> Result<generations::GenerationInfo> {
357416
Ok(generations[current_idx - 1].clone())
358417
}
359418

419+
/// Finds a specific generation by its number
420+
///
421+
/// Searches the system profiles directory for a generation with the
422+
/// specified number and returns its information if found.
360423
fn find_generation_by_number(number: u64) -> Result<generations::GenerationInfo> {
361424
let profile_path = PathBuf::from(SYSTEM_PROFILE);
362425

@@ -388,6 +451,10 @@ fn find_generation_by_number(number: u64) -> Result<generations::GenerationInfo>
388451
Ok(generations[0].clone())
389452
}
390453

454+
/// Gets the number of the currently active generation
455+
///
456+
/// This is useful for rollback operations, especially when needing
457+
/// to restore the system if activation of an older generation fails.
391458
fn get_current_generation_number() -> Result<u64> {
392459
let profile_path = PathBuf::from(SYSTEM_PROFILE);
393460

@@ -414,6 +481,13 @@ fn get_current_generation_number() -> Result<u64> {
414481
.map_err(|_| eyre!("Invalid generation number"))
415482
}
416483

484+
/// Determines the final attribute name for VM builds
485+
///
486+
/// Returns the appropriate Nix attribute based on whether
487+
/// the VM should include a bootloader:
488+
/// - "vmWithBootLoader" for VM with bootloader
489+
/// - "vm" for standard VM
490+
/// - "toplevel" for regular builds
417491
pub fn get_final_attr(build_vm: bool, with_bootloader: bool) -> String {
418492
let attr = if build_vm && with_bootloader {
419493
"vmWithBootLoader"
@@ -426,6 +500,11 @@ pub fn get_final_attr(build_vm: bool, with_bootloader: bool) -> String {
426500
}
427501

428502
impl OsReplArgs {
503+
/// Opens a Nix REPL for exploring `NixOS` configurations
504+
///
505+
/// Provides an interactive environment to explore and evaluate
506+
/// components of a `NixOS` configuration. This is useful for
507+
/// debugging or exploring available options.
429508
fn run_with_installable(self, installable: Installable) -> Result<()> {
430509
// Get hostname, with fallback to system hostname
431510
let hostname = match self.hostname {
@@ -454,6 +533,13 @@ impl OsReplArgs {
454533
}
455534

456535
impl OsGenerationsArgs {
536+
/// Lists information about available `NixOS` generations
537+
///
538+
/// This function:
539+
/// 1. Identifies the profile and confirms it exists
540+
/// 2. Finds all generations associated with that profile
541+
/// 3. Collects metadata about each generation (number, date, etc.)
542+
/// 4. Displays the information in a formatted list
457543
fn info(&self) -> Result<()> {
458544
let profile = match self.profile {
459545
Some(ref p) => PathBuf::from(p),

src/util/platform.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,37 @@ pub fn process_specialisation(
337337
}
338338

339339
/// Execute common actions for a rebuild operation across platforms
340-
/// This unifies the core workflow that was previously duplicated across platforms
340+
///
341+
/// This function handles the core workflow for building and managing system
342+
/// configurations across different platforms (`NixOS`, Darwin, Home Manager).
343+
/// It unifies what would otherwise be duplicated across platform-specific modules.
344+
///
345+
/// The function takes care of:
346+
/// 1. Properly configuring the attribute path based on platform type
347+
/// 2. Building the configuration
348+
/// 3. Handling specialisations where applicable
349+
/// 4. Comparing the new configuration with the current one
350+
///
351+
/// # Arguments
352+
///
353+
/// * `installable` - The Nix installable representing the configuration
354+
/// * `config_type` - The configuration type (e.g., "nixosConfigurations", "darwinConfigurations")
355+
/// * `extra_path` - Additional path elements for the attribute path
356+
/// * `config_name` - Optional hostname or configuration name
357+
/// * `out_path` - Output path for the build result
358+
/// * `extra_args` - Additional arguments to pass to the build command
359+
/// * `builder` - Optional remote builder to use
360+
/// * `message` - Message to display during the build process
361+
/// * `no_nom` - Whether to disable nix-output-monitor
362+
/// * `specialisation_path` - Path to read specialisations from
363+
/// * `no_specialisation` - Whether to ignore specialisations
364+
/// * `specialisation` - Optional explicit specialisation to use
365+
/// * `current_profile` - Path to the current system profile for comparison
366+
/// * `skip_compare` - Whether to skip comparing the new and current configuration
367+
///
368+
/// # Returns
369+
///
370+
/// The path to the built configuration, which can be used for activation
341371
pub fn handle_rebuild_workflow(
342372
installable: Installable,
343373
config_type: &str,
@@ -379,7 +409,7 @@ pub fn handle_rebuild_workflow(
379409
{
380410
// All darwin configurations expose their outputs under system.build
381411
let toplevel_path = ["config", "system", "build"];
382-
attribute.extend(toplevel_path.iter().map(|s| s.to_string()));
412+
attribute.extend(toplevel_path.iter().map(|s| (*s).to_string()));
383413

384414
// Add the final component (usually "toplevel")
385415
if !extra_path.is_empty() {

0 commit comments

Comments
 (0)