From 1c273cce3af81374730b2accb6ffa8335812abfd Mon Sep 17 00:00:00 2001 From: myypo Date: Sun, 18 Aug 2024 17:41:24 +0300 Subject: [PATCH] chore: add pre-commit hooks --- .gitignore | 2 + flake.lock | 127 ++++++++++++++++++++++++++++++++++++++--------------- flake.nix | 126 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 172 insertions(+), 83 deletions(-) diff --git a/.gitignore b/.gitignore index 0fbd120..e4c2846 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ target result +/.pre-commit-config.yaml + lua/* !lua/.gitkeep diff --git a/flake.lock b/flake.lock index 8000e34..84fca14 100644 --- a/flake.lock +++ b/flake.lock @@ -53,6 +53,22 @@ "type": "github" } }, + "flake-compat_3": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -95,24 +111,6 @@ "type": "indirect" } }, - "flake-utils": { - "inputs": { - "systems": "systems" - }, - "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, "git-hooks": { "inputs": { "flake-compat": "flake-compat_2", @@ -162,6 +160,27 @@ "type": "github" } }, + "gitignore_2": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "hercules-ci-effects": { "inputs": { "flake-parts": "flake-parts_2", @@ -240,12 +259,65 @@ "type": "indirect" } }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1720386169, + "narHash": "sha256-NGKVY4PjzwAa4upkGtAMz1npHGoRzWotlSnVlqI40mo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "194846768975b7ad2c4988bdb82572c00222c0d7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1719082008, + "narHash": "sha256-jHJSUH619zBQ6WdC21fFAlDxHErKVDJ5fpN0Hgx4sjs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9693852a2070b398ee123a329e68f0dab5526681", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat_3", + "gitignore": "gitignore_2", + "nixpkgs": "nixpkgs_2", + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1723803910, + "narHash": "sha256-yezvUuFiEnCFbGuwj/bQcqg7RykIEqudOy/RBrId0pc=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "bfef0ada09e2c8ac55bbcd0831bd0c9d42e651ba", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "fenix": "fenix", - "flake-utils": "flake-utils", "neovim-nightly-overlay": "neovim-nightly-overlay", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" } }, "rust-analyzer-src": { @@ -264,21 +336,6 @@ "repo": "rust-analyzer", "type": "github" } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 298e6a5..83574df 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,8 @@ { inputs = { nixpkgs.url = "nixpkgs/nixos-unstable"; - flake-utils.url = "github:numtide/flake-utils"; + + pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; neovim-nightly-overlay.url = "github:nix-community/neovim-nightly-overlay"; neovim-nightly-overlay.inputs.nixpkgs.follows = "nixpkgs"; @@ -11,61 +12,90 @@ }; outputs = inputs: - with inputs; - flake-utils.lib.eachDefaultSystem ( - system: let - overlays = [fenix.overlays.default neovim-nightly-overlay.overlays.default]; - pkgs = import nixpkgs { - inherit overlays system; - }; - in { - packages = with pkgs; { - default = rustPlatform.buildRustPackage { - name = "compass"; - src = lib.cleanSource ./.; - cargoLock = { - lockFile = ./Cargo.lock; - allowBuiltinFetchGit = true; - }; + with inputs; let + forEachSupportedSystem = let + supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"]; + in ( + f: + nixpkgs.lib.genAttrs supportedSystems ( + system: + f + (let + overlays = [fenix.overlays.default neovim-nightly-overlay.overlays.default]; + in + import nixpkgs {inherit overlays system;}) + ) + ); + in { + checks.pre-commit-check = forEachSupportedSystem (pkgs: + inputs.pre-commit-hooks.lib.${pkgs.system}.run { + src = ./.; + hooks = { + alejandra.enable = true; - doCheck = false; + rustfmt = { + enable = true; + packageOverrides.rustfmt = pkgs.fenix.complete.rustfmt; + }; + clippy = { + enable = true; + packageOverrides.cargo = pkgs.fenix.complete.cargo; + packageOverrides.clippy = pkgs.fenix.complete.clippy; + settings.allFeatures = true; + settings.denyWarnings = true; + }; + }; + }); - nativeBuildInputs = [ - pkg-config - rustPlatform.bindgenHook - ]; + packages = forEachSupportedSystem (pkgs: + with pkgs; { + default = rustPlatform.buildRustPackage { + name = "compass"; + src = lib.cleanSource ./.; + cargoLock = { + lockFile = ./Cargo.lock; }; + + doCheck = false; + + nativeBuildInputs = [ + pkg-config + rustPlatform.bindgenHook + ]; }; + }); - devShells = with pkgs; { - default = - mkShell.override { - stdenv = stdenvAdapters.useMoldLinker clangStdenv; - } - mkShell { - packages = [ - openssl - pkg-config + devShells = forEachSupportedSystem (pkgs: + with pkgs; { + default = + mkShell.override { + stdenv = stdenvAdapters.useMoldLinker clangStdenv; + } + { + inherit (self.checks.pre-commit-check.${pkgs.system}) shellHook; - neovim + packages = [ + openssl + pkg-config - rust-analyzer-nightly + neovim - rustPlatform.bindgenHook + rust-analyzer-nightly - (fenix.complete.withComponents [ - "cargo" - "clippy" - "rust-src" - "rust-std" - "rustc" - "rustfmt" - ]) + rustPlatform.bindgenHook - cargo-watch - ]; - }; - }; - } - ); + (fenix.complete.withComponents [ + "cargo" + "clippy" + "rust-src" + "rust-std" + "rustc" + "rustfmt" + ]) + + cargo-watch + ]; + }; + }); + }; }