root: Add ShellCheck pre-commit configuration#146608
root: Add ShellCheck pre-commit configuration#146608l0b0 wants to merge 1 commit intoNixOS:masterfrom
Conversation
|
Using docker to lint your shell files is overkill. There are way simpler possibilities for probably most editors. I personally use shellcheck linting in vim and vscode through a plugin and extension. This also does not solve the problem that the end user still needs to install something, namely docker. I'd rather would like to see a shell.nix or a CI action instead of this. |
|
I didn't think |
|
pre-commit-hooks.nix may be a good option. It's a shell hook that configures pre-commit with a Nix-generated config. Also provides a derivation that runs the checks, for CI. |
|
Oh. I'll just change it to use Update: Fixed. Thanks for the heads up @SuperSandro2000 and the details @mohe2015! |
7ffbc45 to
5c7e040
Compare
|
@Artturin Is this something you're interested in reviewing? |
5c7e040 to
ffe965d
Compare
|
Ping @domenkozar @grahamc. |
|
Ping? This still seems relevant. |
|
I don't have an opinion on adding impure pre-commit, so the following is just for context.
Installation of hooks via shellHook should be opt in, so I would only recommend that project after |
|
Still worth having. We now have a |
|
IIUC, nixpkgs doesn't want any automatically installed git hooks, e.g. via shellHook. We could add manually installed hooks, but I'm unsure how useful that'd be. How would we promote and encourage their use, for example? What role would they play? Are they for running existing CI rules earlier (before push) or for introducing new rules? I assume the hook's rules would also be enforced separately by CI? If so, what'd be the workflow for someone who wants to run the failing rules locally, without installing any git hooks? I may sound skeptical, but I do think there is value in having pre-commit and/or pre-push hooks. I just think we need to consider rough edges that could add friction for contributors. Especially since the goal of such hooks should be to reduce friction. |
|
Closed because the majority of the feedback was negative. Which I don't quite understand, since at the core this is an opt-in feature with a single configuration file. I work around this by running |
|
I believe certainly optional hooks should become part of the developer experience at some point. I feel like a simple config with formatting only would already provide a net benefit. Regardless of that, I think it's possible to configure pre-commit to use a config file in, say, What if the file came with a top comment that manages expectations and requirements based on the points brought up here, and lists @l0b0 as the file's primary maintainer? |
Agreed
This is how treefmt works by default most of the time. After its first run, it caches the list of formatted files and only checks modified files in subsequent runs. It can also be configured to ask git for the modified files instead of managing its own cache, iirc.
Agreed, I'd prefer to avoid a dependency on git-hooks.nix if possible.
Note that this can cause issues when working with git workftres. I often have to manually reinstall hooks when working in a worktree that is out of sync with the last worktree I installed hooks from. This may be a fundamental issue with git hooks, though.
If we're starting off with a simple hook that only runs treefmt, then I suspect the CI team will be happy to help maintain it. At least, I don't object. But if @l0b0 is happy to be listed as a git hooks maintainer that's even better. |
I'd be happy to maintain I've only started using treefmt this week, so I'm not qualified to configure or maintain it to the standard I would expect from nixpkgs. To be clear, I have no opinion on which one is more appropriate for nixpkgs, and I'd be happy using anything which improves on the current situation. |
|
Nixpkgs is already using and configuring treefmt. It is made available in the default shell for the root directory of nixpkgs. |
I expect this to be significantly faster. No need to re- |
|
My opinion on the matter:
My goal for Nixpkgs is an opt-in pre-commit hook that will just do the right thing: Autoformat every commit with All of that can be done, and I have done all of that in different pieces for different (internal) projects already, it just needs to happen. |
It does auto-format, but it throws an error so that
Yeah, if you only stick to only Nix formatting that's doable :) |
Not sure I follow. When I run The same thing will happen to the pre-commit hook: It will fix everything it can automatically, but if there's something that needs to be taken care of manually, it will fail, thus stopping the commit. No risk to commit something that would fail treefmt. Yes, tools need to behave in a certain sane way, but the requirements are pretty simple, see https://treefmt.com/v2.3/reference/formatter-spec/ |
I think the issue isn't about whether something is auto-fixable or not, but about whether anything occurred at all. If the pre-commit hook changed something, then the author should review the new diff and manually stage the new changes before committing. Just because something was changed automatically, doesn't necessarily mean it should be part of the current commit. I don't have a solid example to hand, but it feels like committing with a partially staged diff ( Another example my be cases where there are more than one acceptable formats. As for If we could implement our own hooks with minimal effort and maintenance, that would be my preference. But I won't block using pre-commit as an initial starting point. |
That's precisely where I disagree. Do you really check the results of every
Yeah, that's precisely the case where the "pushing and popping stashes" approach is really bad. |
I always look at the output "number of files formatted" as a sanity check. Usually I expect it to be a handful of files checked, and 0-1 modified. Depending on the kind of changes I've been doing, I may check how they were formatted and use that to influence potential refactoring or tweaks, such as whitespace, comment positioning, single/multi-line lists, etc. Especially when using a heuristic formatter, like nixfmt, that can be influenced by the author's "intent". I recall in If I'm doing a treewide reformat because the formatter itself has changed, then I'd be spot-checking the result just to feel confident in the formatter itself. If I'm fixing someone elses badly formatted code, then I would only really care whether CI passes, so no real need to check the result in any detail. TL;DR: if any tool has modified something as a result of my local changes, I'd like to know about it before committing. I don't ever want to blindly commit anything, unless maybe I'm intentionally doing a large treewide-style commit. |
|
Clearly, we need to support both ways to run this hook :D |
|
I'd personally trust pre-commit here, because the tool went through many iterations over the years, so the experiernce you get isn't some random guys opinion :) In general it's better to be explicit about squashing things into a commit, but if you want to roll your own solutions that's okay. I'd still like if someone makes the hook installation optional as Robert attempted in cachix/git-hooks.nix#261 |
I do understand that, sure. At the same time, I have used an internal tool based on git-format-staged with auto-commit for 5+ years more than successfully. |
|
I've made the PR so you can individually opt out of git hooks, for those that want to run things manually. |
Motivation for this change
Existing issues requesting ShellCheck compliance: #133088, #21166.
pre-commit is a handy way to configure linters and formatters with a single configuration file. It can easily be set up (
pre-commit install) to run them for you as part of a Git pre-commit handler, and then only runs on the files you are about to commit.Things done
sandbox = trueset innix.conf? (See Nix manual) N/Anix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage N/A./result/bin/) N/A