Skip to content

Instantly share code, notes, and snippets.

@ItsHarper
Created July 10, 2025 20:44
Show Gist options
  • Save ItsHarper/39d312e2675d05190657dede689213e0 to your computer and use it in GitHub Desktop.
Save ItsHarper/39d312e2675d05190657dede689213e0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env nu
# Place at /etc/apk/commit_hooks.d/git_backup.nu and make executable
def main [apkStage: string] {
if $apkStage != "post-commit" {
exit 0
}
/usr/bin/chown root:root /etc/apk/commit_hooks.d/git_backup.nu
/usr/bin/chmod 744 /etc/apk/commit_hooks.d/git_backup.nu
cd /home/harper/chimera-config
# We have to diff against HEAD to include both staged and unstaged changes
let repoWasDirty = (
do { /usr/bin/doas -u harper /usr/bin/git diff HEAD --quiet }
| complete
| get exit_code
) == 1
if $repoWasDirty {
/usr/bin/doas -u harper /usr/bin/git stash push -m "Uncommitted changes prior to apk operation" --quiet
print "Stashed existing changes"
}
/usr/bin/doas -u harper /usr/bin/cp /etc/apk/world .
let worldChanged = (
do { /usr/bin/doas -u harper /usr/bin/git diff --exit-code }
| complete
| get exit_code
) == 1
if $worldChanged {
/usr/bin/doas -u harper /usr/bin/git add world
/usr/bin/doas -u harper /usr/bin/git commit -m "apk: Update world" -m $env.APK_COMMIT_DESC --quiet
print "Committed changes to world (make sure to push them from ~/chimera-config later)"
}
if $repoWasDirty {
/usr/bin/doas -u harper /usr/bin/git stash pop --quiet
print "Restored existing changes"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment