Skip to content

nh-installable: resolve system.nix and default.nix for non-flakes - #745

Draft
NotAShelf wants to merge 1 commit into
masterfrom
notashelf/push-tuomqoupvqkl
Draft

NotAShelf wants to merge 1 commit into
masterfrom
notashelf/push-tuomqoupvqkl

Conversation

@NotAShelf

@NotAShelf NotAShelf commented Jul 22, 2026

Copy link
Copy Markdown
Member

nh now tries default.nix and system.nix akin to nixos-rebuild for explicit compatibility behaviour AFTER flake options were exhausted. This only applies to the default, flakes-first behaviour. For users already configuring nh to respect their non-flake setups, there is no behavioural changes.

Change-Id: I65bd9b5a49fe69a76cda12ffadc941e06a6a6964


Add a 👍 reaction to pull requests you find important.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
📝 Walkthrough

Walkthrough

Changes

The nh-installable crate centralizes default installable selection and local flake validation in a new defaults module. OS file and flake precedence, nixos-system discovery, contextual errors, and delegation from existing resolution paths are implemented and tested.

Installable defaults

Layer / File(s) Summary
Default selection and file resolution
crates/nh-installable/Cargo.toml, crates/nh-installable/src/defaults.rs
Adds command-context-based OS, home, and Darwin resolution, OS file lookup, flake discovery, nixos-system lookup, and precedence tests.
Local flake validation
crates/nh-installable/src/defaults.rs
Adds canonicalization- and symlink-aware flake directory resolution with contextual filesystem error handling.
Resolution integration and delegation
crates/nh-installable/src/lib.rs
Delegates default selection, file resolution, and local flake validation to defaults, removing the previous inline fallback helpers.

Possibly related PRs

Suggested reviewers: faukah

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately summarizes the main change: non-flake fallback resolution for system.nix and default.nix in nh-installable.
Description check ✅ Passed The description is directly related to the changeset and matches the stated flakes-first fallback behavior.

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.

@NotAShelf
NotAShelf marked this pull request as draft July 22, 2026 07:17

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 (1)
crates/nh-installable/src/defaults.rs (1)

182-210: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider adding direct tests for resolve_flake_directory symlink handling.

The symlinked-dir vs. symlinked-flake.nix branches are non-trivial and untested directly (existing tests only cover os_installable_at with plain directories). A couple of tempfile + std::os::unix::fs::symlink-based tests would guard this logic against regressions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/nh-installable/src/defaults.rs` around lines 182 - 210, The
symlink-handling branches in resolve_flake_directory lack direct coverage. Add
focused Unix tests using tempfile directories and symlink to verify both a
symlinked directory containing flake.nix and a plain directory whose flake.nix
is symlinked, asserting the returned resolved directory and relevant error
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/nh-installable/src/defaults.rs`:
- Around line 121-142: Update os_installable_at to check for
system_directory/default.nix after checking system.nix and before falling back
to <nixpkgs/nixos>. Preserve the existing debug logging and file installable
behavior for both discovered Nix files.

---

Nitpick comments:
In `@crates/nh-installable/src/defaults.rs`:
- Around line 182-210: The symlink-handling branches in resolve_flake_directory
lack direct coverage. Add focused Unix tests using tempfile directories and
symlink to verify both a symlinked directory containing flake.nix and a plain
directory whose flake.nix is symlinked, asserting the returned resolved
directory and relevant error behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fb66cf7f-1e83-4324-9311-b41501972010

📥 Commits

Reviewing files that changed from the base of the PR and between beb4e4e and 3874960.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • crates/nh-installable/Cargo.toml
  • crates/nh-installable/src/defaults.rs
  • crates/nh-installable/src/lib.rs

Comment on lines +121 to +142
fn os_installable_at(
system_directory: &Path,
find_system: impl FnOnce() -> Result<Option<PathBuf>>,
) -> Result<Installable> {
if let Some(installable) = find_flake(system_directory, "os", "NH_OS_FLAKE")?
{
return Ok(installable);
}

if let Some(path) = find_system()? {
debug!(path = %path.display(), "Using <nixos-system>");
return Ok(file(path));
}

let system_nix = system_directory.join("system.nix");
if system_nix.is_file() {
debug!(path = %system_nix.display(), "Using system.nix");
return Ok(file(system_nix));
}

Ok(file(PathBuf::from("<nixpkgs/nixos>")))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

set -euo pipefail
git ls-files crates/nh-installable/src/defaults.rs
printf '\n--- outline ---\n'
ast-grep outline crates/nh-installable/src/defaults.rs --view expanded
printf '\n--- relevant lines ---\n'
sed -n '1,220p' crates/nh-installable/src/defaults.rs | cat -n

Repository: nix-community/nh

Length of output: 8642


🏁 Script executed:

set -euo pipefail
sed -n '260,360p' crates/nh-installable/src/defaults.rs | cat -n
printf '\n--- references to resolve_file ---\n'
rg -n "resolve_file\(" -S .
printf '\n--- references to system.nix/default.nix in this area ---\n'
rg -n "system\.nix|default\.nix|nixpkgs/nixos|nixos-system" crates/nh-installable/src/defaults.rs

Repository: nix-community/nh

Length of output: 5037


os_installable_at should also fall back to default.nix. resolve_file already prefers system.nix and then default.nix for OS directories, but this default path only checks system.nix before falling back to <nixpkgs/nixos>, so a bare nh os switch can behave differently from an explicit /etc/nixos path when only default.nix exists.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/nh-installable/src/defaults.rs` around lines 121 - 142, Update
os_installable_at to check for system_directory/default.nix after checking
system.nix and before falling back to <nixpkgs/nixos>. Preserve the existing
debug logging and file installable behavior for both discovered Nix files.

…mode

nh now tries `default.nix` and `system.nix` akin to `nixos-rebuild` for
explicit compatibility behaviour AFTER flake options were exhausted.
This only applies to the default, flakes-first behaviour. For users
already configuring nh to respect their non-flake setups, there is no
behavioural changes.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I65bd9b5a49fe69a76cda12ffadc941e06a6a6964
@NotAShelf
NotAShelf force-pushed the notashelf/push-tuomqoupvqkl branch from b1bcc26 to 56a4341 Compare September 16, 2026 16:03
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.

1 participant