Skip to content

Commit

Permalink
feat: adds simplified generator for pre-commit configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed May 27, 2022
1 parent 66a4430 commit db5403f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
21 changes: 6 additions & 15 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@
plugins = self.plugins.${system};

preCommitConfig = {
repos = [
{
repo = "local";
hooks = [
{
id = "nixpkgs-fmt";
name = "nixpkgs-fmt";
entry = "${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt";
language = "system";
files = "\\.nix";
}
];
}
];
nixpkgs-fmt = {
entry = "${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt";
language = "system";
files = "\\.nix";
};
};
preCommit = plugins.pre-commit.mkConfig { config = preCommitConfig; };
preCommit = plugins.pre-commit.mkLocalConfig { config = preCommitConfig; };

justConfig = {
tasks = {
Expand Down
19 changes: 19 additions & 0 deletions plugins/pre-commit/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ pre-commit, stagesStr }:
{
shellHookExtra = ''
# Uninstall all existing hooks
hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
for hook in $hooks; do
${pre-commit}/bin/pre-commit uninstall -t $hook
done
# Install configured hooks
for stage in ${stagesStr}; do
if [[ "$stage" == "manual" ]]; then
continue
fi
${pre-commit}/bin/pre-commit install -t "$stage"
done
'';
}
1 change: 1 addition & 0 deletions plugins/pre-commit/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ pkgs, lib }:
{
mkConfig = import ./make.nix { inherit pkgs lib; };
mkLocalConfig = import ./make_local.nix { inherit pkgs lib; };
}
17 changes: 1 addition & 16 deletions plugins/pre-commit/make.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,7 @@ let
stagesStr = builtins.concatStringsSep " " stages;

# Add an extra hook for reinstalling required stages whenever the file changes
shellHookExtra = ''
# Uninstall all existing hooks
hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
for hook in $hooks; do
${pre-commit}/bin/pre-commit uninstall -t $hook
done
# Install configured hooks
for stage in ${stagesStr}; do
if [[ "$stage" == "manual" ]]; then
continue
fi
${pre-commit}/bin/pre-commit install -t "$stage"
done
'';
shellHookExtra = (import ./common.nix { inherit pre-commit stagesStr; }).shellHookExtra;

# Generate the module
result = lib.common.mkTemplate {
Expand Down
36 changes: 36 additions & 0 deletions plugins/pre-commit/make_local.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ pkgs, lib }:
{ config, pre-commit ? pkgs.pre-commit, jq ? pkgs.jq, yq ? pkgs.yq-go }:
with pkgs.lib;
let
# Find all stages that require installation
stages = unique (flatten (builtins.map (hook: optionals (hook ? stages) hook.stages) (attrValues config)));
stagesStr = builtins.concatStringsSep " " stages;

# Add an extra hook for reinstalling required stages whenever the file changes
shellHookExtra = (import ./common.nix { inherit pre-commit stagesStr; }).shellHookExtra;

# Add structure to config
hooks = attrValues (pkgs.lib.mapAttrs
(id: hook: (
{ inherit id; name = if (hook ? name) then hook.name else id; } // hook
))
config);
data = {
repos = [
{
inherit hooks;
repo = "local";
}
];
};

# Generate the module
result = lib.common.mkTemplate {
inherit data shellHookExtra;
files = [ ./template.cue ];
output = ".pre-commit-config.yaml";
};
in
{
inherit (result) configFile shellHook;
}

0 comments on commit db5403f

Please sign in to comment.