Skip to content

feat: auto-format Nix files with nixfmt after applying semantic edits#442

Draft
Scott McMaster (scottmcmaster) wants to merge 1 commit into
developfrom
06-22-scott-nix-fmt
Draft

feat: auto-format Nix files with nixfmt after applying semantic edits#442
Scott McMaster (scottmcmaster) wants to merge 1 commit into
developfrom
06-22-scott-nix-fmt

Conversation

@scottmcmaster

@scottmcmaster Scott McMaster (scottmcmaster) commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add a fail-free nixfmt step to the end of semantic edits (which seems more appropriate than formatting an entire repo or even an entire diff at the end). This way, the limitations of our semantic editor won't cause unusual-looking flake files. By putting it there instead of just in the tool, it will also be applied in other places we us the semantic editor, like tracking untracked changes.

Here is an example from one where I intentionally put some bad indents in my commited flake:

diff --git a/modules/darwin/packages.nix b/modules/darwin/packages.nix
index bdbbe63..7c19bc6 100644
--- a/modules/darwin/packages.nix
+++ b/modules/darwin/packages.nix
@@ -23,8 +23,10 @@
     # git   # version control
     # vim   # editor
     # htop  # process viewer
-];
+    ripgrep
+    wget
+  ];
 
   # If you prefer per-user profiles, consider using `home-manager` instead
   # of placing everything in the global system profile.
-   }
+}

Test Plan

Ran a few manual cases that cause non-trivial nix file edits.

  • No test plan needed

Docs

  • Docs updated (companion PR in darkmatter/nixmac-web: #___)
  • No docs update needed

Copy link
Copy Markdown
Collaborator Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • automerge - adds this PR to the back of the merge queue
  • urgent - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@scottmcmaster Scott McMaster (scottmcmaster) changed the title scott-nix-fmt feat: auto-format Nix files with nixfmt after applying semantic edits Jun 22, 2026
@darkmatter

darkmatter Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

🎨 Storybook preview

Open Storybook preview

Updated for 6bd5bd3


❌ Failed snapshots (1)

These stories' HTML snapshots changed. Update snapshots ↗ to regenerate baselines and open a PR:

Widget/DarwinWidget › Onboarding With Directory

Widget/DarwinWidget › Onboarding With Directory


Accept changes

  • Click here to accept these changes
What does this do?

The screenshots above show UI changes detected by the Storybook
snapshot tests run on this PR. Each image is the rendered output of
a Storybook story from the code in this PR branch; the snapshot
test compared it against the committed baseline in
__snapshots__/ and flagged the difference.

Checking the box tells the darkmatter[bot] to regenerate the
baselines from this PR's current code and commit them directly to
this branch. The new baselines become the source of truth for
future runs — only accept after confirming the visual changes are
intentional.

Comparison baseline: the committed __snapshots__/ files on this
PR branch (carried forward from develop). Accept updates them in
place on this branch.

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor
Warnings
⚠️ PR is marked WIP / draft — do not merge until ready for review.
Messages
📖 No docs update needed — acknowledged.

📋 PR Overview

Lines changed 379 (+61 / -318)
Files 0 added, 4 modified, 0 deleted
Draft / WIP yes
Has Test Plan yes
No Test Plan Needed no
New UI components no
New Storybook stories no
New Rust modules no
New TS source files no
New tests no
package.json touched no
Cargo.toml touched no
Infra / CI touched no

🔬 Coverage

Report Lines Statements Functions Branches
apps/native/coverage/coverage-summary.json 28.7% 28.9% 27.3% 21.9%

Generated by 🚫 dangerJS against 6bd5bd3

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a post-processing formatting step after applying semantic Nix edits so files don’t end up with awkward indentation/structure due to rnix-based rewriting, aiming to keep edited flake/module files consistently formatted.

Changes:

  • Introduces a new Rust helper to run nixfmt via nix run … from within the user’s config directory.
  • Calls the formatter at the end of apply_semantic_edit, warning (but not failing) if formatting fails.
  • Updates Bun/Nix dependency lock artifacts (bun.lock, bun.nix) as part of the change set.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
apps/native/src-tauri/src/system/nix.rs Adds nix_format() helper that shells out to nix run … for formatting.
apps/native/src-tauri/src/evolve/nix_file_editor.rs Invokes formatting after semantic edits, intended to be fail-free.
bun.lock Large lockfile resolution updates (not described in PR summary).
bun.nix Regenerated/updated Bun-to-Nix dependency mapping consistent with lock changes.

Comment on lines +375 to +384
// Format the file by default.
// If the formatting fails for some reason, allow the edit to be successful (but warn).
let config_dir = base.to_string_lossy();
let format_result = nix_format(&config_dir, &edit.path);
if let Err(e) = format_result {
log::warn!(
"apply_semantic_edit succeeded but nix-format failed: {}. The file may be left in an unformatted state; run nix-format manually to fix formatting issues.",
e
);
}
// Format the file by default.
// If the formatting fails for some reason, allow the edit to be successful (but warn).
let config_dir = base.to_string_lossy();
let format_result = nix_format(&config_dir, &edit.path);
Comment on lines +335 to +343
/// Runs `nixfmt` directly from the flake on the provided file.
/// Executes the command:
/// `nix run nixpkgs#nixfmt -- <file>`
pub fn nix_format(config_dir: &str, file: &str) -> Result<String> {
log::debug!("Running nix format on file: {}", file);

let output = nix_command(config_dir)
.args(["run", "nixpkgs#nixfmt", "--", file])
.output()?;
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