-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
nixfmt-tree: init #384857
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
nixfmt-tree: init #384857
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,133 @@ | ||||||
| { | ||||||
| lib, | ||||||
| runCommand, | ||||||
| buildEnv, | ||||||
| makeWrapper, | ||||||
| writers, | ||||||
| treefmt2, | ||||||
| nixfmt-rfc-style, | ||||||
| nixfmt-tree, | ||||||
|
|
||||||
| settings ? { | ||||||
| # The default is warn, which would be too annoying for people who just care about Nix | ||||||
| on-unmatched = "info"; | ||||||
| # Assumes the user is using Git, fails if it's not | ||||||
| tree-root-file = ".git/index"; | ||||||
|
Comment on lines
+14
to
+15
Contributor
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. Sadly this also assumes a non-bare git repo and not using multiple/separate worktrees. I suspect it will be common for users to only want to modify nixfmt-tree.override (old: {
settings = old.settings // {
tree-root-file = "some-other-file";
};
})Luckily it isn't a nested setting, so they won't need I don't have a better default to suggest for now, I think this is more a fundamental issue with the way treefmt searches for the root of the tree. Maybe down the line, treefmt would allow us to configure a more dynamic method of determining the root, such as defining a script that can be run to test a directory, instead of a plain filename that is tested for existence. I'm not asking for changes: just raising the issue for the record. |
||||||
|
|
||||||
| formatter.nixfmt = { | ||||||
| command = "nixfmt"; | ||||||
| includes = [ "*.nix" ]; | ||||||
| }; | ||||||
| }, | ||||||
| runtimePackages ? [ | ||||||
| nixfmt-rfc-style | ||||||
| ], | ||||||
| }: | ||||||
| buildEnv { | ||||||
| name = "nixfmt-tree"; | ||||||
|
|
||||||
| # Allows this derivation to be used as a shell providing both treefmt and nixfmt | ||||||
| paths = [ treefmt2 ] ++ runtimePackages; | ||||||
| pathsToLink = [ "/bin" ]; | ||||||
|
|
||||||
| nativeBuildInputs = [ | ||||||
| makeWrapper | ||||||
| ]; | ||||||
|
|
||||||
| postBuild = '' | ||||||
| wrapProgram $out/bin/treefmt \ | ||||||
| --prefix PATH : $out/bin \ | ||||||
| --add-flags "--config-file ${writers.writeTOML "treefmt.toml" settings}" | ||||||
| ''; | ||||||
|
|
||||||
| meta = { | ||||||
| mainProgram = "treefmt"; | ||||||
| description = "Official Nix formatter zero-setup starter using treefmt"; | ||||||
| longDescription = '' | ||||||
| A zero-setup [treefmt](https://treefmt.com/) starter to get started using the [official Nix formatter](https://github.com/NixOS/nixfmt). | ||||||
|
|
||||||
| - For `nix fmt` to format all Nix files, add this to the `flake.nix` outputs: | ||||||
|
|
||||||
| ```nix | ||||||
| formatter.''${system} = nixpkgs.legacyPackages.''${system}.nixfmt-tree; | ||||||
| ``` | ||||||
|
|
||||||
| - The same can be done more efficiently with the `treefmt` command, | ||||||
| which you can get in `nix-shell`/`nix develop` by extending `mkShell` using | ||||||
|
||||||
|
|
||||||
| ```nix | ||||||
| mkShell { | ||||||
| packages = [ pkgs.nixfmt-tree ]; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| You can then also use `treefmt` in a pre-commit/pre-push [Git hook](https://git-scm.com/docs/githooks) | ||||||
| and `nixfmt` with your editors format-on-save feature. | ||||||
|
|
||||||
| - To check formatting in CI, run the following in a checkout of your Git repository: | ||||||
| ``` | ||||||
| treefmt --ci | ||||||
| ``` | ||||||
|
|
||||||
| For more flexibility, you can customise this package using | ||||||
|
||||||
| For more flexibility, you can customise this package using | |
| For more flexibility, you can customise this package using: |
Also very minor nit: some of your code blocks have a preceding blank line, while others do not. This should probably be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meta: I don't think it's even worth pointing out such small nits, it takes up way more time for the back-and-forth of writing the review and addressing it, than the value we get out of it 😅
Non-meta: There's a imo a good reason for this, it's because this makes sense grammatically (complete sentence before ":"):
For
nix fmtto format all Nix files, add this to theflake.nixoutputs: code.
While this doesn't:
For
nix fmtto format all Nix files, add this to theflake.nixoutputs code.
Whereas with the one here, this does make sense syntactically:
For more flexibility, you can customise this package using code.
While this doesn't (incomplete sentence before ":"):
For more flexibility, you can customise this package using: code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meta: I don't think it's even worth pointing out such small nits, it takes up way more time for the back-and-forth of writing the review and addressing it, than the value we get out of it 😅
You're right, I wouldn't have commented if I wasn't already reviewing and wasn't clear about the (in)significance. I do get more nit-picky when it comes to user-facing prose...
Non-meta: There's a imo a good reason for this
If it was a deliberate decision please disregard my feedback. I wasn't sure, as you'd used it on some lines but not others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do get more nit-picky when it comes to user-facing prose...
Imo that's best as a follow-up PR instead :)
If it was a deliberate decision please disregard my feedback. I wasn't sure, as you'd used it on some lines but not others.
All good ❤️
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way beyond scope for this PR, but would it make sense down the line to consider using
evalModulesto configuresettings?That way the default config could be partially overridden or extended using the module system.
Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's where treefmt-nix comes in, because it does effectively exactly that (and more) :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
treefmt-nix is great, but for simple cases maybe it's overkill.
I wonder if having modular settings here offers a nice middle ground of flexibility and simplicity, without needing another dependency.
Just food for thought, not necessarily feedback on this PR