feat: auto-format Nix files with nixfmt after applying semantic edits#442
feat: auto-format Nix files with nixfmt after applying semantic edits#442Scott McMaster (scottmcmaster) wants to merge 1 commit into
nixfmt after applying semantic edits#442Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
nixfmt after applying semantic edits
🎨 Storybook previewUpdated for 6bd5bd3 ❌ Failed snapshots (1)These stories' HTML snapshots changed. Update snapshots ↗ to regenerate baselines and open a PR: Widget/DarwinWidget › Onboarding With DirectoryAccept changes
What does this do?The screenshots above show UI changes detected by the Storybook Checking the box tells the Comparison baseline: the committed |
📋 PR Overview
🔬 Coverage
|
There was a problem hiding this comment.
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
nixfmtvianix 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. |
| // 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); |
| /// 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()?; |


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:
Test Plan
Ran a few manual cases that cause non-trivial nix file edits.
Docs