Merge branch 'checks' into 'main'

chore: set up pre-commit checks

See merge request kernald/reddit-magnet!29
This commit is contained in:
Marc Plano-Lesay 2025-05-04 12:29:06 +00:00
commit ed13d54ba1
5 changed files with 176 additions and 43 deletions

5
.gitignore vendored
View file

@ -2,7 +2,4 @@
.idea/
/target
.rust-toolchain
*.act
*.bmp
*.jpg
*.png
.pre-commit-config.yaml

View file

@ -18,6 +18,28 @@ tests:
- cargo fmt --check
- cargo test
flake-check:
stage: test
image: nixpkgs/nix-flakes:nixos-24.11-x86_64-linux
before_script:
- mkdir -p ~/.ssh
- eval $(ssh-agent -s)
- chmod 400 "$NIX_SSH_PRIVATE_KEY"
- ssh-add "$NIX_SSH_PRIVATE_KEY"
- cp "$SSH_KNOWN_HOSTS" ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- echo "substituters = ssh://nix-ssh@build.enoent.fr?priority=10 https://cache.nixos.org?priority=40 https://cuda-maintainers.cachix.org https://nix-community.cachix.org https://statix.cachix.org https://pre-commit-hooks.cachix.org" >> /etc/nix/nix.conf
- echo "trusted-substituters = ssh://nix-ssh@build.enoent.fr" >> /etc/nix/nix.conf
- echo "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cuda-maintainers.cachix.org-1:0dq3bujKpuEPMCX6U4WylrUDZ9JyUG0VpVZa7CNfq5E= nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= statix.cachix.org-1:Z9E/g1YjCjU117QOOt07OjhljCoRZddiAm4VVESvais= pre-commit-hooks.cachix.org-1:Pkk3Panw5AW24TOv6kz3PvLhlH8puAsJTBbOPmBo7Rc=" >> /etc/nix/nix.conf
- echo "extra-trusted-public-keys = galatea-0:E7F1FzfoY+X9l9Xn9AvNWDkW1U7H644FHDzX3lpxHkI=" >> /etc/nix/nix.conf
- echo "builders-use-substitutes = true" >> /etc/nix/nix.conf
- echo "max-jobs = 0" >> /etc/nix/nix.conf
- echo "ssh://nix-ssh@build.enoent.fr x86_64-linux" >> /etc/nix/machines
- echo "ssh://nix-ssh@build.enoent.fr aarch64-linux" >> /etc/nix/machines
- nix store info --store ssh://nix-ssh@build.enoent.fr
script:
- nix flake check
build:amd64:
stage: build
image: rust

62
flake.lock generated
View file

@ -1,5 +1,21 @@
{
"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"
@ -18,6 +34,27 @@
"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": 1744932701,
@ -34,10 +71,33 @@
"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": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
}
},
"systems": {

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;
});
}

View file

@ -1,9 +1,9 @@
CREATE TABLE magnets
(
id INTEGER PRIMARY KEY,
title VARCHAR NOT NULL,
submitter VARCHAR NOT NULL,
subreddit VARCHAR NOT NULL,
link VARCHAR NOT NULL,
id INTEGER PRIMARY KEY,
title VARCHAR NOT NULL,
submitter VARCHAR NOT NULL,
subreddit VARCHAR NOT NULL,
link VARCHAR NOT NULL,
published_at DATETIME NOT NULL
)
)