Skip to content

Commit d665a44

Browse files
authored
Export the package to PATH in shellHook (#640)
* pre-commit: expose `shellHook` with the pre-commit package in PATH * lint
1 parent 1faaa15 commit d665a44

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

flake-module.nix

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ in
3737
Nixpkgs to use in the pre-commit [`settings`](#opt-perSystem.pre-commit.settings).
3838
'';
3939
default = pkgs;
40-
defaultText = lib.literalMD "`pkgs` (module argument)";
40+
defaultText = lib.literalExpression "`pkgs` (module argument)";
4141
};
4242
settings = mkOption {
4343
type = types.submoduleWith {
@@ -49,16 +49,23 @@ in
4949
The git-hooks.nix configuration.
5050
'';
5151
};
52+
shellHook = mkOption {
53+
type = types.str;
54+
description = "A shell hook that installs up the git hooks in a development shell.";
55+
default = cfg.settings.installationScript;
56+
defaultText = lib.literalExpression "bash statements";
57+
readOnly = true;
58+
};
5259
installationScript = mkOption {
5360
type = types.str;
54-
description = "A bash fragment that sets up [pre-commit](https://pre-commit.com/).";
61+
description = "A bash snippet that sets up the git hooks in the current repository.";
5562
default = cfg.settings.installationScript;
56-
defaultText = lib.literalMD "bash statements";
63+
defaultText = lib.literalExpression "bash statements";
5764
readOnly = true;
5865
};
5966
devShell = mkOption {
6067
type = types.package;
61-
description = "A development shell with pre-commit installed and setup.";
68+
description = "A development shell with the git hooks installed and all the packages made available.";
6269
readOnly = true;
6370
};
6471
};
@@ -72,8 +79,8 @@ in
7279
hooks.treefmt.package = lib.mkIf (options?treefmt) (lib.mkOverride 900 config.treefmt.build.wrapper);
7380
};
7481
pre-commit.devShell = pkgs.mkShell {
75-
nativeBuildInputs = cfg.settings.enabledPackages ++ [ cfg.settings.package ];
76-
shellHook = cfg.installationScript;
82+
inherit (cfg.settings) shellHook;
83+
nativeBuildInputs = cfg.settings.enabledPackages;
7784
};
7885
};
7986
});

modules/hook.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ in
5757
name = mkOption {
5858
type = types.str;
5959
default = name;
60-
defaultText = lib.literalMD "the attribute name the hook submodule is bound to, same as `id`";
60+
defaultText = lib.literalExpression "the attribute name the hook submodule is bound to, same as `id`";
6161
description =
6262
''
6363
The name of the hook. Shown during hook execution.

modules/pre-commit.nix

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,24 @@ in
282282
defaultText = lib.literalExpression "<derivation>";
283283
};
284284

285+
shellHook =
286+
mkOption {
287+
type = types.str;
288+
description =
289+
''
290+
A shell hook that sets up the git hooks in a development shell.
291+
292+
Pass to `mkShell` as `shellHook` to use.
293+
'';
294+
readOnly = true;
295+
};
296+
285297
installationScript =
286298
mkOption {
287299
type = types.str;
288300
description =
289301
''
290-
A bash snippet that installs nix-pre-commit-hooks in the current directory
302+
A bash snippet that installs the git hooks in the current repository.
291303
'';
292304
readOnly = true;
293305
};
@@ -439,6 +451,12 @@ in
439451
default_stages = cfg.default_stages;
440452
};
441453

454+
shellHook =
455+
''
456+
${config.installationScript}
457+
export PATH=${config.package}/bin:$PATH
458+
'';
459+
442460
installationScript =
443461
''
444462
if ${boolToString cfg.install.enable}; then

nix/run.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ let
2828
}
2929
] ++ imports;
3030
};
31-
inherit (project.config) installationScript;
3231

3332
in
3433
project.config.run // {
3534
inherit (project) config;
36-
inherit (project.config) enabledPackages;
37-
shellHook = installationScript;
35+
inherit (project.config) enabledPackages shellHook;
3836
}

template/flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# NOTE: You can also use `config.pre-commit.devShell`
2828
devShells.default = pkgs.mkShell {
2929
shellHook = ''
30-
${config.pre-commit.installationScript}
30+
${config.pre-commit.shellHook}
3131
echo 1>&2 "Welcome to the development shell!"
3232
'';
3333

0 commit comments

Comments
 (0)