2021-10-24 10:56:37 +03:00
|
|
|
# statix
|
2021-09-11 18:48:09 +03:00
|
|
|
|
2021-10-24 10:56:37 +03:00
|
|
|
> Lints and suggestions for the Nix programming language.
|
|
|
|
|
2021-10-26 16:48:28 +03:00
|
|
|
`statix check` highlights antipatterns in Nix code. `statix
|
|
|
|
fix` can fix several such occurrences.
|
2021-09-11 18:48:09 +03:00
|
|
|
|
2021-10-02 08:22:22 +03:00
|
|
|
For the time-being, `statix` works only with ASTs
|
2021-09-11 18:48:09 +03:00
|
|
|
produced by the `rnix-parser` crate and does not evaluate
|
2021-12-14 07:20:27 +03:00
|
|
|
any nix code (imports, attr sets etc.).
|
2021-09-11 18:48:09 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
```shell
|
2021-10-26 16:48:28 +03:00
|
|
|
$ statix check tests/c.nix
|
2021-10-24 15:25:57 +03:00
|
|
|
[W04] Warning: Assignment instead of inherit from
|
|
|
|
╭─[tests/c.nix:2:3]
|
|
|
|
│
|
|
|
|
2 │ mtl = pkgs.haskellPackages.mtl;
|
|
|
|
· ───────────────┬───────────────
|
|
|
|
· ╰───────────────── This assignment is better written with inherit
|
|
|
|
───╯
|
|
|
|
|
2021-10-26 16:48:28 +03:00
|
|
|
$ statix fix --dry-run tests/c.nix
|
2021-10-24 15:25:57 +03:00
|
|
|
--- tests/c.nix
|
|
|
|
+++ tests/c.nix [fixed]
|
|
|
|
@@ -1,6 +1,6 @@
|
|
|
|
let
|
|
|
|
- mtl = pkgs.haskellPackages.mtl;
|
|
|
|
+ inherit (pkgs.haskellPackages) mtl;
|
|
|
|
in
|
|
|
|
null
|
|
|
|
```
|
|
|
|
|
2021-10-24 10:56:37 +03:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
`statix` is available via a nix flake:
|
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
```shell
|
|
|
|
# build from source
|
2021-10-24 10:56:37 +03:00
|
|
|
nix build git+https://git.peppe.rs/languages/statix
|
|
|
|
./result/bin/statix --help
|
2021-10-24 15:25:57 +03:00
|
|
|
|
|
|
|
# statix also provides a flake app
|
|
|
|
nix run git+https://git.peppe.rs/languages/statix -- --help
|
|
|
|
|
|
|
|
# save time on builds using cachix
|
|
|
|
cachix use statix
|
2021-10-24 10:56:37 +03:00
|
|
|
```
|
|
|
|
|
2022-01-30 10:09:38 +03:00
|
|
|
Install from nixpkgs:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
nix run nixpkgs#statix -- help
|
|
|
|
```
|
|
|
|
|
2021-12-14 07:20:27 +03:00
|
|
|
Install with [brew/linuxbrew](https://brew.sh)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
brew install statix
|
|
|
|
```
|
|
|
|
|
2021-10-24 10:56:37 +03:00
|
|
|
## Usage
|
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
Basic usage is as simple as:
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
```shell
|
|
|
|
# recursively finds nix files and raises lints
|
2021-10-26 16:48:28 +03:00
|
|
|
statix check /path/to/dir
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
# ignore generated files, such as Cargo.nix
|
2021-11-04 17:47:45 +03:00
|
|
|
statix check /path/to/dir -i Cargo.nix
|
|
|
|
|
|
|
|
# ignore more than one file
|
|
|
|
statix check /path/to/dir -i a.nix b.nix c.nix
|
|
|
|
|
|
|
|
# ignore an entire directory
|
|
|
|
statix check /path/to/dir -i .direnv
|
|
|
|
|
|
|
|
# statix respects your .gitignore if it exists
|
|
|
|
# run statix in "unrestricted" mode, to disable that
|
|
|
|
statix check /path/to/dir -u
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
# see `statix -h` for a full list of options
|
|
|
|
```
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
Certain lints have suggestions. Apply suggestions back to
|
|
|
|
the source with:
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
```shell
|
2021-10-26 16:48:28 +03:00
|
|
|
statix fix /path/to/file
|
2021-10-24 10:56:37 +03:00
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
# show diff, do not write to file
|
2021-10-26 16:48:28 +03:00
|
|
|
statix fix --dry-run /path/to/file
|
2021-10-24 15:25:57 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
`statix` supports a variety of output formats; standard,
|
|
|
|
json and errfmt:
|
|
|
|
|
|
|
|
```shell
|
2021-10-26 16:48:28 +03:00
|
|
|
statix check /path/to/dir -o json # only when compiled with --all-features
|
|
|
|
statix check /path/to/dir -o errfmt # singleline, easy to integrate with vim
|
2021-10-24 10:56:37 +03:00
|
|
|
```
|
|
|
|
|
2021-11-28 11:00:40 +03:00
|
|
|
### Configuration
|
|
|
|
|
|
|
|
Ignore lints and fixes by creating a `statix.toml` file at
|
|
|
|
your project root:
|
|
|
|
|
|
|
|
```
|
|
|
|
# within statix.toml
|
|
|
|
disabled = [
|
2021-11-28 16:55:36 +03:00
|
|
|
"empty_pattern"
|
2021-11-28 11:00:40 +03:00
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
`statix` automatically discovers the configuration file by
|
|
|
|
traversing parents of the current directory and looking for
|
|
|
|
a `statix.toml` file. Alternatively, you can pass the path
|
|
|
|
to the `statix.toml` file on the command line with the
|
|
|
|
`--config` flag (available on `statix check` and `statix
|
2021-11-28 16:55:36 +03:00
|
|
|
fix`).
|
2021-11-28 11:00:40 +03:00
|
|
|
|
2022-05-01 18:17:35 +03:00
|
|
|
The available lints are (see `statix list` for an updated
|
|
|
|
list):
|
2021-11-28 11:00:40 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
bool_comparison
|
|
|
|
empty_let_in
|
|
|
|
manual_inherit
|
|
|
|
manual_inherit_from
|
|
|
|
legacy_let_syntax
|
|
|
|
collapsible_let_in
|
|
|
|
eta_reduction
|
|
|
|
useless_parens
|
|
|
|
empty_pattern
|
|
|
|
redundant_pattern_bind
|
|
|
|
unquoted_uri
|
|
|
|
deprecated_is_null
|
2022-01-30 10:09:38 +03:00
|
|
|
empty_inherit
|
|
|
|
faster_groupby
|
|
|
|
faster_zipattrswith
|
|
|
|
deprecated_to_path
|
2022-05-01 18:17:35 +03:00
|
|
|
bool_simplification
|
|
|
|
useless_has_attr
|
2021-11-28 11:00:40 +03:00
|
|
|
```
|
|
|
|
|
2022-05-01 18:17:35 +03:00
|
|
|
All lints are enabled by default. Generate a minimal config
|
|
|
|
with `statix dump > statix.toml`.
|
2021-11-28 11:00:40 +03:00
|
|
|
|
2021-10-15 10:37:08 +03:00
|
|
|
## TODO
|
|
|
|
|
2021-10-24 15:25:57 +03:00
|
|
|
- Resolve imports and scopes for better lints
|
|
|
|
- Add silent flag that exits with status
|