git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

feat: update pre-commit.nix to be "run" customizable

Open kakkun61 opened this issue 1 year ago • 0 comments

What is this

This make options.run's readOnly true.

And configFile and installStages will be public so that a customized run may use them.

Why is this

For instance a repository having Git submodules will use additional flake inputs which is the same with the submodules.

$ git submodule add … foo
{ inputs.foo.url = "…";
  …;
  outputs = { self, foo, ... }: {
    # build or do something after storing foo contents to ./foo
    # like: preBuild = "ln -s ${submodules.otlp-protobufs}/* -t proto";
  };
}

In this case we want to customize run to store foo before running pre-commit command similarly like:

git-hooks.lib.${system}.run {
  src = ./.;
  hooks = pre-commit-hooks;
  imports = [
    ({ config, lib, pkgs, ... }: {
      run = pkgs.runCommand "pre-commit-run" {buildInputs = [pkgs.git config.package];} ''
        set +e
        HOME=$PWD
        cp -R ${config.rootSrc} src
        chmod -R +w src
        ln -fs ${config.configFile} src/.pre-commit-config.yaml
        # 👇 Inserted
        mkdir src/foo
        ln -s ${foo}/* -t src/foo
        # 👆
        cd src
        …
      '';
    })
  ];
};

This change supports more complex use cases.

kakkun61 avatar Sep 03 '24 11:09 kakkun61