@@ -15,12 +15,25 @@ use crate::util::ensure_ssh_key_login;
1515use crate :: util:: get_hostname;
1616use crate :: util:: platform;
1717
18+ /// Path to the system profile on `NixOS`
1819const SYSTEM_PROFILE : & str = "/nix/var/nix/profiles/system" ;
20+ /// Path to the current system profile on `NixOS`
1921const CURRENT_PROFILE : & str = "/run/current-system" ;
20-
22+ /// Path where `NixOS` stores specialisation information
2123const SPEC_LOCATION : & str = "/etc/specialisation" ;
2224
2325impl 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 ) ]
7193enum OsRebuildVariant {
7294 Build ,
@@ -77,7 +99,25 @@ enum OsRebuildVariant {
7799}
78100
79101impl 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
198238impl 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.
314373fn 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.
360423fn 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.
391458fn 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
417491pub 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
428502impl 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
456535impl 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) ,
0 commit comments