-
-
Notifications
You must be signed in to change notification settings - Fork 310
Add some documentation about the official nix formatter #1129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're mentioning the If flakes are acceptable in nix.dev guides, maybe adding the drv to a flake's Such an example may be a good opportunity to "sneak in" defining
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I'm very ambivalent on this, though. I'm going to leave this unresolved, perhaps someone with strong opinions will show up. |
||
Uh oh!
There was an error while loading. Please reload this page.