Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions source/guides/recipes/autoformatting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(autoformatting)=
# Autoformatting

[`nixfmt`](https://github.com/NixOS/nixfmt) is the official Nix autoformatter.
Official tooling currently does not use nixfmt out of the box. Subscribe to
[NixOS/nix PR #11252](https://github.com/NixOS/nix/pull/11252) for updates on that effort.

Because `nixfmt` doesn't support formatting whole directory trees, you need
additional tooling such as `treefmt`. The `nixfmt-tree` package provides a
`treefmt` pre-configured to run `nixfmt` on all nix files in your project. Just
add it to your shell:

```nix
mkShell {
packages = [ pkgs.nixfmt-tree ];
}
```

Note: this assumes you're project is in a git repository, and you wish to treat
the entire repo as your project to be formatted.

If you need to configure any [treefmt options], or enable formatting other
(non-nix) files, you can use `treefmt.withConfig`:

[treefmt options]: https://treefmt.com/latest/getting-started/configure/#global-options

```nix
pkgs.treefmt.withConfig {
runtimeInputs = [
pkgs.nixfmt-rfc-style
pkgs.ruff
];

settings = {
# Customize detection of the root of the project.
tree-root-file = "flake.nix";

# Configure nixfmt for .nix files.
formatter.nixfmt = {
command = "nixfmt";
includes = [ "*.nix" ];
};

# And for .py file.
formatter.ruff = {
command = "ruff";
options = [ "format" ];
includes = [ "*.py" ];
};
};
}
```

This can get a little tedious.
[treefmt-nix](https://github.com/numtide/treefmt-nix) has a big library of
preconfigured formatters, and provides a `check` derivation you can use in CI.

Choose a reason for hiding this comment

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

If we're mentioning the check derivation, should we include a (simple) example of using it?

If flakes are acceptable in nix.dev guides, maybe adding the drv to a flake's checks output would be a suitable example?

Such an example may be a good opportunity to "sneak in" defining formatter and devshell outputs, although not if that ends up making the example less simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree it would be nice to include a simple example of using the check derivation, but that requires either using npins or flakes to pull in treefmt-nix. It gets messy kind of fast, and I'm lightly leaning towards "just send them to treefmt-nix to figure this out", as I've done here.

I'm very ambivalent on this, though. I'm going to leave this unresolved, perhaps someone with strong opinions will show up.

1 change: 1 addition & 0 deletions source/guides/recipes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Managing remote sources <./dependency-management.md>
Python development environment <./python-environment.md>
post-build-hook.md
continuous-integration-github-actions.md
autoformatting.md
```
Loading