From 591df62e582dd0cfc2d081f1529f1579a2e62ea7 Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Mon, 28 Oct 2024 14:42:21 +0100 Subject: [PATCH] Add commit hook and flake check for formatting --- .envrc | 1 + .pre-commit-config.yaml | 1 + formatter.nix | 15 ++++++++++++++- shell.nix | 4 +++- 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .envrc create mode 120000 .pre-commit-config.yaml diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 120000 index 0000000..b104519 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1 @@ +/nix/store/aimf4r2pn4gapwcm87hbwx73r40cj89i-pre-commit-config.json \ No newline at end of file diff --git a/formatter.nix b/formatter.nix index 0d42f9c..3cef74a 100644 --- a/formatter.nix +++ b/formatter.nix @@ -1,14 +1,27 @@ { + self, nixpkgs, treefmt-nix, flake-utils, + pre-commit-hooks, ... }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = nixpkgs.legacyPackages.${system}; treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix; + treefmtWrapper = treefmtEval.config.build.wrapper; in { - formatter = treefmtEval.config.build.wrapper; + packages.formatter = treefmtWrapper; + formatter = self.packages.${system}.formatter; + checks.pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + treefmt = { + enable = true; + package = treefmtWrapper; + }; + }; + }; } ) diff --git a/shell.nix b/shell.nix index 072adef..261ea16 100644 --- a/shell.nix +++ b/shell.nix @@ -1,4 +1,5 @@ { + self, flake-utils, nixpkgs, ... @@ -7,6 +8,7 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; in { devShells.default = pkgs.mkShell { - buildInputs = with pkgs; [ansible]; + inherit (self.checks.${system}.pre-commit-check) shellHook; + buildInputs = self.checks.${system}.pre-commit-check.enabledPackages ++ [pkgs.ansible]; }; })