From 6d57d1ba9c3eda7c17252af55df11e8eb7151454 Mon Sep 17 00:00:00 2001 From: Jason Yundt Date: Fri, 23 Aug 2024 10:45:40 -0400 Subject: [PATCH] Create .pre-commit-hooks.yaml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will allow people to enforce the style of .nix files in their own repos via the pre-commit tool. This change is related to #221, but it doesn’t actually implement the idea proposed in that issue. When writing the instructions for how to use the nixfmt pre-commit hook, I tried to make it clear that there’s a difference between the pre-commit tool and Git pre-commit hooks in general. I tried to make the difference between those two things clear for two reasons: 1. Both of those things are called pre-commit which makes it easy for people to mix them up [1]. 2. Depending on how #221 shakes out, the nixfmt project might end up adding another alternative mechanism for using nixfmt as part of a Git pre-commit hook script. I’m preemptively using language that makes it easy to differentiate between those two different mechanisms just in case there ends up being two different mechanisms in the future. [1]: --- .pre-commit-hooks.yaml | 7 +++++++ README.md | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..c96e7580 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,7 @@ +- id: &id nixfmt + name: *id + entry: *id + language: haskell + types: + - nix + description: The official (but not yet stable) formatter for Nix code diff --git a/README.md b/README.md index 3df69fcf..5aa43bc1 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,49 @@ Haskell dependencies will be built by Cabal. * `nixfmt < input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout` * `nixfmt file.nix` – format the file in place +### With the `pre-commit` tool + +If you have Nix files in a Git repo and you want to make sure that they’re formatted with `nixfmt`, then you can use the `pre-commit` tool from [pre-commit.com](https://pre-commit.com): + +1. Make sure that you have the `pre-commit` command: + + ```console + $ pre-commit --version + pre-commit 3.7.1 + ``` + +2. Make sure that you’re in your Git repo: + + ```console + $ cd + ``` + +3. Make sure that the `pre-commit` tool is installed as a Git pre-commit hook: + + ```console + $ pre-commit install + pre-commit installed at .git/hooks/pre-commit + ``` + +4. If you don’t already have one, then create a `.pre-commit-config.yaml` file. + +5. Add an entry for the `nixfmt` hook to your `.pre-commit-config.yaml` file: + + ``` + repos: + - repo: https://github.com/NixOS/nixfmt + rev: + hooks: + - id: nixfmt + ``` + + If you want to use a stable version of `nixfmt`, then replace `` with a tag from this repo. If you want to use an unstable version of `nixfmt`, then replace `` with a commit hash from this repo. + +6. Try to commit a badly formatted Nix file in order to make sure that everything works. + +> [!WARNING] +> `nixfmt`’s integration with the `pre-commit` tool is relatively new. At the moment, none of the stable releases of `nixfmt` can be used with the `pre-commit` tool. You’ll have to use an unstable version for the time being. + ## About Serokell