grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
003ed04
main
1{ pkgs , lib , ...} : 2 3let 4gitDir = "/mnt/ext/git" ; 5 6deploy = pkgs . writeShellScript "git-deploy" '' 7set -euo pipefail 8 9export PATH =" ${ 10lib . makeBinPath [ 11pkgs . git 12pkgs . gnutar 13pkgs . coreutils 14pkgs . nix 15] 16} : $ PATH " 17 18export NIX_PATH=" nixpkgs= ${ pkgs . path } " 19 20repo=" $ 1 " 21rev=" $ 2" 22artifactDir=" $ 3" 23htmlDir=" $ 4" 24ref=" $ 5" 25 26tmp=" $( mktemp -d ) " 27finish() { 28status= $ ? 29trap - EXIT 30rm -rf " $ tmp " 31if [ " $ status " -eq 0 ]; then 32outcome=succeeded 33else 34outcome=failed 35fi 36credentials=/var/lib/git-build-notify/credentials.curl 37if [ -r " $ credentials " ]; then 38printf 'Build/deploy %s: %s (%s) at %s\nArtifacts: %s\n' \ 39" $ outcome " " $ repo " " $ ref " " $ rev " " $ artifactDir " | 40${ pkgs . curl } /bin/curl --config " $ credentials " \ 41--fail --silent --show-error --connect-timeout 3 --max-time 10 \ 42--header "Title: Git build/deploy $ outcome " \ 43--data-binary @- --output /dev/null \ 44https://ntfy.gae.moe/githooks || true 45fi 46exit " $ status " 47} 48trap finish EXIT 49 50while read -r key; do 51unset " $ key " 52done < <( git rev-parse --local-env-vars ) 53 54git -C " $ repo " archive " $ rev " | tar -xf - -C " $ tmp " 55cd " $ tmp " 56 57result =" $( nix-build --no-out-link ./hook.nix \ 58--argstr artifactDir " $ artifactDir " \ 59--argstr htmlDir " $ htmlDir " ) " 60 61mkdir -p " $ artifactDir " " $ htmlDir " 62" $ result /bin/deploy" 63'' ; 64in 65{ 66users . users . git . linger = true ; 67 68programs . git = { 69enable = true ; 70config . core . hooksPath = "/etc/git/hooks" ; 71}; 72 73environment . etc . "git/hooks/post-receive" . source = pkgs . writeShellScript "post-receive" '' 74set -euo pipefail 75 76export PATH =" ${ 77lib . makeBinPath [ 78pkgs . git 79pkgs . coreutils 80pkgs . systemd 81] 82} : $ PATH " 83 84export XDG_RUNTIME_DIR=" /run/user/ $( id -u ) " 85 86root=" ${ gitDir } " 87repoPath=" $( pwd -P ) " 88 89case " $ repoPath " in 90" $ root "/*) ;; 91*) exit 0 ;; 92esac 93 94repo="'' $ { repoPath #"$root"/}" 95repo ="'' $ { repo %.git}" 96 97while read -r old new ref; do 98case " $ ref " in 99refs/heads/*) name="'' $ { ref #refs/heads/}" ;; 100refs/tags/*) name ="'' $ { ref #refs/tags/}" ;; 101*) continue ;; 102esac 103[[ " $ new " =~ ^0+$ ]] && continue 104 105git cat-file -e " $ new :hook.nix" 2 > /dev/null || continue 106 107systemd-run --user --no-block --collect \ 108--unit="git-deploy- $( date +%s%N) - $ RANDOM " \ 109" ${ deploy } " \ 110" $ repoPath " \ 111" $ new " \ 112" ${ gitDir } /artifacts/ $ repo / $ name " \ 113" ${ gitDir } /www/ $ repo / $ name " \ 114" $ ref " 115 116echo " Queued deployment for $ ref " >&2 117done 118'' ; 119}