Add Git pre-commit hooks via Nix

This commit is contained in:
Lorenz Leutgeb 2025-04-22 14:25:00 +02:00
parent 77386b0fce
commit ce07e92a2e
No known key found for this signature in database
3 changed files with 116 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/radicle-cli/target
/build/artifacts
/build/*.tar.gz
/.pre-commit-config.yaml

View File

@ -31,6 +31,22 @@
"type": "github"
}
},
"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"
@ -49,7 +65,28 @@
"type": "github"
}
},
"nixpkgs": {
"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-stable": {
"locked": {
"lastModified": 1740142985,
"narHash": "sha256-fCPt2PVnQgiHXGlTRmU5/gFdG/+JT3ycpt+GxY1Vp+s=",
@ -65,12 +102,55 @@
"type": "github"
}
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1740142985,
"narHash": "sha256-fCPt2PVnQgiHXGlTRmU5/gFdG/+JT3ycpt+GxY1Vp+s=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1d1ce35bb6052a23f5692cd89b7cc49acdcb8d0e",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "release-24.11",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1742649964,
"narHash": "sha256-DwOTp7nvfi8mRfuL1escHDXabVXFGT1VlPD1JHrtrco=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "dcf5072734cb576d2b0c59b2ac44f5050b5eac82",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"advisory-db": "advisory-db",
"crane": "crane",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"nixpkgs": [
"nixpkgs-stable"
],
"nixpkgs-stable": "nixpkgs-stable",
"nixpkgs-unstable": "nixpkgs-unstable",
"pre-commit-hooks": "pre-commit-hooks",
"rust-overlay": "rust-overlay"
}
},

View File

@ -2,17 +2,20 @@
description = "Radicle";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-24.11";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/release-24.11";
nixpkgs-stable.url = "github:NixOS/nixpkgs/release-24.11";
nixpkgs.follows = "nixpkgs-stable";
crane = {
url = "github:ipetkov/crane";
crane.url = "github:ipetkov/crane";
pre-commit-hooks = {
url = "github:cachix/git-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
@ -35,7 +38,7 @@
advisory-db,
rust-overlay,
...
}:
} @ inputs:
flake-utils.lib.eachDefaultSystem (system: let
lib = nixpkgs.lib;
pkgs = import nixpkgs {
@ -43,8 +46,8 @@
overlays = [(import rust-overlay)];
};
rustToolChain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolChain;
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
srcFilters = path: type:
builtins.any (suffix: lib.hasSuffix suffix path) [
@ -105,6 +108,23 @@
# Set of checks that are run: `nix flake check`
checks = {
pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
src = ./.;
settings.rust.check.cargoDeps = pkgs.rustPlatform.importCargoLock {lockFile = ./Cargo.lock;};
hooks = {
alejandra.enable = true;
rustfmt.enable = true;
cargo-check.enable = true;
clippy = {
enable = true;
settings.denyWarnings = true;
packageOverrides.cargo = rustToolchain;
packageOverrides.clippy = rustToolchain;
};
shellcheck.enable = true;
};
};
# Build the crate as part of `nix flake check` for convenience
inherit (self.packages.${system}) radicle;
@ -230,6 +250,9 @@
};
devShells.default = craneLib.devShell {
inherit (self.checks.${system}.pre-commit-check) shellHook;
buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = with pkgs; [
cargo-audit
@ -241,7 +264,7 @@
sqlite
];
env.RUST_SRC_PATH = "${rustToolChain}/lib/rustlib/src/rust/library";
env.RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
};
});
}