We added a linter to Nixpkgs checking workflows

This linter, nixf is part of nixd, a nix language server. By using it, we will output the same diagnostic message as nixd, mainly:

  1. Better parsing error in some cases, like missing semicolon and unexpected character at line end
  2. Unused code, like arguments, variables, rec, with, etc
  3. Escaping with (use of external variables inside with, which can easily cause ambiguity). This can be part of avoiding overuse of with. (This may be postponed until we reach consensus on handling with in many aspects)

By using GitHub’s workflow message feature, we render it in ‘Files changed’ overview, directly below the lines with errors. You can check an example here: workflows/check-nixf-tidy.yml: by Aleksanaa · Pull Request #1 · Aleksanaa/nixpkgs · GitHub (But there are going to be more examples popping up in Nixpkgs lol)

This workflow is the same as the checking format one, and will only check files that did not have problems in the past, as well as newly created files. But after a few rounds of treewide, we can minimize the existing files with problems, so hopefully we will get a cleaner codebase.

This feature is still in the experimental stage, and may encounter edge cases or areas where people are dissatisfied. Feel free to remind me (@Aleksanaa) and @inclyc under PR where problem occurs. We will actively fix these issues and focus on future improvements.

This workflow can also be provided as a separated GitHub action in the future to enpower other similar projects at the same time. If anyone would like to do this, they are more than welcome.

Many thanks to @inclyc for developing such a powerful tool, and @Infinisil for telling me this very crucial GitHub feature that makes it work like a charm!

44 Likes

Original PR is here: workflows/check-nixf-tidy.yml: init by Aleksanaa · Pull Request #330066 · NixOS/nixpkgs · GitHub

1 Like

yay‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌!

1 Like

This is seriously cool!

1 Like

This is great!
I seriously love it when I don’t have to think about the specific formats and conventions when contributing to a project.

Would it make sense to add it to shell.nix like the formatter?

1 Like

Maybe I should write a wrapper to achieve a similar effect to deadnix. The current rendering is implemented using a jq expression, which may be a bit lame in our toolbox because I need to write another jq expression to make it more human friendly

2 Likes

Maybe just use nixd the language server, it shares the frontend with nixf-tidy, and we don’t need worry about rendering because diagnostics are rendered on man’s favorite editor.

2 Likes

Good point, now we’re brewing another epic editor war :sweat_smile:

1 Like

Unused code shouldn’t block PRs, imagine all the r-ryantm PRs that would stall. Is it possible to turn them into warnings rather than errors?

Files that had errors before editing will be temporarily skipped. For files without problems, r-ryantm will not corrupt them.

2 Likes

We should set stricter rules for this. Of course, it is OK to output them in a different color, but I tend to think that this is an error for us, so let’s output it in the error color.

Can these kinds of lints be added nixpkgs-lint/src/queries.rs at e3a0489204a5db0cd806c26ddecb608da59072a0 · nix-community/nixpkgs-lint · GitHub

3 Likes

Neat, i’m still unsure but lets see how it goes. My greatest fear is that we end up unable to merge the plethora of otherwise good drive-by contributions whose authors are often unresponsive to review feedback.

We can’t do anything about this, for example, they don’t format, write non-standard commit messages (I can still tolerate this, using squash merge). In this case, there is no difference between the reviewer pointing out that there is an unused variable and CI pointing out it, and the latter is even more efficient.

Of course they should not be bothered by existing errors, because the files with errors before editing will be ignored. If their submission is an important fix but they made some mistakes, there will be a committer to help them deal with it.

Yes absolutely we can do, these checks are specific to nixpkgs, maybe introduced under a CLI flag.

elpa.nix has unused varables, such as version, before a PR. Yet, this file is not ignored.

That’s @inclyc’s bot, not the workflow

1 Like

Yes, that was another implementation which based on errors occurred on git-diff, it’s a little more chatty and based on PR reviews (not CI comments).

Regarding this, I’m willing to sit down, run the linter on all packages, and fix them one by one. I could do this as a chore for about 10 minutes daily or such. It would take months depending on how many packages “break”, but I guess once it’s all cleared up, the CI wouldn’t need to try and ignore existing issues.

Nevertheless, I’m not sure how much of this is worth than putting my time towards something considered more important.

2 Likes

Oh also, although my CI does not extract it, nixf-tidy actually includes the suggested fix information which can be processed with a script. It currently does not support unused variables, but that should also be trivial.

1 Like