feat: add package to exclude gitignored hooks

tools like renovate can run soonix:update after updating stuff, but the
gitignored hooks cannot be committed anyways, so why bother building
them
This commit is contained in:
technofab 2026-01-01 15:56:25 +01:00
parent 56f281eea4
commit 87399d28fe
Signed by: technofab
SSH key fingerprint: SHA256:bV4h88OqS/AxjbPn66uUdvK9JsgIW4tv3vwJQ8tpMqQ
2 changed files with 116 additions and 78 deletions

View file

@ -153,83 +153,93 @@ in {
config = let config = let
hooks = config.hooks; hooks = config.hooks;
hookNames = builtins.attrNames hooks; # allow excluding gitignore since stuff like renovate can't use/commit it anyways
getHookNames = {includeGitignored ? true}:
if includeGitignored
then (builtins.attrNames hooks)
else
map
(hook: hook.name)
(builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks));
runHooks = concatMapStringsSep "\n" (hookName: let runHooks = args:
hook = hooks.${hookName}; concatMapStringsSep "\n" (hookName: let
modes = { hook = hooks.${hookName};
link = modes = {
# sh link =
'' # sh
if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then ''
_soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then
mkdir -p "$(dirname "${hook.output}")" _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}"
ln -sf "${hook.generatedDerivation}" "${hook.output}" mkdir -p "$(dirname "${hook.output}")"
_changed=true ln -sf "${hook.generatedDerivation}" "${hook.output}"
else _changed=true
_soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" else
fi _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}"
''; fi
copy = '';
# sh copy =
'' # sh
if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then ''
_soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then
mkdir -p "$(dirname "${hook.output}")" _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}"
# required since they're read only mkdir -p "$(dirname "${hook.output}")"
rm -f "${hook.output}" # required since they're read only
cp "${hook.generatedDerivation}" "${hook.output}" rm -f "${hook.output}"
_changed=true cp "${hook.generatedDerivation}" "${hook.output}"
else _changed=true
_soonix_log "info" "${hookName}" "File up to date: ${hook.output}" else
fi _soonix_log "info" "${hookName}" "File up to date: ${hook.output}"
''; fi
}; '';
};
optionalGitignore = optionalGitignore =
if hook.hook.gitignore if hook.hook.gitignore
then '' then ''
_soonix_add_to_gitignore "${hook.output}" _soonix_add_to_gitignore "${hook.output}"
''
else "";
in
builtins.addErrorContext "[soonix] while generating script for ${hookName}"
# sh
'' ''
else ""; # Process hook: ${hookName}
in while IFS= read -r line; do
# sh case "$line" in
'' UPDATED) _soonix_updated+=("${hookName}") ;;
# Process hook: ${hookName} UPTODATE) _soonix_uptodate+=("${hookName}") ;;
while IFS= read -r line; do *) echo "$line" ;;
case "$line" in esac
UPDATED) _soonix_updated+=("${hookName}") ;; done < <(
UPTODATE) _soonix_uptodate+=("${hookName}") ;; set -euo pipefail
*) echo "$line" ;; _changed=false
esac
done < <(
set -euo pipefail
_changed=false
${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")} ${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")}
# Add to gitignore if requested # Add to gitignore if requested
${optionalGitignore} ${optionalGitignore}
# Run extra commands if file changed # Run extra commands if file changed
if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then
_soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}"
eval "${hook.hook.extra}" eval "${hook.hook.extra}"
fi fi
if [[ "$_changed" == "true" ]]; then if [[ "$_changed" == "true" ]]; then
echo "UPDATED" echo "UPDATED"
else else
echo "UPTODATE" echo "UPTODATE"
fi fi
) || { ) || {
_soonix_log "error" "${hookName}" "Failed to process hook" _soonix_log "error" "${hookName}" "Failed to process hook"
_soonix_failed+=("${hookName}") _soonix_failed+=("${hookName}")
} }
'') '')
hookNames; (getHookNames args);
generatedShellHook = generateShellHook = args:
builtins.addErrorContext "[soonix] while generating shell hook"
# sh # sh
'' ''
_soonix_log() { _soonix_log() {
@ -265,7 +275,7 @@ in {
_soonix_failed=() _soonix_failed=()
_soonix_uptodate=() _soonix_uptodate=()
${runHooks} ${runHooks args}
local output=$'\E[msoonix:\E[38;5;8m' local output=$'\E[msoonix:\E[38;5;8m'
local status=0 local status=0
@ -297,8 +307,8 @@ in {
in rec { in rec {
# nothing to do if no hooks exist # nothing to do if no hooks exist
shellHook = shellHook =
if (builtins.length hookNames > 0) if (builtins.length (builtins.attrNames config.hooks) > 0)
then generatedShellHook then generateShellHook {}
else ""; else "";
shellHookFile = pkgs.writeShellScript "shellHook" shellHook; shellHookFile = pkgs.writeShellScript "shellHook" shellHook;
devshellModule = { devshellModule = {
@ -307,11 +317,17 @@ in {
}; };
finalFiles = buildAllFiles allFiles; finalFiles = buildAllFiles allFiles;
# make it simpler to update the hooks without any devshell # make it simpler to update the hooks without any devshell
packages."soonix:update" = pkgs.writeShellScriptBin "soonix:update" '' packages = let
function _soonix() { mkPackage = args:
${shellHook} pkgs.writeShellScriptBin "soonix:update" ''
} function _soonix() {
_soonix ${generateShellHook args}
''; }
_soonix
'';
in {
"soonix:update" = mkPackage {};
"soonix:update:excludegitignore" = mkPackage {includeGitignored = false;};
};
}; };
} }

View file

@ -69,6 +69,28 @@ in {
assert_file_contains ${shellHook} "gomplate" assert_file_contains ${shellHook} "gomplate"
''; '';
} }
{
name = "packages";
type = "script";
script = let
conf = (soonix.make {inherit hooks;}).config;
baseBin = conf.packages."soonix:update" + "/bin/soonix:update";
exclBin = conf.packages."soonix:update:excludegitignore" + "/bin/soonix:update";
in
# sh
''
${ntlib.helpers.path [pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
assert -f "${baseBin}" "should exist"
assert -f "${exclBin}" "should exist"
assert_file_contains "${baseBin}" "gotmpl"
assert_file_contains "${baseBin}" "test.json"
assert_file_not_contains "${exclBin}" "gotmpl"
assert_file_contains "${exclBin}" "test.json"
'';
}
]; ];
}; };
} }