Skip to content

Commit fee04a7

Browse files
committed
Add packageInputs to allow overriding complex wrapper hooks
1 parent 6b40525 commit fee04a7

File tree

2 files changed

+76
-44
lines changed

2 files changed

+76
-44
lines changed

modules/hook.nix

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ in
4848
type = types.nullOr types.package;
4949
description = lib.mdDoc
5050
''
51-
An optional package that provides the hook's dependencies.
51+
The package that provides the hook.
52+
'';
53+
};
54+
55+
packageInputs = mkOption {
56+
type = types.submodule {
57+
freeformType = types.attrsOf types.package;
58+
};
59+
default = { };
60+
description = lib.mdDoc
61+
''
62+
Additional inputs required to construct the hook package.
5263
'';
5364
};
5465

modules/hooks.nix

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{ config, lib, pkgs, ... }:
22
let
3-
inherit (config) tools;
3+
inherit (config) hooks tools settings;
44
cfg = config;
5-
hooks = config.hooks;
6-
settings = config.settings;
75
inherit (lib) flatten mapAttrs mapAttrsToList mkDefault mkOption mkRenamedOptionModule types;
86

97
hookModule =
@@ -142,6 +140,16 @@ in
142140
description = lib.mdDoc "Additional clippy settings";
143141
type = types.submodule {
144142
imports = hookModule;
143+
options.packageInputs = {
144+
cargo = mkOption {
145+
type = types.package;
146+
description = lib.mdDoc "The cargo package to use for clippy";
147+
};
148+
clippy = mkOption {
149+
type = types.package;
150+
description = lib.mdDoc "The clippy package to use for clippy";
151+
};
152+
};
145153
options.settings = {
146154
denyWarnings = mkOption {
147155
type = types.bool;
@@ -1230,6 +1238,22 @@ in
12301238
};
12311239
};
12321240
};
1241+
rustfmt = mkOption {
1242+
description = lib.mdDoc "Additional rustfmt settings";
1243+
type = types.submodule {
1244+
imports = hookModule;
1245+
options.packageInputs = {
1246+
cargo = mkOption {
1247+
type = types.package;
1248+
description = lib.mdDoc "The cargo package to use.";
1249+
};
1250+
rustfmt = mkOption {
1251+
type = types.package;
1252+
description = lib.mdDoc "The rustfmt package to use.";
1253+
};
1254+
};
1255+
};
1256+
};
12331257
statix = mkOption {
12341258
description = lib.mdDoc "Additional statix settings";
12351259
type = types.submodule {
@@ -1252,41 +1276,18 @@ in
12521276
};
12531277
};
12541278
};
1255-
# TODO: should this be an option like `packages` or `formatters`?
1256-
# A list of packages to wrap in an env with treefmt.
1257-
# treefmt = mkOption {
1258-
# description = lib.mdDoc "Additional treefmt settings";
1259-
# type = types.submodule {
1260-
# imports = hookModule;
1261-
# options.settings = {
1262-
# package = mkOption {
1263-
# type = types.package;
1264-
# description = lib.mdDoc
1265-
# ''
1266-
# The `treefmt` package to use.
1267-
#
1268-
# Should include all the formatters configured by treefmt.
1269-
#
1270-
# For example:
1271-
# ```nix
1272-
# pkgs.writeShellApplication {
1273-
# name = "treefmt";
1274-
# runtimeInputs = [
1275-
# pkgs.treefmt
1276-
# pkgs.nixpkgs-fmt
1277-
# pkgs.black
1278-
# ];
1279-
# text =
1280-
# '''
1281-
# exec treefmt "$@"
1282-
# ''';
1283-
# }
1284-
# ```
1285-
# '';
1286-
# };
1287-
# };
1288-
# };
1289-
# };
1279+
treefmt = mkOption {
1280+
description = lib.mdDoc "Additional treefmt settings";
1281+
type = types.submodule {
1282+
imports = hookModule;
1283+
options.packageInputs = {
1284+
treefmt = mkOption {
1285+
type = types.package;
1286+
description = lib.mdDoc "The treefmt package to use.";
1287+
};
1288+
};
1289+
};
1290+
};
12901291
typos = mkOption {
12911292
description = lib.mdDoc "Additional typos settings";
12921293
type = types.submodule {
@@ -1640,20 +1641,22 @@ in
16401641
};
16411642
clippy =
16421643
let
1644+
inherit (hooks.clippy) packageInputs;
16431645
wrapper = pkgs.symlinkJoin {
16441646
name = "clippy-wrapped";
1645-
paths = [ tools.clippy ];
1647+
paths = [ packageInputs.clippy ];
16461648
nativeBuildInputs = [ pkgs.makeWrapper ];
16471649
postBuild = ''
16481650
wrapProgram $out/bin/cargo-clippy \
1649-
--prefix PATH : ${lib.makeBinPath [ tools.cargo ]}
1651+
--prefix PATH : ${lib.makeBinPath [ packageInputs.cargo ]}
16501652
'';
16511653
};
16521654
in
16531655
{
16541656
name = "clippy";
16551657
description = "Lint Rust code.";
16561658
package = wrapper;
1659+
packageInputs = { cargo = tools.cargo; clippy = tools.clippy; };
16571660
entry = "${hooks.clippy.package}/bin/cargo-clippy clippy ${cargoManifestPathArg} ${lib.optionalString hooks.clippy.settings.offline "--offline"} ${lib.optionalString hooks.clippy.settings.allFeatures "--all-features"} -- ${lib.optionalString hooks.clippy.settings.denyWarnings "-D warnings"}";
16581661
files = "\\.rs$";
16591662
pass_filenames = false;
@@ -2654,20 +2657,22 @@ in
26542657
};
26552658
rustfmt =
26562659
let
2660+
inherit (hooks.rustfmt) packageInputs;
26572661
wrapper = pkgs.symlinkJoin {
26582662
name = "rustfmt-wrapped";
2659-
paths = [ tools.rustfmt ];
2663+
paths = [ packageInputs.rustfmt ];
26602664
nativeBuildInputs = [ pkgs.makeWrapper ];
26612665
postBuild = ''
26622666
wrapProgram $out/bin/cargo-fmt \
2663-
--prefix PATH : ${lib.makeBinPath [ tools.cargo tools.rustfmt ]}
2667+
--prefix PATH : ${lib.makeBinPath [ packageInputs.cargo packageInputs.rustfmt ]}
26642668
'';
26652669
};
26662670
in
26672671
{
26682672
name = "rustfmt";
26692673
description = "Format Rust code.";
26702674
package = wrapper;
2675+
packageInputs = { cargo = tools.cargo; rustfmt = tools.rustfmt; };
26712676
entry = "${hooks.rustfmt.package}/bin/cargo-fmt fmt ${cargoManifestPathArg} -- --color always";
26722677
files = "\\.rs$";
26732678
pass_filenames = false;
@@ -2803,12 +2808,28 @@ in
28032808
files = "(\\.json$)|(\\.toml$)|(\\.mli?$)";
28042809
};
28052810
treefmt =
2811+
let
2812+
inherit (hooks.treefmt) packageInputs;
2813+
wrapper =
2814+
pkgs.writeShellApplication {
2815+
name = "treefmt";
2816+
runtimeInputs = [
2817+
packageInputs.treefmt
2818+
] ++ builtins.attrValues (builtins.removeAttrs packageInputs [ "treefmt" ]);
2819+
2820+
text =
2821+
''
2822+
exec treefmt "$@"
2823+
'';
2824+
};
2825+
in
28062826
{
28072827
name = "treefmt";
28082828
description = "One CLI to format the code tree.";
28092829
types = [ "file" ];
28102830
pass_filenames = true;
2811-
package = tools.treefmt;
2831+
package = wrapper;
2832+
packageInputs = { treefmt = tools.treefmt; };
28122833
entry = "${hooks.treefmt.package}/bin/treefmt --fail-on-change";
28132834
};
28142835
typos =

0 commit comments

Comments
 (0)