Skip to content

Conversation

@NotAShelf
Copy link
Member

@NotAShelf NotAShelf commented Oct 4, 2025

Adds a --run flag to nh os build-vm, that can be used to automatically start the built VM once the build is complete.

Change-Id: I6a6a696460cbcf9422104fbdc062156a5a234227

Summary by CodeRabbit

  • New Features

    • Added a --run (-r) CLI flag to optionally launch the VM immediately after a successful build.
  • Improvements

    • When --run is used, the build will attempt to execute the discovered VM runner and log progress/errors.
    • Post-build and rebuild messages now show the discovered runner path or a fallback instruction for launching the VM.
  • Style

    • Minor formatting tweaks with no behavioral changes.

@coderabbitai
Copy link

coderabbitai bot commented Oct 4, 2025

Walkthrough

Adds a new --run/-r CLI flag to OsBuildVmArgs and uses it to optionally execute the built VM in the NixOS build path. Introduces private helpers to find, print, and run a run-*-vm script; those helpers are duplicated in src/nixos.rs. No other public APIs changed.

Changes

Cohort / File(s) Summary
CLI flag addition
src/interface.rs
Adds pub run: bool with #[arg(long, short = 'r')] to OsBuildVmArgs for optional immediate VM execution.
NixOS build flow changes
src/nixos.rs
OsBuildVmArgs::build_vm now derives out_path, reads should_run from self.run, and conditionally calls run_vm(out_path) after rebuild; OsRebuildArgs::rebuild prints VM instructions for non-dry rebuilds.
VM helper utilities
src/nixos.rs
Adds private helpers find_vm_script(out_path), print_vm_instructions(out_path), and run_vm(out_path) to discover, log, and execute a run-*-vm script under bin/.
Duplicated helpers
src/nixos.rs
The VM helper functions are defined twice within the file, producing duplicate definitions.
Formatting-only changes
src/commands.rs, src/generations.rs
Minor reformatting of multi-line expressions and chained calls; no behavioral changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as User
  participant CLI as OsBuildVmArgs CLI
  participant Build as build_vm
  participant Helpers as VM Helpers
  participant Host as Host Shell

  U->>CLI: invoke build command (optional --run / -r)
  CLI->>Build: build_vm(args)
  Build->>Build: build Nix derivation -> produce out_path
  alt --run provided
    Build->>Helpers: run_vm(out_path)
    Helpers->>Helpers: find_vm_script(out_path)
    alt script found
      Helpers->>Host: exec run-*-vm (spawn)
      Host-->>Helpers: exit status
    else script not found
      Helpers->>CLI: print_vm_instructions(out_path) (fallback)
    end
  else no --run
    Build->>CLI: print_vm_instructions(out_path)
  end
  Build-->>CLI: return
  CLI-->>U: display logs/instructions
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly captures the primary change of adding a --run option to the NixOS VM builder command, accurately reflecting the new functionality without extraneous detail.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch notashelf/push-tznonkqvxxyz

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1641518 and 3c7c158.

📒 Files selected for processing (1)
  • src/nixos.rs (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/nixos.rs (1)
src/commands.rs (3)
  • new (160-171)
  • new (615-623)
  • new (737-746)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Build NH on Linux
  • GitHub Check: Test NH on Linux
  • GitHub Check: Test NH on Darwin
  • GitHub Check: Test NH on Linux
  • GitHub Check: Test NH on Darwin
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Build NH on Linux
🔇 Additional comments (5)
src/nixos.rs (5)

67-88: LGTM! Clean implementation of the --run flag.

The implementation correctly:

  • Extracts the should_run flag and output path before building
  • Builds the VM using the existing infrastructure
  • Conditionally executes the VM only when the flag is set

The logic flow is clear and the error handling is appropriate.


281-286: LGTM! Helpful user guidance.

Printing VM instructions after a successful build provides clear guidance to users on how to start their VM. The condition checks ensure instructions are only shown when appropriate (BuildVm variant, not dry run).


614-646: LGTM! Graceful error handling with helpful fallback.

The function provides clear instructions whether or not the VM script is successfully located. The fallback message with a wildcard pattern ({}/bin/run-*-vm) is particularly helpful if script discovery fails.


648-680: LGTM! Proper VM script execution.

The implementation correctly:

  • Locates the VM runner script using the helper function
  • Executes it with show_output(true) for interactive VM usage
  • Provides clear error context including the script path
  • Uses proper error wrapping for the entire operation

The approach is safe since the script comes from a Nix build output.


566-680: No duplicate definitions found: find_vm_script, print_vm_instructions, and run_vm are each defined only once in src/nixos.rs.

Likely an incorrect or invalid review comment.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/nixos.rs (1)

616-634: Consider documenting that VM execution is synchronous.

The VM is executed synchronously, blocking until the VM is shut down. This is likely the intended behavior for simplicity, but consider documenting this in the CLI help text so users understand the tool won't return until they shut down the VM.

If you decide the blocking behavior should be documented, add a note to the --run flag's help text in src/interface.rs:

 /// Run the VM immediately after building
-#[arg(long, short = 'r')]
+#[arg(long, short = 'r', help = "Run the VM immediately after building (blocks until VM is shut down)")]
 pub run: bool,
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a30ace8 and c5b6392.

📒 Files selected for processing (2)
  • src/interface.rs (1 hunks)
  • src/nixos.rs (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/interface.rs (1)
src/commands.rs (1)
  • arg (199-202)
src/nixos.rs (1)
src/commands.rs (3)
  • new (156-167)
  • new (611-619)
  • new (733-742)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Build NH on Linux
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Test NH on Darwin
  • GitHub Check: Test NH on Linux
  • GitHub Check: Test NH on Darwin
  • GitHub Check: Test NH on Linux
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Build NH on Linux
🔇 Additional comments (5)
src/interface.rs (1)

195-197: LGTM!

The new --run / -r flag is well-implemented and follows the existing patterns in the codebase. The boolean field appropriately defaults to false, preserving backward compatibility.

src/nixos.rs (4)

67-87: LGTM!

The implementation correctly captures the --run flag, builds the VM, and conditionally executes it. The output path handling is consistent with the existing patterns in the rebuild method.


282-285: LGTM!

The placement of print_vm_instructions in the non-dry BuildVm path is correct. Even when the --run flag is used (which executes the VM after this returns), printing the instructions remains useful as it shows the user the script location for future manual runs.


596-614: LGTM!

The function gracefully handles the case where the VM script can't be found, providing a helpful fallback message with the expected path pattern. This is good UX.


566-594: No duplicate VM helper function definitions Confirmed that find_vm_script, print_vm_instructions, and run_vm each appear only once in src/nixos.rs.

Adds a `--run` flag to `nh os build-vm`, that can be used to
automatically start the built VM once the build is complete.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a696460cbcf9422104fbdc062156a5a234227
@NotAShelf NotAShelf force-pushed the notashelf/push-tznonkqvxxyz branch from c5b6392 to 1641518 Compare October 4, 2025 17:59
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/nixos.rs (2)

616-634: Consider verifying executable permissions before running the script.

The function attempts to execute the VM script without checking if it has executable permissions, which could lead to a less clear error message at runtime.

Add a permissions check:

 fn run_vm(out_path: &Path) -> Result<()> {
   let vm_script = find_vm_script(out_path)?;
 
+  // Verify the script is executable
+  if let Ok(metadata) = fs::metadata(&vm_script) {
+    #[cfg(unix)]
+    {
+      use std::os::unix::fs::PermissionsExt;
+      if metadata.permissions().mode() & 0o111 == 0 {
+        return Err(eyre!(
+          "VM script at {} is not executable. Try: chmod +x {}",
+          vm_script.display(),
+          vm_script.display()
+        ));
+      }
+    }
+  }
+
   info!(

580-591: Ensure deterministic VM script selection (src/nixos.rs:580–587)
fs::read_dir yields entries in arbitrary order; if multiple run-*-vm scripts exist, the current .find() will pick one non-deterministically. Collect all matching entries, sort by file name (or error if more than one), then select the runner.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5b6392 and 1641518.

📒 Files selected for processing (4)
  • src/commands.rs (1 hunks)
  • src/generations.rs (2 hunks)
  • src/interface.rs (1 hunks)
  • src/nixos.rs (3 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/generations.rs
  • src/commands.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/interface.rs
🧰 Additional context used
🧬 Code graph analysis (1)
src/nixos.rs (1)
src/commands.rs (3)
  • new (160-171)
  • new (615-623)
  • new (737-746)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Test NH on Darwin
  • GitHub Check: treewide-checks
  • GitHub Check: Test NH on Linux
  • GitHub Check: Build NH on Linux
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Build NH on Linux
  • GitHub Check: Test NH on Linux
  • GitHub Check: Build NH on Darwin
  • GitHub Check: Test NH on Darwin
  • GitHub Check: treewide-checks
🔇 Additional comments (4)
src/nixos.rs (4)

67-88: LGTM! Clean implementation of the --run flag.

The logic correctly captures the run flag, builds the VM, and conditionally executes it. The error propagation and path handling are appropriate.


281-285: Good addition of user guidance for VM builds.

Printing instructions on how to run the VM when not in dry-run mode improves the user experience.


596-614: LGTM! Graceful error handling with helpful fallback.

The function properly handles both success and failure cases from find_vm_script, providing clear guidance to users in either scenario.


566-634: Inconsistency: AI summary mentions duplication that isn't present.

The AI-generated summary claims "VM helper utilities are declared in two separate locations within src/nixos.rs, effectively duplicating the utilities," but the provided code shows only one set of these helper functions (lines 566-634). This appears to be an inconsistency between the summary and the actual code changes.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a69649dd90b33ab8b88126d30ed0bb5545671
@NotAShelf NotAShelf merged commit 9b584e4 into master Oct 4, 2025
13 checks passed
@NotAShelf NotAShelf deleted the notashelf/push-tznonkqvxxyz branch October 4, 2025 19:14
@coderabbitai coderabbitai bot mentioned this pull request Oct 6, 2025
13 tasks
@coderabbitai coderabbitai bot mentioned this pull request Oct 15, 2025
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants