|
291 | 291 | readOnly = true;
|
292 | 292 | };
|
293 | 293 |
|
| 294 | + installByDefault = |
| 295 | + mkOption { |
| 296 | + type = types.bool; |
| 297 | + default = true; |
| 298 | + description = lib.mdDoc '' |
| 299 | + Whether to install the hooks _by default_. |
| 300 | +
|
| 301 | + By priority, the behavior of `installationScript` (ie `nix develop`) |
| 302 | + is controlled by the user's `NIX_PRE_COMMIT_HOOKS_INSTALL` environment |
| 303 | + variable. The purpose of this variable is that users can globally override |
| 304 | + the use of `pre-commit` for all their projects. |
| 305 | +
|
| 306 | + Regardless of any settings, you can still install the hooks by running: |
| 307 | +
|
| 308 | + ```bash |
| 309 | + nix-pre-commit-hooks-install |
| 310 | + ``` |
| 311 | +
|
| 312 | + After running that, the pre-commit hooks scripts and config file will be |
| 313 | + updated by `installationScript` (ie `nix develop`) as usual. |
| 314 | + ''; |
| 315 | + }; |
| 316 | + |
294 | 317 | src =
|
295 | 318 | lib.mkOption {
|
296 | 319 | description = lib.mdDoc ''
|
|
375 | 398 | installationScript = ''
|
376 | 399 | export PATH=${cfg.package}/bin:$PATH
|
377 | 400 |
|
| 401 | + export NIX_PRE_COMMIT_HOOKS_INSTALL="''${NIX_PRE_COMMIT_HOOKS_INSTALL:-${if cfg.installByDefault then "1" else "0"}}" |
| 402 | +
|
378 | 403 | nix-pre-commit-hooks-update() {
|
379 | 404 | local git_dir
|
380 | 405 | if ! type -t git >/dev/null; then
|
|
389 | 414 |
|
390 | 415 | if readlink .pre-commit-config.yaml >/dev/null \
|
391 | 416 | && [[ $(readlink .pre-commit-config.yaml) == ${configFile} ]]; then
|
392 |
| - echo 1>&2 "pre-commit-hooks.nix: hooks up to date" |
| 417 | + echo 1>&2 "pre-commit-hooks.nix: hooks config is up to date" |
393 | 418 | else
|
394 | 419 | echo 1>&2 "pre-commit-hooks.nix: updating $PWD repo"
|
395 | 420 |
|
|
405 | 430 | ln -s ${configFile} .pre-commit-config.yaml
|
406 | 431 | # Remove any previously installed hooks (since pre-commit itself has no convergent design)
|
407 | 432 | hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
|
408 |
| - for hook in $hooks; do |
409 |
| - pre-commit uninstall -t $hook |
410 |
| - done |
| 433 | +
|
| 434 | + # ... but if the user has opted out of automatic installation, |
| 435 | + # don't uninstall, because in that case already-installed hooks |
| 436 | + # represent a deliberate choice, and we don't want to keep them |
| 437 | + # and maintain them. |
| 438 | + if [[ "$NIX_PRE_COMMIT_HOOKS_INSTALL" == 1 ]]; then |
| 439 | + for hook in $hooks; do |
| 440 | + pre-commit uninstall -t $hook |
| 441 | + done |
| 442 | + fi |
| 443 | +
|
411 | 444 | # Add hooks for configured stages (only) ...
|
412 | 445 | if [ ! -z "${concatStringsSep " " install_stages}" ]; then
|
413 | 446 | for stage in ${concatStringsSep " " install_stages}; do
|
|
426 | 459 | exit 1
|
427 | 460 | ;;
|
428 | 461 | esac
|
429 |
| - pre-commit install -t $stage |
| 462 | + # If the user environment doesn't want hooks, don't install them |
| 463 | + # If the hook already exists, update the embedded pre-commit package version, |
| 464 | + # because they may have opted in by running `nix-pre-commit-hooks-install` |
| 465 | + if [[ "$NIX_PRE_COMMIT_HOOKS_INSTALL" == 1 ]] || \ |
| 466 | + [[ -e "$gitdir/hooks/$stage" ]]; then |
| 467 | + pre-commit install -t $stage |
| 468 | + fi |
430 | 469 | done
|
431 | 470 | # ... or default 'pre-commit' hook
|
432 | 471 | else
|
433 |
| - pre-commit install |
| 472 | + if [[ "$NIX_PRE_COMMIT_HOOKS_INSTALL" == 1 ]] || \ |
| 473 | + [[ -e "$gitdir/hooks/pre-commit" ]]; then |
| 474 | + pre-commit install |
| 475 | + else |
| 476 | + echo 1>&2 "pre-commit-hooks.nix: skipping installation of pre-commit hook scripts. Run 'nix-pre-commit-hooks-install' to install them." |
| 477 | + fi |
434 | 478 | fi
|
435 | 479 | fi
|
436 | 480 | fi
|
437 | 481 | fi
|
438 | 482 | }
|
439 | 483 |
|
| 484 | + nix-pre-commit-hooks-install() { |
| 485 | + # Make sure installation will run |
| 486 | + [ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml |
| 487 | +
|
| 488 | + NIX_PRE_COMMIT_HOOKS_INSTALL=1 nix-pre-commit-hooks-update |
| 489 | + } |
| 490 | +
|
440 | 491 | nix-pre-commit-hooks-update
|
441 | 492 | '';
|
442 | 493 | };
|
|
0 commit comments