Skip to content

dev: modularize hooks #593

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

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
4,248 changes: 218 additions & 4,030 deletions modules/hooks.nix

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions modules/hooks/actionlint.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ config, tools, lib, ... }:
{
config = {
files = "^.github/workflows/";
types = [ "yaml" ];
package = tools.actionlint;
entry = "${config.package}/bin/actionlint";
};
}
53 changes: 53 additions & 0 deletions modules/hooks/alejandra.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{ tools, config, lib, mkCmdArgs, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
check =
mkOption {
type = types.bool;
description = "Check if the input is already formatted and disable writing in-place the modified content";
default = false;
example = true;
};
exclude =
mkOption {
type = types.listOf types.str;
description = "Files or directories to exclude from formatting.";
default = [ ];
example = [ "flake.nix" "./templates" ];
};
threads =
mkOption {
type = types.nullOr types.int;
description = "Number of formatting threads to spawn.";
default = null;
example = 8;
};
verbosity =
mkOption {
type = types.enum [ "normal" "quiet" "silent" ];
description = "Whether informational messages or all messages should be hidden or not.";
default = "normal";
example = "quiet";
};
};

config = {
package = tools.alejandra;
entry =
let
cmdArgs =
mkCmdArgs (with config.settings; [
[ check "--check" ]
[ (exclude != [ ]) "--exclude ${lib.strings.concatStringsSep " --exclude " (map lib.escapeShellArg (lib.unique exclude))}" ]
[ (verbosity == "quiet") "-q" ]
[ (verbosity == "silent") "-qq" ]
[ (threads != null) "--threads ${toString threads}" ]
]);
in
"${config.package}/bin/alejandra ${cmdArgs}";
files = "\\.nix$";
};
}
7 changes: 7 additions & 0 deletions modules/hooks/annex.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ config, tools, lib, ... }:
{
config = {
package = tools.git-annex;
entry = "${config.package}/bin/git-annex pre-commit";
};
}
33 changes: 33 additions & 0 deletions modules/hooks/ansible-lint.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{ tools, config, lib, mkCmdArgs, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
configPath = mkOption {
type = types.str;
description = "Path to the YAML configuration file.";
# an empty string translates to use default configuration of the
# underlying ansible-lint binary
default = "";
};
subdir = mkOption {
type = types.str;
description = "Path to the Ansible subdirectory.";
default = "";
};
};

config = {
package = tools.ansible-lint;
entry =
let
cmdArgs =
mkCmdArgs [
[ (config.settings.configPath != "") "-c ${config.settings.configPath}" ]
];
in
"${config.package}/bin/ansible-lint ${cmdArgs}";
files = if config.settings.subdir != "" then "${config.settings.subdir}/" else "";
};
}
34 changes: 34 additions & 0 deletions modules/hooks/autoflake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{ tools, config, lib, migrateBinPathToPackage, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
binPath =
mkOption {
type = types.nullOr types.str;
description = "Path to autoflake binary.";
default = null;
defaultText = lib.literalExpression ''
"''${config.package}/bin/autoflake"
'';
};

flags =
mkOption {
type = types.str;
description = "Flags passed to autoflake.";
default = "--in-place --expand-star-imports --remove-duplicate-keys --remove-unused-variables";
};
};

config = {
package = tools.autoflake;
entry =
let
binPath = migrateBinPathToPackage config "/bin/autoflake";
in
"${binPath} ${config.settings.flags}";
types = [ "python" ];
};
}
9 changes: 9 additions & 0 deletions modules/hooks/bats.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ tools, lib, config, ... }:
{
config = {
types = [ "shell" ];
types_or = [ "bats" "bash" ];
package = tools.bats;
entry = "${config.package}/bin/bats -p";
};
}
8 changes: 8 additions & 0 deletions modules/hooks/beautysh.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
types = [ "shell" ];
package = tools.beautysh;
entry = "${config.package}/bin/beautysh";
};
}
54 changes: 54 additions & 0 deletions modules/hooks/biome.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{ tools, config, lib, mkCmdArgs, migrateBinPathToPackage, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
binPath =
mkOption {
type = types.nullOr (types.oneOf [ types.str types.path ]);
description = ''
`biome` binary path.
For example, if you want to use the `biome` binary from `node_modules`, use `"./node_modules/.bin/biome"`.
Use a string instead of a path to avoid having to Git track the file in projects that use Nix flakes.
'';
default = null;
defaultText = lib.literalExpression ''
"''${config.package}/bin/biome"
'';
example = lib.literalExpression ''
"./node_modules/.bin/biome"
'';
};

write =
mkOption {
type = types.bool;
description = "Whether to edit files inplace.";
default = true;
};

configPath = mkOption {
type = types.str;
description = "Path to the configuration JSON file";
# an empty string translates to use default configuration of the
# underlying biome binary (i.e biome.json if exists)
default = "";
};
};

config = {
types_or = [ "javascript" "jsx" "ts" "tsx" "json" ];
package = tools.biome;
entry =
let
binPath = migrateBinPathToPackage config "/bin/biome";
cmdArgs =
mkCmdArgs [
[ (config.settings.write) "--write" ]
[ (config.settings.configPath != "") "--config-path ${config.settings.configPath}" ]
];
in
"${binPath} check ${cmdArgs}";
};
}
20 changes: 20 additions & 0 deletions modules/hooks/black.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ tools, config, lib, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
flags = mkOption {
type = types.str;
description = "Flags passed to black. See all available [here](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#command-line-options).";
default = "";
example = "--skip-magic-trailing-comma";
};
};

config = {
package = tools.black;
entry = "${config.package}/bin/black ${config.settings.flags}";
types = [ "file" "python" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/cabal-fmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.cabal-fmt;
entry = "${config.package}/bin/cabal-fmt --inplace";
files = "\\.cabal$";
};
}
16 changes: 16 additions & 0 deletions modules/hooks/cabal-gild.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ tools, config, lib, pkgs, ... }:
{
config = {
package = tools.cabal-gild;
entry =
let
script = pkgs.writeShellScript "precommit-cabal-gild" ''
for file in "$@"; do
${config.package}/bin/cabal-gild --io="$file"
done
'';
in
builtins.toString script;
files = "\\.cabal$";
};
}
21 changes: 21 additions & 0 deletions modules/hooks/cabal2nix.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{ tools, config, lib, ... }:
let
inherit (lib) mkOption types;
in
{
options.settings = {
outputFilename =
mkOption {
type = types.str;
description = "The name of the output file generated after running `cabal2nix`.";
default = "default.nix";
};
};

config = {
package = tools.cabal2nix-dir;
entry = "${config.package}/bin/cabal2nix-dir --outputFileName=${config.settings.outputFilename}";
files = "\\.cabal$";
after = [ "hpack" ];
};
}
16 changes: 16 additions & 0 deletions modules/hooks/cargo-check.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ tools, lib, settings, config, ... }:
let
inherit (settings.rust) cargoManifestPath;
cargoManifestPathArg =
lib.optionalString
(cargoManifestPath != null)
"--manifest-path ${lib.escapeShellArg cargoManifestPath}";
in
{
config = {
package = tools.cargo;
entry = "${config.package}/bin/cargo check ${cargoManifestPathArg}";
files = "\\.rs$";
pass_filenames = false;
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-added-large-files.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-added-large-files";
stages = [ "pre-commit" "pre-push" "manual" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-builtin-literals.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-builtin-literals";
types = [ "python" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-case-conflicts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-case-conflict";
types = [ "file" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-docstring-first.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-docstring-first";
types = [ "python" ];
};
}
9 changes: 9 additions & 0 deletions modules/hooks/check-executables-have-shebangs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-executables-have-shebangs";
types = [ "text" "executable" ];
stages = [ "pre-commit" "pre-push" "manual" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-json.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-json";
types = [ "json" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-merge-conflicts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-merge-conflict";
types = [ "text" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-python.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-ast";
types = [ "python" ];
};
}
9 changes: 9 additions & 0 deletions modules/hooks/check-shebang-scripts-are-executable.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ tools, lib, config, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-shebang-scripts-are-executable";
types = [ "text" ];
stages = [ "pre-commit" "pre-push" "manual" ];
};
}
8 changes: 8 additions & 0 deletions modules/hooks/check-symlinks.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ config, tools, lib, ... }:
{
config = {
package = tools.pre-commit-hooks;
entry = "${config.package}/bin/check-symlinks";
types = [ "symlink" ];
};
}
Loading