From 1fb1bf8a73ccf207dbe967cdb7f2f4e0122c8bd5 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 15 Jul 2025 02:27:03 +0100 Subject: [PATCH] flake: move formatting and git hooks into dedicated modules --- flake/dev/default.nix | 119 +--------------------------------------- flake/dev/fmt.nix | 69 +++++++++++++++++++++++ flake/dev/git-hooks.nix | 62 +++++++++++++++++++++ 3 files changed, 133 insertions(+), 117 deletions(-) create mode 100644 flake/dev/fmt.nix create mode 100644 flake/dev/git-hooks.nix diff --git a/flake/dev/default.nix b/flake/dev/default.nix index 59d6b9ca..454b1d02 100644 --- a/flake/dev/default.nix +++ b/flake/dev/default.nix @@ -1,127 +1,12 @@ -{ inputs, ... }: { imports = [ ./devshell.nix + ./fmt.nix ./generate-all-maintainers + ./git-hooks.nix ./list-plugins ./package-tests.nix ./template-tests.nix ./tests.nix - inputs.git-hooks.flakeModule - inputs.treefmt-nix.flakeModule ]; - - perSystem = - { - pkgs, - config, - system, - ... - }: - { - ci.buildbot = { - inherit (config.checks) treefmt; - }; - - treefmt.config = { - projectRootFile = "flake.nix"; - flakeCheck = true; - - programs = { - # keep-sorted start block=yes newline_separated=no - isort.enable = true; - keep-sorted.enable = true; - nixfmt = { - enable = true; - package = pkgs.nixfmt-rfc-style; - }; - prettier = { - enable = true; - excludes = [ "**.md" ]; - }; - ruff = { - check = true; - format = true; - }; - shfmt.enable = true; - statix = { - enable = true; - disabled-lints = [ - # We often use `nullable == true` - "bool_comparison" - ]; - }; - stylua.enable = true; - # FIXME: re-enable on darwin, currently broken: taplo with options '[format]' failed to apply: exit status 101 - taplo.enable = pkgs.stdenv.isLinux; - # keep-sorted end - }; - - settings = { - global.excludes = [ - ".editorconfig" - ".envrc" - ".git-blame-ignore-revs" - ".gitignore" - "LICENSE" - "flake.lock" - "**.md" - "**.scm" - "**.svg" - "**/man/*.5" - # Those files are generated by pytest-regression, which then `diff`s them. - # Formatting them will make the tests fail. - "docs/gfm-alerts-to-admonitions/tests/**/*.yml" - ]; - formatter.ruff-format.options = [ "--isolated" ]; - }; - }; - - pre-commit = { - # We have a treefmt check already, so this is redundant. - # We also can't run the test if it includes running `nix build`, - # since the nix CLI can't build within a derivation builder. - check.enable = false; - - settings.hooks = { - deadnix = { - enable = true; - - settings = { - noLambdaArg = true; - noLambdaPatternNames = true; - edit = true; - }; - }; - treefmt = { - enable = true; - package = config.formatter; - }; - typos = { - enable = true; - excludes = [ "generated/*" ]; - }; - maintainers = { - enable = true; - name = "maintainers"; - description = "Check maintainers when it is modified."; - files = "^lib/maintainers[.]nix$"; - package = pkgs.nix; - entry = "nix build --no-link --print-build-logs"; - args = [ ".#checks.${system}.maintainers" ]; - pass_filenames = false; - }; - plugins-by-name = { - enable = true; - name = "plugins-by-name"; - description = "Check `plugins/by-name` when it's modified."; - files = "^(?:tests/test-sources/)?plugins/by-name/"; - package = pkgs.nix; - entry = "nix build --no-link --print-build-logs"; - args = [ ".#checks.${system}.plugins-by-name" ]; - pass_filenames = false; - }; - }; - }; - }; } diff --git a/flake/dev/fmt.nix b/flake/dev/fmt.nix new file mode 100644 index 00000000..e3733497 --- /dev/null +++ b/flake/dev/fmt.nix @@ -0,0 +1,69 @@ +{ inputs, ... }: +{ + imports = [ + inputs.treefmt-nix.flakeModule + ]; + + perSystem = + { config, pkgs, ... }: + { + ci.buildbot = { + inherit (config.checks) treefmt; + }; + + treefmt.config = { + projectRootFile = "flake.nix"; + flakeCheck = true; + + programs = { + # keep-sorted start block=yes newline_separated=no + isort.enable = true; + keep-sorted.enable = true; + nixfmt = { + enable = true; + package = pkgs.nixfmt-rfc-style; + }; + prettier = { + enable = true; + excludes = [ "**.md" ]; + }; + ruff = { + check = true; + format = true; + }; + shfmt.enable = true; + statix = { + enable = true; + disabled-lints = [ + # We often use `nullable == true` + "bool_comparison" + ]; + }; + stylua.enable = true; + # FIXME: re-enable on darwin, currently broken: taplo with options '[format]' failed to apply: exit status 101 + taplo.enable = pkgs.stdenv.isLinux; + # keep-sorted end + }; + + settings = { + global.excludes = [ + ".editorconfig" + ".envrc" + ".git-blame-ignore-revs" + ".gitignore" + "LICENSE" + "flake.lock" + "**.md" + "**.scm" + "**.svg" + "**/man/*.5" + # Those files are generated by pytest-regression, which then `diff`s them. + # Formatting them will make the tests fail. + "docs/gfm-alerts-to-admonitions/tests/**/*.yml" + ]; + formatter.ruff-format.options = [ "--isolated" ]; + }; + }; + + }; +} diff --git a/flake/dev/git-hooks.nix b/flake/dev/git-hooks.nix new file mode 100644 index 00000000..7dd4c0b0 --- /dev/null +++ b/flake/dev/git-hooks.nix @@ -0,0 +1,62 @@ +{ inputs, ... }: +{ + imports = [ + inputs.git-hooks.flakeModule + ]; + + perSystem = + { + config, + pkgs, + system, + ... + }: + { + pre-commit = { + # We have a treefmt check already, so this is redundant. + # We also can't run the test if it includes running `nix build`, + # since the nix CLI can't build within a derivation builder. + check.enable = false; + + settings.hooks = { + deadnix = { + enable = true; + + settings = { + noLambdaArg = true; + noLambdaPatternNames = true; + edit = true; + }; + }; + treefmt = { + enable = true; + package = config.formatter; + }; + typos = { + enable = true; + excludes = [ "generated/*" ]; + }; + maintainers = { + enable = true; + name = "maintainers"; + description = "Check maintainers when it is modified."; + files = "^lib/maintainers[.]nix$"; + package = pkgs.nix; + entry = "nix build --no-link --print-build-logs"; + args = [ ".#checks.${system}.maintainers" ]; + pass_filenames = false; + }; + plugins-by-name = { + enable = true; + name = "plugins-by-name"; + description = "Check `plugins/by-name` when it's modified."; + files = "^(?:tests/test-sources/)?plugins/by-name/"; + package = pkgs.nix; + entry = "nix build --no-link --print-build-logs"; + args = [ ".#checks.${system}.plugins-by-name" ]; + pass_filenames = false; + }; + }; + }; + }; +}