git: add config helper for hooks

This commit is contained in:
Charlie Moog 2022-09-04 11:57:45 -05:00 committed by Robert Helgesson
parent 2e41a1bab3
commit 340ec22f6f
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 53 additions and 0 deletions

View file

@ -201,6 +201,21 @@ in {
'';
};
hooks = mkOption {
type = types.attrsOf types.path;
default = { };
example = literalExpression ''
{
pre-commit = ./pre-commit-script;
}
'';
description = ''
Configuration helper for Git hooks.
See <link xlink:href="https://git-scm.com/docs/githooks" />
for reference.
'';
};
iniContent = mkOption {
type = gitIniType;
internal = true;
@ -449,6 +464,15 @@ in {
};
})
(mkIf (cfg.hooks != { }) {
programs.git.iniContent = {
core.hooksPath = let
entries =
mapAttrsToList (name: path: { inherit name path; }) cfg.hooks;
in toString (pkgs.linkFarm "git-hooks" entries);
};
})
(mkIf (cfg.aliases != { }) { programs.git.iniContent.alias = cfg.aliases; })
(mkIf (lib.isAttrs cfg.extraConfig) {

View file

@ -5,4 +5,5 @@
git-with-str-extra-config = ./git-with-str-extra-config.nix;
git-with-signing-key-id = ./git-with-signing-key-id.nix;
git-without-signing-key-id = ./git-without-signing-key-id.nix;
git-with-hooks = ./git-with-hooks.nix;
}

View file

@ -0,0 +1,2 @@
#!/bin/sh
echo "pre-commit hook..."

View file

@ -0,0 +1,26 @@
{ pkgs, ... }:
{
programs.git = {
enable = true;
hooks = { pre-commit = ./git-pre-commit-hook.sh; };
};
nmt.script = ''
function getGitConfig() {
${pkgs.gitMinimal}/bin/git config \
--file $TESTED/home-files/.config/git/config \
--get $1
}
assertFileExists home-files/.config/git/config
hookPath=$(getGitConfig core.hooksPath)
assertLinkExists $hookPath/pre-commit
actual="$(readlink "$hookPath/pre-commit")"
expected="${./git-pre-commit-hook.sh}"
if [[ $actual != $expected ]]; then
fail "Symlink $hookPath/pre-commit should point to $expected via the Nix store, but it actually points to $actual."
fi
'';
}