From 8d117a74d6d88bdca212fd95241f7a086d109b54 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Thu, 22 May 2025 19:42:47 +1000 Subject: [PATCH] chore: check commit messages --- flake.nix | 102 +-------------------------- nix/git-hooks.nix | 74 +++++++++++++++++++ nix/pkgs/conventional-pre-commit.nix | 20 ++++++ nix/shell.nix | 39 ++++++++++ 4 files changed, 135 insertions(+), 100 deletions(-) create mode 100644 nix/git-hooks.nix create mode 100644 nix/pkgs/conventional-pre-commit.nix create mode 100644 nix/shell.nix diff --git a/flake.nix b/flake.nix index 1247dc7..d3dd6f0 100644 --- a/flake.nix +++ b/flake.nix @@ -32,106 +32,8 @@ }; in { - checks = { - pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - # TOML - check-toml.enable = true; - - # YAML - check-yaml.enable = true; - - # Nix - deadnix.enable = true; - flake-checker.enable = true; - nixpkgs-fmt.enable = true; - statix.enable = true; - - # Rust - clippy = { - enable = true; - settings = { - denyWarnings = true; - }; - extraPackages = with pkgs; [ - openssl - pkg-config - ]; - }; - rustfmt.enable = true; - cargo-check = { - enable = true; - extraPackages = with pkgs; [ - pkg-config - openssl - ]; - }; - - # SQL - sqlfluff = { - enable = true; - name = "sqlfluff"; - entry = "${pkgs.sqlfluff}/bin/sqlfluff lint --dialect sqlite"; - files = "\\.sql$"; - language = "system"; - }; - - # Git - no-commit-to-branch = { - enable = true; - settings = { - branch = [ "main" ]; - }; - }; - }; - - settings = { - rust.check.cargoDeps = pkgs.rustPlatform.importCargoLock { - lockFile = ./Cargo.lock; - }; - }; - }; - }; - - devShells = { - default = pkgs.mkShell { - name = "reddit-magnet"; - buildInputs = with pkgs; [ - cargo - cargo-edit - cargo-insta - cargo-machete - cargo-release - cargo-sort - diesel-cli - git-cliff - openssl - pkg-config - rustc - rust-toolchain - sqlfluff - sqlite - ] ++ lib.optionals stdenv.isDarwin [ - libiconv - ] ++ self.checks.${system}.pre-commit-check.enabledPackages; - RUST_BACKTRACE = 1; - - # Copy rust-toolchain to project directory for easy use in IntelliJ - shellHook = '' - if [ -L ./.rust-toolchain ] && [ "$(readlink ./.rust-toolchain)" = "${rust-toolchain}" ]; then - echo "Rust toolchain symlink is already correct." - else - rm -f ./.rust-toolchain - ln -s ${rust-toolchain} ./.rust-toolchain - echo "Rust toolchain symlink updated." - fi - - ${self.checks.${system}.pre-commit-check.shellHook} - ''; - }; - }; - + checks = import ./nix/git-hooks.nix { inherit inputs pkgs; }; + devShells = import ./nix/shell.nix { inherit pkgs rust-toolchain self; }; formatter = pkgs.nixpkgs-fmt; }); } diff --git a/nix/git-hooks.nix b/nix/git-hooks.nix new file mode 100644 index 0000000..da41708 --- /dev/null +++ b/nix/git-hooks.nix @@ -0,0 +1,74 @@ +{ inputs, pkgs, ... }: +let + conventional-commit = pkgs.callPackage ./pkgs/conventional-pre-commit.nix { }; +in +{ + pre-commit-check = inputs.pre-commit-hooks.lib.${pkgs.system}.run { + src = ./..; + hooks = { + # TOML + check-toml.enable = true; + + # YAML + check-yaml.enable = true; + + # Nix + deadnix.enable = true; + flake-checker.enable = true; + nixpkgs-fmt.enable = true; + statix.enable = true; + + # Rust + clippy = { + enable = true; + settings = { + denyWarnings = true; + }; + extraPackages = with pkgs; [ + openssl + pkg-config + ]; + }; + rustfmt.enable = true; + cargo-check = { + enable = true; + extraPackages = with pkgs; [ + pkg-config + openssl + ]; + }; + + # SQL + sqlfluff = { + enable = true; + name = "sqlfluff"; + entry = "${pkgs.sqlfluff}/bin/sqlfluff lint --dialect sqlite"; + files = "\\.sql$"; + language = "system"; + }; + + # Git + no-commit-to-branch = { + enable = true; + settings = { + branch = [ "main" ]; + }; + }; + conventional-commit = { + enable = true; + name = "conventional-commit"; + description = "A pre-commit hook that checks commit messages for Conventional Commits formatting"; + package = conventional-commit; + entry = "${conventional-commit}/bin/conventional-pre-commit"; + args = [ "--strict" "feat" "fix" "chore" "revert" "style" "docs" "build" "refactor" "test" "ci" "perf" ]; + stages = [ "commit-msg" ]; + }; + }; + + settings = { + rust.check.cargoDeps = pkgs.rustPlatform.importCargoLock { + lockFile = ../Cargo.lock; + }; + }; + }; +} diff --git a/nix/pkgs/conventional-pre-commit.nix b/nix/pkgs/conventional-pre-commit.nix new file mode 100644 index 0000000..dbeb136 --- /dev/null +++ b/nix/pkgs/conventional-pre-commit.nix @@ -0,0 +1,20 @@ +{ python3Packages, fetchPypi, ... }: + +python3Packages.buildPythonApplication rec { + pname = "conventional_pre_commit"; + version = "4.2.0"; + + src = fetchPypi { + inherit pname version; + hash = "sha256-a1ooZzOMWKHRTTAN5otWwXt8hAO7EiFV84Y5pCPSH/E="; + }; + + doCheck = false; + pyproject = true; + + build-system = with python3Packages; [ + setuptools + setuptools-scm + wheel + ]; +} diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 0000000..4442aa3 --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,39 @@ +{ pkgs, rust-toolchain, self, ... }: + +{ + default = pkgs.mkShell { + name = "reddit-magnet"; + buildInputs = with pkgs; [ + cargo + cargo-edit + cargo-insta + cargo-machete + cargo-release + cargo-sort + diesel-cli + git-cliff + openssl + pkg-config + rustc + rust-toolchain + sqlfluff + sqlite + ] ++ lib.optionals stdenv.isDarwin [ + libiconv + ] ++ self.checks.${pkgs.system}.pre-commit-check.enabledPackages; + RUST_BACKTRACE = 1; + + # Copy rust-toolchain to project directory for easy use in IntelliJ + shellHook = '' + if [ -L ./.rust-toolchain ] && [ "$(readlink ./.rust-toolchain)" = "${rust-toolchain}" ]; then + echo "Rust toolchain symlink is already correct." + else + rm -f ./.rust-toolchain + ln -s ${rust-toolchain} ./.rust-toolchain + echo "Rust toolchain symlink updated." + fi + + ${self.checks.${pkgs.system}.pre-commit-check.shellHook} + ''; + }; +}