Skip to content

Commit

Permalink
Pass down default_states to each hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Feb 10, 2024
1 parent 21b2d51 commit 7482e4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 152 deletions.
5 changes: 2 additions & 3 deletions modules/hook.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{ config, name, lib, ... }:
{ config, name, lib, default_stages, ... }:

let
inherit (lib) concatStringsSep mkOption types;
cfg = config;
mergeExcludes =
excludes:
if excludes == [ ] then "^$" else "(${concatStringsSep "|" excludes})";
Expand Down Expand Up @@ -135,7 +134,7 @@ in
description = lib.mdDoc ''
Confines the hook to run at a particular stage.
'';
default = cfg.default_stages;
default = default_stages;
defaultText = (lib.literalExpression or lib.literalExample) "default_stages";
};

Expand Down
155 changes: 6 additions & 149 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,155 +17,12 @@ let
cfg = config;
install_stages = lib.unique (cfg.default_stages ++ (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks)));

hookType = types.submodule (
{ config, name, ... }: {
options = {
enable =
mkOption {
type = types.bool;
description = lib.mdDoc "Whether to enable this pre-commit hook.";
default = false;
};
raw =
mkOption {
type = types.attrsOf types.unspecified;
description = lib.mdDoc
''
Raw fields of a pre-commit hook. This is mostly for internal use but
exposed in case you need to work around something.
Default: taken from the other hook options.
'';
};
name =
mkOption {
type = types.str;
# default = name;
defaultText = lib.literalMD "internal name, same as `id`";
description = lib.mdDoc
''
The name of the hook - shown during hook execution.
'';
};
entry =
mkOption {
type = types.str;
description = lib.mdDoc
''
The entry point - the executable to run. {option}`entry` can also contain arguments that will not be overridden, such as `entry = "autopep8 -i";`.
'';
};
language =
mkOption {
type = types.str;
description = lib.mdDoc
''
The language of the hook - tells pre-commit how to install the hook.
'';
default = "system";
};
files =
mkOption {
type = types.str;
description = lib.mdDoc
''
The pattern of files to run on.
'';
default = "";
};
types =
mkOption {
type = types.listOf types.str;
description = lib.mdDoc
''
List of file types to run on. See [Filtering files with types](https://pre-commit.com/#plugins).
'';
default = [ "file" ];
};
types_or =
mkOption {
type = types.listOf types.str;
description = lib.mdDoc
''
List of file types to run on, where only a single type needs to match.
'';
default = [ ];
};
description =
mkOption {
type = types.str;
description = lib.mdDoc
''
Description of the hook. used for metadata purposes only.
'';
default = "";
};
excludes =
mkOption {
type = types.listOf types.str;
description = lib.mdDoc
''
Exclude files that were matched by these patterns.
'';
default = [ ];
};
pass_filenames =
mkOption {
type = types.bool;
description = lib.mdDoc ''
Whether to pass filenames as arguments to the entry point.
'';
default = true;
};
fail_fast = mkOption {
type = types.bool;
description = lib.mdDoc ''
if true pre-commit will stop running hooks if this hook fails.
'';
default = false;
};
require_serial = mkOption {
type = types.bool;
description = lib.mdDoc ''
if true this hook will execute using a single process instead of in parallel.
'';
default = false;
};
stages =
mkOption {
type = types.listOf types.str;
description = lib.mdDoc ''
Confines the hook to run at a particular stage.
'';
default = cfg.default_stages;
defaultText = (lib.literalExpression or lib.literalExample) "default_stages";
};
verbose = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
forces the output of the hook to be printed even when the hook passes.
'';
};
always_run = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
if true this hook will run even if there are no matching files.
'';
};
};
config =
{
raw =
{
inherit (config) name entry language files types types_or pass_filenames fail_fast require_serial stages verbose always_run;
id = config.name;
exclude = mergeExcludes config.excludes;
};
};
}
);
hookType = types.submodule {
imports = [
({ ... }: { _module.args.default_stages = cfg.default_stages; })
./hook.nix
];
};

mergeExcludes =
excludes:
Expand Down

0 comments on commit 7482e4a

Please sign in to comment.