Skip to content

ci/treefmt: add nixf-diagnose#438559

Merged
Mic92 merged 4 commits intoNixOS:masterfrom
wolfgangwalther:ci-nixf-diagnose
Oct 5, 2025
Merged

ci/treefmt: add nixf-diagnose#438559
Mic92 merged 4 commits intoNixOS:masterfrom
wolfgangwalther:ci-nixf-diagnose

Conversation

@wolfgangwalther
Copy link
Copy Markdown
Contributor

@wolfgangwalther wolfgangwalther commented Aug 30, 2025

This currently has plenty of failures, so we disable many checks. We can then start working towards fixing these rules 1-by-1.

This is still a draft, because it needs an update of nixf-diagnose for inclyc/nixf-diagnose#7. This is hacked into the first WIP commit for experimentation. Once #438615 hits the channel, we can update the pin.

Closes #332695

Things done


Add a 👍 reaction to pull requests you find important.

@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: testing Tooling for automated testing of packages and modules 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library 6.topic: continuous integration Affects continuous integration (CI) in Nixpkgs, including Ofborg and GitHub Actions 8.has: documentation This PR adds or changes documentation backport release-25.05 labels Aug 30, 2025
Copy link
Copy Markdown
Contributor

@MattSturgeon MattSturgeon left a comment

Choose a reason for hiding this comment

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

Nice

Comment on lines -107 to +109
+ optionalString (config.networking.primaryIPv6Address != "") (
"${config.networking.primaryIPv6Address} ${hostnames}"
)
+ optionalString (
config.networking.primaryIPv6Address != ""
) "${config.networking.primaryIPv6Address} ${hostnames}"
Copy link
Copy Markdown
Contributor

@MattSturgeon MattSturgeon Aug 30, 2025

Choose a reason for hiding this comment

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

I'm a big fan of removing redundant parentheses. I believe it makes the intentional parentheses more meaningful when the reader knows they are meaningful, and it reduces noise.

Although, we can see in this example, that sometimes parentheses can be used to influence formatting (also seen in the nixos/lib/systemd-lib.nix example).


Another time where clarifying parentheses can be useful, is when applying an or expression as a function argument, or including an or expression as an item in a list literal.

Technically foo bar.baz or null is the same as foo (bar.baz or null) and the parentheses are redundant. However, since or is (the only?) example where having whitespace in an expression doesn't delimitate applied arguments, I think most readers find they add a lot of clarity in this very specific scenario.

The same is true for list literals, where [ foo.bar or "baz" ] is actually a singleton list (equivalent to [ (foo.bar or "baz") ]), despite many thinking it looks like a three item list due to the whitespace.

Note that unlike or, ? cannot be used without parentheses:

Details

nix-repl> [ {} ? foo ]
error: syntax error, unexpected '?'
       at «string»:1:6:
            1| [ {} ? foo ]
             |      ^

nix-repl> [ ({} ? foo) ]
[ false ]

nix-repl> [ {}.foo or null ]
[ null ]


Does the linting rule make an exception for clarifying or expressions? Or perhaps there may even be a linting rule to enforce clarifying parentheses around or expressions applied as function arguments or list elements?

(maybe this is more a question for upstream, or I should stop being lazy and go RTFM read the upstream docs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I asked myself the same questions that you did here and then concluded: The whole "redundant parentheses" is mostly a question of making the code more readable... which is not really a thing for the linter to worry about. I think the formatter should take care of that, thus I had opened NixOS/nixfmt#330. I also found NixOS/nixfmt#251, which is exactly the scenario you're describing as well.

In general, I think nixfmt should establish clear rules on when syntactically redundant parentheses are either added or removed - consistently.

And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when nixfmt can do it.

All of these thoughts came after I had already worked on a few of these parentheses, so just left the commits in here for now - also a good base to start discussion with.

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.

Based on the discussion in NixOS/nixfmt#330, I'm guessing we'll need to open discussions with nixf-diagnose regarding:

  1. Implementing auto-fix for some specific lints.
  2. Separating the "redundant parentheses" rule into subcategories, that we can selectively ignore.
  3. Adding an "enforce clarifying parentheses" rule for specific scenarios
    (i.e. when using or in a list or function argument).

IDK whether these should be general "toggle the rule in x scenario" subcategories, or if it is fine to just have a "except when it comes to or" option. OTTOMH, that is the only scenario where clarifying parentheses are universally uncontroversial.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@inclyc would love to hear your opinion here.

Copy link
Copy Markdown
Member

@inclyc inclyc Sep 1, 2025

Choose a reason for hiding this comment

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

And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when nixfmt can do it.

Would be easy to add more "rules" in nixf according to your feedback.

  1. Implementing auto-fix for some specific lints.

It is currently supported in nixf library, but not landed in nixf-diagnose the wrapper.

See inclyc/nixf-diagnose#9

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And no, the upstream linter only has a single rule about that to warn about these - I think we should essentially disable this, when nixfmt can do it.

Would be easy to add more "rules" in nixf according to your feedback.

That's great to hear. I'd probably start with:

And then when we do this across all of Nixpkgs, we might find more cases where parentheses should be kept.

@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Aug 30, 2025
@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. and removed 2.status: merge conflict This PR has merge conflicts with the target branch labels Aug 30, 2025
@nixpkgs-ci nixpkgs-ci bot added 6.topic: python Python is a high-level, general-purpose programming language. 6.topic: GNOME GNOME desktop environment and its underlying platform 6.topic: haskell General-purpose, statically typed, purely functional programming language 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: printing Drivers, CUPS & Co. 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 6.topic: ocaml OCaml is a general-purpose, high-level, multi-paradigm programming language. 6.topic: TeX Issues regarding texlive and TeX in general and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. labels Aug 30, 2025
@wolfgangwalther wolfgangwalther force-pushed the ci-nixf-diagnose branch 2 times, most recently from 2a6d269 to 4a0c5b4 Compare September 27, 2025 14:24
@wolfgangwalther wolfgangwalther marked this pull request as ready for review September 27, 2025 14:24
@wolfgangwalther
Copy link
Copy Markdown
Contributor Author

This is ready for review now. It technically works and all, now we need to check whether we are confident with enforcing these rules as a starter.

@nix-owners nix-owners bot requested a review from SomeoneSerge September 27, 2025 14:28
@wolfgangwalther wolfgangwalther requested a review from a team September 27, 2025 14:48
Copy link
Copy Markdown
Contributor

@MattSturgeon MattSturgeon left a comment

Choose a reason for hiding this comment

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

2 nits with the comments in ci/default.nix, and some confusion about the redundant-parentheses rule not applying to all redundant parentheses.

Also, I don't think we fully resolved the conversation regarding clarifying parens around or expressions?

But no blockers, so LGTM.

Thanks for pushing to get this added; this kinda linting should help to improve the baseline code quality, or at least cleanliness.

"--ignore=sema-unused-def-lambda-noarg-formal"
"--ignore=sema-unused-def-lambda-witharg-arg"
"--ignore=sema-unused-def-lambda-witharg-formal"
"--ignore=sema-unused-def-let"
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.

I agree, unused let bindings should be cleaned up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think there is actually quite a lot of redundant code that would be removed here. I think this would hit entire definitions of things, so that's a big potential to remove dead stuff that actually simplifies maintenance.

@wolfgangwalther wolfgangwalther removed this from Stdenv Sep 27, 2025
@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 27, 2025
@wolfgangwalther
Copy link
Copy Markdown
Contributor Author

wolfgangwalther commented Sep 27, 2025

Explaining the rebuilds:

@wolfgangwalther wolfgangwalther force-pushed the ci-nixf-diagnose branch 2 times, most recently from c0342db to 121ed77 Compare September 28, 2025 12:47
@wolfgangwalther
Copy link
Copy Markdown
Contributor Author

nixpkgs-review result

Generated using nixpkgs-review.

Command: nixpkgs-review pr 438559
Commit: 121ed77a5378d19d82a540c716455ebc20e4c875


x86_64-linux

⏩ 2 packages blacklisted:
  • nixos-install-tools
  • tests.nixos-functions.nixos-test
✅ 4 packages built:
  • nix-required-mounts
  • nix-required-mounts.dist
  • nixpkgs-manual
  • vimPluginsUpdater

aarch64-linux

⏩ 2 packages blacklisted:
  • nixos-install-tools
  • tests.nixos-functions.nixos-test
✅ 4 packages built:
  • nix-required-mounts
  • nix-required-mounts.dist
  • nixpkgs-manual
  • vimPluginsUpdater

x86_64-darwin

✅ 4 packages built:
  • nix-required-mounts
  • nix-required-mounts.dist
  • nixpkgs-manual
  • vimPluginsUpdater

aarch64-darwin

✅ 4 packages built:
  • nix-required-mounts
  • nix-required-mounts.dist
  • nixpkgs-manual
  • vimPluginsUpdater

Copy link
Copy Markdown
Contributor

@qweered qweered left a comment

Choose a reason for hiding this comment

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

Let's do this!

@nixpkgs-ci nixpkgs-ci bot added 12.approvals: 2 This PR was reviewed and approved by two persons. and removed 12.approvals: 1 This PR was reviewed and approved by one person. labels Sep 28, 2025
Copy link
Copy Markdown
Member

@jopejoe1 jopejoe1 left a comment

Choose a reason for hiding this comment

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

Love this, but needs a rebase :)

This currently has plenty of failures, so we disable many checks. We can
now start working towards fixing these rules 1-by-1.
Auto-fix by nixf-diagnose.
Auto-fixed by nixf-diagnose.
Auto-fixed by nixf-diagnose.
@wolfgangwalther
Copy link
Copy Markdown
Contributor Author

I'll work on the periodic merges and on backporting this now.

@nixpkgs-ci

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: agda A dependently typed programming language / interactive theorem prover 6.topic: cinnamon Desktop environment 6.topic: continuous integration Affects continuous integration (CI) in Nixpkgs, including Ofborg and GitHub Actions 6.topic: coq A formal proof management system 6.topic: COSMIC COSMIC is a software platform for designing beautiful user experiences 6.topic: dotnet Language: .NET 6.topic: emacs Text editor 6.topic: erlang General-purpose, concurrent, functional high-level programming language 6.topic: flutter Open-source UI software development kit for cross-platform applications 6.topic: games Gaming on NixOS 6.topic: GNOME GNOME desktop environment and its underlying platform 6.topic: hardware Drivers, Firmware and Kernels 6.topic: haskell General-purpose, statically typed, purely functional programming language 6.topic: kernel The Linux kernel 6.topic: lib The Nixpkgs function library 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 6.topic: lua Lua is a powerful, efficient, lightweight, embeddable scripting language. 6.topic: mate The MATE Desktop Environment 6.topic: module system About "NixOS" module system internals 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: nodejs Node.js is a free, open-source, cross-platform JavaScript runtime environment 6.topic: nvidia Nvidia-specific issues and fixes 6.topic: ocaml OCaml is a general-purpose, high-level, multi-paradigm programming language. 6.topic: pantheon The Pantheon desktop environment 6.topic: php PHP is a general-purpose scripting language geared towards web development. 6.topic: printing Drivers, CUPS & Co. 6.topic: python Python is a high-level, general-purpose programming language. 6.topic: qt/kde Object-oriented framework for GUI creation 6.topic: R R is a programming language for statistical computing and data visualization. 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 6.topic: stdenv Standard environment 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 6.topic: testing Tooling for automated testing of packages and modules 6.topic: TeX Issues regarding texlive and TeX in general 6.topic: vim Advanced text editor 6.topic: vscode A free and versatile code editor that supports almost every major programming language. 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 8.has: port to stable This PR already has a backport to the stable release. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 3+ This PR was reviewed and approved by three or more persons.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Documentation: mention nixf-tidy in contributing guide

7 participants