Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lefthook: run directly instead of (default) complex script #374

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/data/configs/lefthook.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
let
inherit (inputs) nixpkgs;
inherit (inputs.nixpkgs) lib;
in {
data = {
commit-msg = {
commands = {
conform = {
# allow WIP, fixup!/squash! commits locally
run = ''
[[ "$(head -n 1 {1})" =~ ^WIP(:.*)?$|^wip(:.*)?$|fixup\!.*|squash\!.* ]] ||
conform enforce --commit-msg-file {1}'';
${lib.getExe nixpkgs.conform} enforce --commit-msg-file {1}'';
skip = ["merge" "rebase"];
};
};
};
pre-commit = {
commands = {
treefmt = {
run = "treefmt --fail-on-change {staged_files}";
run = "${lib.getExe nixpkgs.treefmt} --fail-on-change {staged_files}";
skip = ["merge" "rebase"];
};
};
Expand Down
10 changes: 5 additions & 5 deletions src/data/configs/treefmt.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let
inherit (inputs) nixpkgs;
inherit (inputs.nixpkgs) nodePackages;
inherit (inputs.nixpkgs) lib;
in {
packages = [
nixpkgs.alejandra
Expand All @@ -12,12 +12,12 @@ in {
data = {
formatter = {
nix = {
command = "alejandra";
command = lib.getExe nixpkgs.alejandra;
includes = ["*.nix"];
};
prettier = {
command = "prettier";
options = ["--plugin" "${nodePackages.prettier-plugin-toml}/lib/node_modules/prettier-plugin-toml/lib/api.js" "--write"];
command = lib.getExe nixpkgs.nodePackages.prettier;
options = ["--plugin" "${nixpkgs.nodePackages.prettier-plugin-toml}/lib/node_modules/prettier-plugin-toml/lib/api.js" "--write"];
includes = [
"*.css"
"*.html"
Expand All @@ -33,7 +33,7 @@ in {
];
};
shell = {
command = "shfmt";
command = lib.getExe nixpkgs.shfmt;
options = ["-i" "2" "-s" "-w"];
includes = ["*.sh"];
};
Expand Down
43 changes: 26 additions & 17 deletions src/lib/cfg/lefthook.nix
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
let
inherit (inputs) nixpkgs;
l = nixpkgs.lib // builtins;
in {
data = {};
format = "yaml";
output = "lefthook.yml";
packages = [nixpkgs.lefthook];
hook.extra = d: let
# Add an extra hook for adding required stages whenever the file changes
skip_attrs = [
lib = nixpkgs.lib // builtins;

mkScript = stage:
nixpkgs.writeScript "lefthook-${stage}" ''
#!${nixpkgs.runtimeShell}
[ "$LEFTHOOK" == "0" ] || ${lib.getExe nixpkgs.lefthook} run "${stage}" "$@"
'';

toStagesConfig = config:
lib.removeAttrs config [
"colors"
"extends"
"skip_output"
"source_dir"
"source_dir_local"
];
stages = l.attrNames (l.removeAttrs d skip_attrs);
stagesStr = l.concatStringsSep " " stages;
in ''
# Install configured hooks
for stage in ${stagesStr}; do
${l.getExe nixpkgs.lefthook} add -f "$stage"
done
'';
in {
data = {};
format = "yaml";
output = "lefthook.yml";
packages = [nixpkgs.lefthook];
# Add an extra hook for adding required stages whenever the file changes
hook.extra = config:
lib.pipe config [
toStagesConfig
lib.attrNames
(lib.map (stage: ''ln -sf "${mkScript stage}" ".git/hooks/${stage}"''))
(stages:
lib.optional (stages != []) "mkdir -p .git/hooks"
++ stages)
(lib.concatStringsSep "\n")
];
}
Loading