From dc1ffda6986db8c2ef49e361a1dfb0e800f51083 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Fri, 12 Apr 2024 09:46:50 -0700 Subject: [PATCH 1/3] Add `extraPackages` option for packages propagated to `enabledPackages` This lets additional runtime packages like formatters for `treefmt` be propagated to `enabledPackages` for use in a developer environment. --- modules/hook.nix | 12 ++++++++++++ modules/hooks.nix | 36 ++++++++++++++++++++---------------- modules/pre-commit.nix | 9 ++++++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/modules/hook.nix b/modules/hook.nix index 9af067a8..f6737704 100644 --- a/modules/hook.nix +++ b/modules/hook.nix @@ -78,6 +78,18 @@ in ''; }; + extraPackages = mkOption { + type = types.listOf types.package; + default = [ ]; + description = lib.mdDoc + '' + Additional packages required to run the hook. + + These are propagated to `enabledPackages` for constructing developer + environments. + ''; + }; + entry = mkOption { type = types.str; description = lib.mdDoc diff --git a/modules/hooks.nix b/modules/hooks.nix index 9e64c0f0..0a2007e2 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -1399,22 +1399,26 @@ in hooks.treefmt.packageOverrides.treefmt = pkgs.treefmt; ``` ''; - type = types.submodule { - imports = [ hookModule ]; - options.packageOverrides = { - treefmt = mkOption { - type = types.package; - description = lib.mdDoc "The treefmt package to use"; - }; - }; - options.settings = { - formatters = mkOption { - type = types.listOf types.package; - description = lib.mdDoc "The formatter packages configured by treefmt"; - default = [ ]; - }; - }; - }; + type = types.submodule + ({ config, ... }: + { + imports = [ hookModule ]; + options.packageOverrides = { + treefmt = mkOption { + type = types.package; + description = lib.mdDoc "The treefmt package to use"; + }; + }; + options.settings = { + formatters = mkOption { + type = types.listOf types.package; + description = lib.mdDoc "The formatter packages configured by treefmt"; + default = [ ]; + }; + }; + + config.extraPackages = config.settings.formatters; + }); }; typos = mkOption { description = lib.mdDoc "typos hook"; diff --git a/modules/pre-commit.nix b/modules/pre-commit.nix index 7c068ec9..0234084c 100644 --- a/modules/pre-commit.nix +++ b/modules/pre-commit.nix @@ -138,7 +138,14 @@ in Useful for including into the developer environment. ''; - default = builtins.map (hook: hook.package) (lib.filter (hook: hook.enable && hook.package != null) (builtins.attrValues config.hooks)); + default = lib.pipe config.hooks [ + builtins.attrValues + (lib.filter (hook: hook.enable)) + (builtins.concatMap (hook: + (lib.optional (hook.package != null) hook.package) + ++ hook.extraPackages + )) + ]; }; hooks = From 00a90e7bbaffeeecc05a1c8ba814d9edeec84701 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Fri, 12 Apr 2024 09:54:29 -0700 Subject: [PATCH 2/3] Add `extraPackages` for `clippy`, `rustfmt`, `dune-fmt` --- modules/hooks.nix | 129 ++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 57 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 0a2007e2..7aba9be0 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -171,36 +171,42 @@ in }; clippy = mkOption { description = lib.mdDoc "clippy hook"; - type = types.submodule { - imports = [ hookModule ]; - options.packageOverrides = { - cargo = mkOption { - type = types.package; - description = lib.mdDoc "The cargo package to use"; - }; - clippy = mkOption { - type = types.package; - description = lib.mdDoc "The clippy package to use"; - }; - }; - options.settings = { - denyWarnings = mkOption { - type = types.bool; - description = lib.mdDoc "Fail when warnings are present"; - default = false; - }; - offline = mkOption { - type = types.bool; - description = lib.mdDoc "Run clippy offline"; - default = true; + type = types.submodule + ({ config, ... }: { + imports = [ hookModule ]; + options.packageOverrides = { + cargo = mkOption { + type = types.package; + description = lib.mdDoc "The cargo package to use"; + }; + clippy = mkOption { + type = types.package; + description = lib.mdDoc "The clippy package to use"; + }; }; - allFeatures = mkOption { - type = types.bool; - description = lib.mdDoc "Run clippy with --all-features"; - default = false; + options.settings = { + denyWarnings = mkOption { + type = types.bool; + description = lib.mdDoc "Fail when warnings are present"; + default = false; + }; + offline = mkOption { + type = types.bool; + description = lib.mdDoc "Run clippy offline"; + default = true; + }; + allFeatures = mkOption { + type = types.bool; + description = lib.mdDoc "Run clippy with --all-features"; + default = false; + }; }; - }; - }; + + config.extraPackages = [ + config.settings.packageOverrides.cargo + config.settings.packageOverrides.clippy + ]; + }); }; cmake-format = mkOption { description = lib.mdDoc "cmake-format hook"; @@ -333,24 +339,27 @@ in }; dune-fmt = mkOption { description = lib.mdDoc "dune-fmt hook"; - type = types.submodule { - imports = [ hookModule ]; - options.settings = { - auto-promote = - mkOption { - type = types.bool; - description = lib.mdDoc "Whether to auto-promote the changes."; - default = true; - }; + type = types.submodule + ({ config, ... }: { + imports = [ hookModule ]; + options.settings = { + auto-promote = + mkOption { + type = types.bool; + description = lib.mdDoc "Whether to auto-promote the changes."; + default = true; + }; - extraRuntimeInputs = - mkOption { - type = types.listOf types.package; - description = lib.mdDoc "Extra runtimeInputs to add to the environment, eg. `ocamlformat`."; - default = [ ]; - }; - }; - }; + extraRuntimeInputs = + mkOption { + type = types.listOf types.package; + description = lib.mdDoc "Extra runtimeInputs to add to the environment, eg. `ocamlformat`."; + default = [ ]; + }; + }; + + config.extraPackages = config.settings.extraRuntimeInputs; + }); }; eclint = mkOption { description = lib.mdDoc "eclint hook"; @@ -1324,19 +1333,25 @@ in hooks.rustfmt.packageOverrides.rustfmt = pkgs.rustfmt; ``` ''; - type = types.submodule { - imports = [ hookModule ]; - options.packageOverrides = { - cargo = mkOption { - type = types.package; - description = lib.mdDoc "The cargo package to use."; - }; - rustfmt = mkOption { - type = types.package; - description = lib.mdDoc "The rustfmt package to use."; + type = types.submodule + ({ config, ... }: { + imports = [ hookModule ]; + options.packageOverrides = { + cargo = mkOption { + type = types.package; + description = lib.mdDoc "The cargo package to use."; + }; + rustfmt = mkOption { + type = types.package; + description = lib.mdDoc "The rustfmt package to use."; + }; }; - }; - }; + + config.extraPackages = [ + config.settings.packageOverrides.cargo + config.settings.packageOverrides.rustfmt + ]; + }); }; statix = mkOption { description = lib.mdDoc "statix hook"; From bc5491d571291052763e34dde78155f59f70e0eb Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Wed, 17 Apr 2024 08:41:54 -0700 Subject: [PATCH 3/3] `config` -> `config.settings` Co-authored-by: sander --- modules/hooks.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 7aba9be0..2fa846b0 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -203,8 +203,8 @@ in }; config.extraPackages = [ - config.settings.packageOverrides.cargo - config.settings.packageOverrides.clippy + config.packageOverrides.cargo + config.packageOverrides.clippy ]; }); }; @@ -1348,8 +1348,8 @@ in }; config.extraPackages = [ - config.settings.packageOverrides.cargo - config.settings.packageOverrides.rustfmt + config.packageOverrides.cargo + config.packageOverrides.rustfmt ]; }); };