nh icon indicating copy to clipboard operation
nh copied to clipboard

various: allow specifying profiles for platform commands

Open NotAShelf opened this issue 3 months ago • 1 comments

Allows specifying a --profile for Nix commands in NixOS, Darwin, and Home-Manager subcommands (os, darwin, and home respectively). This fills in the gap of --profile-path from nixos-install and similar CLIs being missing in NH.

Summary by CodeRabbit

  • New Features

    • Add --profile option to OS, Darwin, Home rebuild and OS rollback to target a custom profile path.
  • Improvements

    • Clear, early error when a provided profile path does not exist; CLI validates profile as a symlink.
    • Use the provided profile path for builds, rollbacks, diffs, and activation flows.
    • Refined sudo prompt wording and improved environment handling for elevated commands.
  • Chores

    • More resilient password-cache locking behavior (no public API changes).
  • Documentation

    • Add changelog entry describing the new --profile option.

NotAShelf avatar Sep 10 '25 09:09 NotAShelf

Walkthrough

Adds a new optional --profile/-P option to OS, Home, and Darwin rebuild/rollback commands with CLI-time symlink validation, threads the provided profile through build/rollback/diff logic, adjusts mutex poison handling and sudo/env formatting in command helpers, and updates nix invocations to propagate env and wrap errors.

Changes

Cohort / File(s) Summary of changes
CLI: add profile flag & validator
src/interface.rs
Adds pub profile: Option<std::path::PathBuf> to OsRebuildArgs, OsRollbackArgs, HomeRebuildArgs, DarwinRebuildArgs; introduces symlink_path_validator(s: &str) -> Result<PathBuf,String> and wires it as value_parser for --profile (-P) with FilePath hint.
NixOS: unify/derive profile path
src/nixos.rs
Thread an optionally provided profile through rebuilds and rollbacks: derive system_profile_path from --profile or fallback to SYSTEM_PROFILE, use it for --profile in nix builds, diffs, symlink targets, generation resolution, rollback restoration, and activation flows.
Darwin: helpers & nix invocation
src/darwin.rs
Add get_system_profile and get_current_profile_pathbuf helpers; use computed profile path for nix --profile arg; update nix invocation to call .with_required_env(), .run(), and wrap errors with context.
Home: profile override for prev_generation
src/home.rs
Add USER_PROFILE_PATH and HOME_PROFILE_PATH constants; if --profile provided and exists, use it as prev_generation (clone selected path); otherwise preserve existing discovery between user and home profiles; log chosen prev_generation.
Command helpers: mutex, sudo prompt, env formatting
src/commands.rs
Recover poisoned mutex guards using PoisonError::into_inner() in password cache access; change sudo prompt to use a named {host} placeholder; format elevated command environment pairs as {key}={value} in both Set and Preserve branches.
Docs
CHANGELOG.md
Add Unreleased entry documenting the new --profile flag for os, home, and darwin subcommands and its behavior.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant CLI as CLI Parser
  participant Cmd as Rebuild/Rollback Logic
  participant Validator as Symlink Validator
  participant Nix as Nix Runner

  User->>CLI: Invoke command [--profile /path]?
  CLI->>Validator: validate --profile (symlink exists)
  alt valid profile provided
    Validator-->>CLI: Ok(profile PathBuf)
  else invalid/missing provided profile
    Validator-->>CLI: Err -> CLI returns error
  end
  CLI->>Cmd: pass args (profile: Option<PathBuf>)
  Cmd->>Cmd: derive system_profile_path (provided or default)
  Cmd->>Nix: build/run with ["build","--no-link","--profile", <system_profile_path>]
  Note right of Nix: env formatted as key=value\n.with_required_env() → .run()\nerrors wrapped with context
  Nix-->>Cmd: result
  Cmd-->>User: outcome / error

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Pre-merge checks (2 passed, 1 inconclusive)

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title Check ❓ Inconclusive The current title captures the core feature—adding profile specification to multiple commands—but prefixes it with the vague term “various,” which adds noise without clarifying which commands are affected, making the title less precise and harder to scan at a glance. Please remove the generic “various” prefix and rename the PR to succinctly reflect the main change, for example: “Allow specifying --profile for os, home, and darwin commands.”
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
  • [ ] Commit unit tests in branch notashelf/push-lmlmokouvzvy

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

coderabbitai[bot] avatar Sep 10 '25 09:09 coderabbitai[bot]