diff --git a/.gitignore b/.gitignore index 089308c..3f4b997 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .direnv/ .idea/ /target +.rust-toolchain +.pre-commit-config.yaml diff --git a/flake.lock b/flake.lock index 36ef2ff..73bfdd0 100644 --- a/flake.lock +++ b/flake.lock @@ -1,15 +1,31 @@ { "nodes": { + "flake-compat": { + "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-utils": { "inputs": { "systems": "systems" }, "locked": { - "lastModified": 1726560853, - "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -18,13 +34,34 @@ "type": "github" } }, + "gitignore": { + "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" + } + }, "nixpkgs": { "locked": { - "lastModified": 1729413321, - "narHash": "sha256-I4tuhRpZFa6Fu6dcH9Dlo5LlH17peT79vx1y1SpeKt0=", + "lastModified": 1753694789, + "narHash": "sha256-cKgvtz6fKuK1Xr5LQW/zOUiAC0oSQoA9nOISB0pJZqM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "1997e4aa514312c1af7e2bda7fad1644e778ff26", + "rev": "dc9637876d0dcc8c9e5e22986b857632effeb727", "type": "github" }, "original": { @@ -34,10 +71,33 @@ "type": "github" } }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1750779888, + "narHash": "sha256-wibppH3g/E2lxU43ZQHC5yA/7kIKLGxVEnsnVK1BtRg=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "16ec914f6fb6f599ce988427d9d94efddf25fe6d", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" } }, "systems": { diff --git a/flake.nix b/flake.nix index 3d7a291..d3dd6f0 100644 --- a/flake.nix +++ b/flake.nix @@ -2,33 +2,38 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; + pre-commit-hooks = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = - { nixpkgs + { self + , nixpkgs , flake-utils , ... - }: + }@inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; - in - { - devShells.default = pkgs.mkShell { - name = "immich-tools"; - buildInputs = with pkgs; [ + # Place all toolchain components in a single directory for IntelliJ + rust-toolchain = pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc cargo - cargo-machete - cargo-release - cargo-sort - openssl - pkg-config + rustPlatform.rustcSrc ] ++ lib.optionals stdenv.isDarwin [ libiconv - darwin.apple_sdk.frameworks.SystemConfiguration ]; }; + in + { + 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..355da46 --- /dev/null +++ b/nix/git-hooks.nix @@ -0,0 +1,65 @@ +{ 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 + ]; + }; + + # 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..a620fe0 --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,36 @@ +{ pkgs, rust-toolchain, self, ... }: + +{ + default = pkgs.mkShell { + name = "immich-tools"; + buildInputs = with pkgs; [ + cargo + cargo-edit + cargo-insta + cargo-machete + cargo-release + cargo-sort + git-cliff + openssl + pkg-config + rustc + rust-toolchain + ] ++ 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} + ''; + }; +}