chore: set up pre-commit checks

This commit is contained in:
Marc Plano-Lesay 2025-05-04 21:46:13 +10:00
parent a0ffa68948
commit b10f83ea27
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
5 changed files with 176 additions and 43 deletions

118
flake.nix
View file

@ -2,13 +2,18 @@
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 {
@ -28,37 +33,86 @@
};
in
{
devShells.default = pkgs.mkShell {
name = "reddit-magnet";
buildInputs = with pkgs; [
cargo
cargo-edit
cargo-machete
cargo-release
cargo-sort
diesel-cli
git-cliff
openssl
pkg-config
rustc
rust-toolchain
sqlite
] ++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.SystemConfiguration
];
RUST_BACKTRACE = 1;
checks = {
pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
# TOML
check-toml.enable = true;
# 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
'';
# 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
];
};
# 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-machete
cargo-release
cargo-sort
diesel-cli
git-cliff
openssl
pkg-config
rustc
rust-toolchain
sqlfluff
sqlite
] ++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.SystemConfiguration
] ++ 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}
'';
};
};
formatter = pkgs.nixpkgs-fmt;
});
}