Statix, is a linter for the Nix programming language, written in Rust with rnix-parser. Here is an excerpt from the readme:
statix check highlights antipatterns in Nix code. statix fix can fix several such occurrences.
It detects around 11 antipatterns at the moment (it can also fix these with statix fix!). The fix subcommand is beautiful in that it is idempotent and can fix all issues in one go.
Feel free to open issues on Github with lint ideas and things you’d like to see! Here is a little screenshot of statix check in action:
This is neat, but the flake hardcodes Linux. I was able to get it to work on Darwin by adding libiconv to the buildInputs. Is it possible to update it to support Darwin?
It also threw up several errors on my nixos-configs. I’m not sure if that’s just a limitation of the tool, or I’m doing something very weird. They obviously build and activate though. Should I open an issue on GitHub?
[./hosts/x86_64-darwin/claudette/users/reckenrode/home-manager/default.nix] syntax error: unexpected TOKEN_SQUARE_B_OPEN at 448..449, wanted any of [TOKEN_IDENT]
[./common/home-manager.nix] syntax error: error node at 683..691
[./hosts/x86_64-linux/zhloe/kea/default.nix] syntax error: unexpected TOKEN_ASSIGN at 282..317, wanted any of [TOKEN_SEMICOLON]
[./lib/mkHosts.nix] syntax error: unexpected TOKEN_DYNAMIC_START at 612..619, wanted any of [TOKEN_SEMICOLON]
Thanks for testing it out on Darwin! I can add Darwin support over the next couple of days.
It also threw up several errors on my nixos-configs. I’m not sure if that’s just a limitation of the tool, or I’m doing something very weird. They obviously build and activate though. Should I open an issue on GitHub?
Most likely a bug with rnix-parser (or atleast, the version of rnix-parser that I am using). Let me give it a go on a more recent version of rnix, and see if the errors persist, if they do, I’ll file issues over on the rnix-parser repo.
It could be! I did briefly take a look at nixpkgs-hammering, but it seemed to be focused more on nixpkgs itself, and not general nix antipatterns. In the near future, I am looking to partially evaluate nix code as well (flake inputs and imports), would you say that is within scope of nixpkgs-hammering?
I investigated this a bit, rnix-parser does not support template expressions in paths, introduced in nix 2.4, which not yet released. I’ll try to add support for this over the weekend.
I would say language level issues are in scope of nixpkgs code reviews. Some of those lints in nixpkgs-hammering already are independent of nixpkgs itself. I opened an issue: https://github.com/jtojnar/nixpkgs-hammering/issues/124
@nerdypepper
Thanks for your work on statix. I rly like the usability of it compared to other linters!
Apologies if this is a bit of beginner question, I still feel new to the nixos ecosystem. During my learning I stumbled over In the Nix language — nix.dev documentation and they make intuitive sense to me.
Are there any plans of including these checks? It’s non-trivial to make the descriptions to statixs checks.
@don.dfh@nerdypepper That specific list anti-patterns is not entirely uncontroversial though. Since we (@domenkozar@lucperkins) want to make nix.dev the official hub for Nix ecosystem documentation, we will eventually have to break that down to what Nix experts can agree on to be best practice. So, don’t take it too seriously for now. We can pick up that specific bit of discussion when the Nix logo appears on that web site’s top-left corner.