grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
2aaa11a
main
1{ 2config , 3pkgs , 4lib , 5 ... 6} : 7 8let 9gitDir = "/mnt/ext/git" ; 10 11deploy = pkgs . writeShellScript "git-deploy" '' 12set -euo pipefail 13 14export PATH =" ${ 15lib . makeBinPath [ 16pkgs . git 17pkgs . gnutar 18pkgs . coreutils 19pkgs . nix 20pkgs . util-linux 21] 22} : $ PATH " 23 24export NIX_PATH=" nixpkgs= ${ pkgs . path } " 25 26repo=" $ 1 " 27rev=" $ 2" 28artifactDir=" $ 3" 29htmlDir=" $ 4" 30ref=" $ 5" 31 32mkdir -p " $ XDG_RUNTIME_DIR/git-deploy" 33lock= $( printf '%s' " $ repo " | sha256sum | cut -d ' ' -f1 ) 34exec 9>" $ XDG_RUNTIME_DIR/git-deploy/ $ lock .lock" 35flock 9 36 37current= $( git -C " $ repo " rev-parse --verify " $ ref ^{commit}" 2 > /dev/null) || exit 0 38if [ " $ current " != " $ rev " ]; then 39echo "Skipping obsolete deployment for $ ref " 40exit 0 41fi 42 43tmp=" $( mktemp -d ) " 44finish() { 45status= $ ? 46trap - EXIT 47rm -rf " $ tmp " 48if [ " $ status " -eq 0 ]; then 49outcome=succeeded 50else 51outcome=failed 52fi 53credentials= ${ config . sops . secrets . git-build-notify . path } 54if [ -r " $ credentials " ]; then 55printf 'Build/deploy %s: %s (%s) at %s\nArtifacts: %s\n' \ 56" $ outcome " " $ repo " " $ ref " " $ rev " " $ artifactDir " | 57${ pkgs . curl } /bin/curl --config " $ credentials " \ 58--fail --silent --show-error --connect-timeout 3 --max-time 10 \ 59--header "Title: Git build/deploy $ outcome " \ 60--data-binary @- --output /dev/null \ 61https://ntfy.gae.moe/githooks || true 62fi 63exit " $ status " 64} 65trap finish EXIT 66 67while read -r key; do 68unset " $ key " 69done < <( git rev-parse --local-env-vars ) 70 71git -C " $ repo " archive " $ rev " | tar -xf - -C " $ tmp " 72cd " $ tmp " 73 74result =" $( nix-build --out-link " $ tmp /result" ./hook.nix \ 75--argstr artifactDir " $ artifactDir " \ 76--argstr htmlDir " $ htmlDir " ) " 77 78mkdir -p " $ artifactDir " " $ htmlDir " 79" $ result /bin/deploy" 80 81commitDate =" $( git -C " $ repo " log -1 --format=%ct " $ rev ^{commit}" ) " 82 83find " $ artifactDir " " $ htmlDir " \ 84-exec touch -h -d "@ $ commitDate " {} + 85'' ; 86 87queue = pkgs . writeShellScript "git-queue-deploy" '' 88set -euo pipefail 89export PATH = ${ 90lib . makeBinPath [ 91pkgs . git 92pkgs . coreutils 93pkgs . systemd 94] 95} : $ PATH 96XDG_RUNTIME_DIR ="/run/user/ $( id -u ) " 97export XDG_RUNTIME_DIR 98if [ " $( id -un ) " != git ]; then 99echo "Run this command as the git user" >&2 100exit 1 101fi 102 103repoPath = $( realpath " $ 1 " ) 104ref =" $ 2 " 105rev =" $ 3 " 106root = ${ gitDir } 107case " $ repoPath " in 108" $ root "/*) ;; 109*) echo "Repository must be beneath $ root " >&2; exit 1 ;; 110esac 111case " $ ref " in 112refs/heads/*) name ="'' $ { ref #refs/heads/}"; otherNamespace=refs/tags ;; 113refs/tags/*) name ="'' $ { ref #refs/tags/}"; otherNamespace=refs/heads ;; 114*) exit 0 ;; 115esac 116commit= $( git -C " $ repoPath " rev-parse --verify " $ rev ^{commit}" 2 > /dev/null) || exit 0 117git -C " $ repoPath " cat-file -e " $ commit :hook.nix" 2 > /dev/null || exit 0 118others = $( git -C " $ repoPath " for-each-ref --format= '%(refname)' " $ otherNamespace " ) 119while IFS = read -r other; do 120[ -n " $ other " ] || continue 121otherName ="'' $ { other #"$otherNamespace"/}" 122if [ " $ name " = " $ otherName " ] || [[ " $ name " = " $ otherName /"* ]] || [[ " $ otherName " = " $ name /"* ]]; then 123if git -C " $ repoPath " cat-file -e " $ other :hook.nix" 2 > /dev/null; then 124echo "Cannot deploy $ ref : $ other overlaps its output path" >&2 125exit 1 126fi 127fi 128done <<< " $ others " 129 130repo ="'' $ { repoPath #"$root"/}" 131repo ="'' $ { repo %.git}" 132systemd-run --user --no-block --collect \ 133--unit="git-deploy- $( date +%s%N) - $ RANDOM " \ 134${ deploy } " $ repoPath " " $ commit " \ 135" $ root /artifacts/ $ repo / $ name " " $ root /www/ $ repo / $ name " " $ ref " 136echo "Queued deployment for $ ref " >&2 137'' ; 138 139regenerate = pkgs . writeShellScriptBin "git-regenerate" '' 140set -euo pipefail 141export PATH = ${ 142lib . makeBinPath [ 143pkgs . git 144pkgs . coreutils 145] 146} : $ PATH 147if [ " $ #" -gt 1 ]; then 148echo "Usage: git-regenerate [REPOSITORY]" >&2 149exit 2 150fi 151repo =$( realpath "'' $ {1:-.}") 152refs= $( git -C " $ repo " for-each-ref --format= '%(refname)' refs/heads refs/tags) 153status=0 154while IFS= read -r ref; do 155[ -n " $ ref " ] || continue 156${ queue } " $ repo " " $ ref " " $ ref " || status =1 157done <<< " $ refs " 158exit " $ status " 159'' ; 160 161mirror = pkgs . writeShellScript "git-mirror" '' 162set -euo pipefail 163export PATH = ${ 164lib . makeBinPath [ 165pkgs . git 166pkgs . coreutils 167pkgs . util-linux 168] 169} : $ PATH 170export GIT_TERMINAL_PROMPT =0 171export GIT_SSH_COMMAND =' ${ pkgs . openssh } /bin/ssh -o BatchMode=yes -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=2' 172repo="$1" 173mkdir -p "$XDG_RUNTIME_DIR/git-deploy" 174lock=$(printf '%s' "$repo" | sha256sum | cut -d' ' -f1) 175exec 9>"$XDG_RUNTIME_DIR/git-deploy/$lock-mirror.lock" 176flock 9 177 178for remote in tangled github codeberg; do 179git -C "$repo" remote get-url --push "$remote" &>/dev/null || continue 180echo "Pushing $repo to $remote..." >&2 181failed=0 182timeout 120 git -C "$repo" push --force "$remote" --all || failed=1 183timeout 120 git -C "$repo" push --force "$remote" --tags || failed=1 184if [ "$failed" -ne 0 ]; then 185credentials= ${ config . sops . secrets . git-build-notify . path } 186if [ -r " $ credentials " ]; then 187printf 'Failed to push %s to %s\n' " $ repo " " $ remote " | 188${ pkgs . curl } /bin/curl --config " $ credentials " \ 189--fail --silent --show-error --connect-timeout 3 --max-time 10 \ 190--header 'Title: Git mirror failed' \ 191--data-binary @- --output /dev/null https://ntfy.gae.moe/githooks || true 192fi 193fi 194done 195'' ; 196in 197{ 198sops . secrets . git-build-notify = { 199sopsFile = ../secrets/knuth/git-build-notify.json ; 200format = "binary" ; 201owner = "git" ; 202}; 203 204environment . systemPackages = [ regenerate ]; 205users . users . git . linger = true ; 206 207programs . git = { 208enable = true ; 209config . core . hooksPath = "/etc/git/hooks" ; 210}; 211 212environment . etc . "git/hooks/post-receive" . source = pkgs . writeShellScript "post-receive" '' 213set -euo pipefail 214 215export PATH =" ${ 216lib . makeBinPath [ 217pkgs . git 218pkgs . coreutils 219pkgs . systemd 220] 221} : $ PATH " 222 223XDG_RUNTIME_DIR=" /run/user/ $( id -u ) " 224export XDG_RUNTIME_DIR 225 226root=" ${ gitDir } " 227repoPath=" $( pwd -P ) " 228 229case " $ repoPath " in 230" $ root "/*) ;; 231*) exit 0 ;; 232esac 233 234updates=" $( cat ) " 235 236status=0 237while read -r _old new ref; do 238[[ " $ new " =~ ^0+ $ ]] && continue 239${ queue } " $ repoPath " " $ ref " " $ new " || status =1 240done <<< " $ updates " 241 242systemd-run --user --no-block --collect \ 243--unit= "git-mirror- $( date +%s%N) - $ RANDOM " ${ mirror } " $ repoPath " || status =1 244 245localHook =" $ repoPath /hooks/post-receive" 246 247if [ -x " $ localHook " ]; then 248printf '%s\n' " $ updates " | " $ localHook " || status =1 249fi 250exit " $ status " 251'' ; 252}