Skip to content

Commit

Permalink
Add packageInputs to allow overriding complex wrapper hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Mar 15, 2024
1 parent 6b40525 commit acdf40c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
13 changes: 12 additions & 1 deletion modules/hook.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ in
type = types.nullOr types.package;
description = lib.mdDoc
''
An optional package that provides the hook's dependencies.
The package that provides the hook.
'';
};

packageInputs = mkOption {
type = types.submodule {
freeformType = types.lazyAttrsOf types.package;
};
default = { };
description = lib.mdDoc
''
Additional inputs required to construct the hook package.
'';
};

Expand Down
81 changes: 38 additions & 43 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{ config, lib, pkgs, ... }:
let
inherit (config) tools;
inherit (config) hooks tools settings;
cfg = config;
hooks = config.hooks;
settings = config.settings;
inherit (lib) flatten mapAttrs mapAttrsToList mkDefault mkOption mkRenamedOptionModule types;

hookModule =
Expand Down Expand Up @@ -1252,41 +1250,18 @@ in
};
};
};
# TODO: should this be an option like `packages` or `formatters`?
# A list of packages to wrap in an env with treefmt.
# treefmt = mkOption {
# description = lib.mdDoc "Additional treefmt settings";
# type = types.submodule {
# imports = hookModule;
# options.settings = {
# package = mkOption {
# type = types.package;
# description = lib.mdDoc
# ''
# The `treefmt` package to use.
#
# Should include all the formatters configured by treefmt.
#
# For example:
# ```nix
# pkgs.writeShellApplication {
# name = "treefmt";
# runtimeInputs = [
# pkgs.treefmt
# pkgs.nixpkgs-fmt
# pkgs.black
# ];
# text =
# '''
# exec treefmt "$@"
# ''';
# }
# ```
# '';
# };
# };
# };
# };
treefmt = mkOption {
description = lib.mdDoc "Additional treefmt settings";
type = types.submodule {
imports = hookModule;
options.packageInputs = {
treefmt = mkOption {
type = types.package;
description = lib.mdDoc "The treefmt package to use.";
};
};
};
};
typos = mkOption {
description = lib.mdDoc "Additional typos settings";
type = types.submodule {
Expand Down Expand Up @@ -1640,20 +1615,22 @@ in
};
clippy =
let
inherit (hooks.clippy) packageInputs;
wrapper = pkgs.symlinkJoin {
name = "clippy-wrapped";
paths = [ tools.clippy ];
paths = [ packageInputs.clippy ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/cargo-clippy \
--prefix PATH : ${lib.makeBinPath [ tools.cargo ]}
--prefix PATH : ${lib.makeBinPath [ packageInputs.cargo ]}
'';
};
in
{
name = "clippy";
description = "Lint Rust code.";
package = wrapper;
packageInputs = { cargo = tools.cargo; clippy = tools.clippy; };
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"}";
files = "\\.rs$";
pass_filenames = false;
Expand Down Expand Up @@ -2654,20 +2631,22 @@ in
};
rustfmt =
let
inherit (hooks.rustfmt) packageInputs;
wrapper = pkgs.symlinkJoin {
name = "rustfmt-wrapped";
paths = [ tools.rustfmt ];
paths = [ packageInputs.rustfmt ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/cargo-fmt \
--prefix PATH : ${lib.makeBinPath [ tools.cargo tools.rustfmt ]}
--prefix PATH : ${lib.makeBinPath [ packageInputs.cargo packageInputs.rustfmt ]}
'';
};
in
{
name = "rustfmt";
description = "Format Rust code.";
package = wrapper;
packageInputs = { cargo = tools.cargo; rustfmt = tools.rustfmt; };
entry = "${hooks.rustfmt.package}/bin/cargo-fmt fmt ${cargoManifestPathArg} -- --color always";
files = "\\.rs$";
pass_filenames = false;
Expand Down Expand Up @@ -2803,12 +2782,28 @@ in
files = "(\\.json$)|(\\.toml$)|(\\.mli?$)";
};
treefmt =
let
inherit (hooks.treefmt) packageInputs;
wrapper =
pkgs.writeShellApplication {
name = "treefmt";
runtimeInputs = [
packageInputs.treefmt
] ++ builtins.attrValues (builtins.removeAttrs packageInputs [ "treefmt" ]);

text =
''
exec treefmt "$@"
'';
};
in
{
name = "treefmt";
description = "One CLI to format the code tree.";
types = [ "file" ];
pass_filenames = true;
package = tools.treefmt;
package = wrapper;
packageInputs = { treefmt = tools.treefmt; };
entry = "${hooks.treefmt.package}/bin/treefmt --fail-on-change";
};
typos =
Expand Down

0 comments on commit acdf40c

Please sign in to comment.