Kieran's opinionated (and probably slightly dumb) nix config

bug: fix knot sync

dunkirk.sh fbadff7c 735743c4

verified
+64 -56
+64 -56
modules/nixos/services/knot-sync.nix
··· 61 61 log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "$LOG_FILE"; } 62 62 63 63 # Create the post-receive hook template 64 - cat <<'EOF' > /tmp/post-receive.template 65 - #!${pkgs.bash}/bin/bash 66 - # post-receive hook to sync to GitHub - AUTOGENERATED 64 + # Use plain 'git' - it's in PATH via environment.systemPackages 65 + cat <<'HOOKEOF' > /tmp/post-receive.template 66 + #!/usr/bin/env bash 67 + # post-receive hook to sync to GitHub - AUTOGENERATED 67 68 68 - # Load environment variables from secrets file 69 - if [ -f "${cfg.secretsFile}" ]; then 70 - source "${cfg.secretsFile}" 71 - fi 69 + # Load environment variables from secrets file 70 + if [ -f "${cfg.secretsFile}" ]; then 71 + source "${cfg.secretsFile}" 72 + fi 72 73 73 - # Variables 74 - GITHUB_USERNAME="${cfg.githubUsername}" 75 - LOG_FILE="${cfg.logFile}" 76 - REPO_NAME=$(basename $(pwd)) 74 + # Variables 75 + GITHUB_USERNAME="${cfg.githubUsername}" 76 + LOG_FILE="${cfg.logFile}" 77 + REPO_NAME=$(basename $(pwd)) 77 78 78 - # Log function 79 - log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "''${LOG_FILE}"; } 79 + # Log function 80 + log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "''${LOG_FILE}"; } 80 81 81 - # Check for nosync marker 82 - if [ -f "$(pwd)/.nosync" ]; then 83 - log "Skipping sync for $REPO_NAME (nosync marker present)" 84 - exit 0 85 - fi 82 + # Check for nosync marker 83 + if [ -f "$(pwd)/.nosync" ]; then 84 + log "Skipping sync for $REPO_NAME (nosync marker present)" 85 + exit 0 86 + fi 86 87 87 - # Function to sync to GitHub 88 - sync_to_github() { 89 - log "Syncing $REPO_NAME to GitHub" 90 - expected_url="https://''${GITHUB_USERNAME}:''${GITHUB_TOKEN}@github.com/''${GITHUB_USERNAME}/''${REPO_NAME}.git" 91 - current_url=$(${pkgs.git}/bin/git remote get-url origin 2>/dev/null || echo "") 88 + # Function to sync to GitHub 89 + sync_to_github() { 90 + log "Syncing $REPO_NAME to GitHub" 91 + expected_url="https://''${GITHUB_USERNAME}:''${GITHUB_TOKEN}@github.com/''${GITHUB_USERNAME}/''${REPO_NAME}.git" 92 + current_url=$(git remote get-url origin 2>/dev/null || echo "") 92 93 93 - if [ -z "$current_url" ]; then 94 - log "Adding origin remote" 95 - ${pkgs.git}/bin/git remote add origin "$expected_url" 96 - elif [ "$current_url" != "$expected_url" ]; then 97 - log "Updating origin remote URL" 98 - ${pkgs.git}/bin/git remote set-url origin "$expected_url" 99 - fi 94 + if [ -z "$current_url" ]; then 95 + log "Adding origin remote" 96 + git remote add origin "$expected_url" 97 + elif [ "$current_url" != "$expected_url" ]; then 98 + log "Updating origin remote URL" 99 + git remote set-url origin "$expected_url" 100 + fi 100 101 101 - # Mirror push everything (refs, tags, branches) 102 - if ${pkgs.git}/bin/git push --mirror origin 2>&1 | tee -a "''${LOG_FILE}"; then 103 - log "Sync succeeded for $REPO_NAME" 104 - return 0 105 - else 106 - log "Sync failed for $REPO_NAME" 107 - return 1 108 - fi 109 - } 102 + # Mirror push everything (refs, tags, branches) 103 + if git push --mirror origin 2>&1 | tee -a "''${LOG_FILE}"; then 104 + log "Sync succeeded for $REPO_NAME" 105 + return 0 106 + else 107 + log "Sync failed for $REPO_NAME" 108 + return 1 109 + fi 110 + } 110 111 111 - # Main 112 - while read oldrev newrev refname; do 113 - log "Received push for ref '$refname' (old revision: $oldrev, new revision: $newrev)" 114 - sync_to_github 115 - done 116 - EOF 112 + # Main 113 + while read oldrev newrev refname; do 114 + log "Received push for ref '$refname' (old revision: $oldrev, new revision: $newrev)" 115 + sync_to_github 116 + done 117 + HOOKEOF 117 118 118 119 HOOK_TEMPLATE="/tmp/post-receive.template" 119 120 120 - # Create the post-receive hook 121 + # Create or update the post-receive hook 121 122 create_hook() { 122 123 local new_repo_path="$1" 123 124 local hook_path="$new_repo_path/hooks/post-receive.d/forward" ··· 129 130 return 0 130 131 fi 131 132 132 - if [ -d "$new_repo_path" ] && [ ! -f "$hook_path" ]; then 133 + if [ -d "$new_repo_path" ]; then 133 134 # Check that it's a git repository, specifically a bare repo 134 135 if [ -f "$new_repo_path/config" ]; then 135 136 # Create hooks directory if it doesn't exist 136 137 mkdir -p "$(dirname "$hook_path")" 137 - # Create hook from the template file, substituting variables. 138 + 139 + # Check if hook is new or needs updating 140 + local is_new=false 141 + [ ! -f "$hook_path" ] && is_new=true 142 + 143 + # Always write the hook (they're autogenerated) 138 144 if cat "$HOOK_TEMPLATE" > "$hook_path" && chmod +x "$hook_path"; then 139 - log "Created hook for $new_repo_path" 140 - # Check if repo has any commits before pushing 141 - if (cd "$new_repo_path" && ${pkgs.git}/bin/git rev-parse HEAD >/dev/null 2>&1); then 142 - # Auto push by simulating a post-receive hook trigger 143 - log "Triggering initial push for $new_repo_path" 144 - (cd "$new_repo_path" && \ 145 - echo "0000000000000000000000000000000000000000 $(${pkgs.git}/bin/git rev-parse HEAD) refs/heads/main" | \ 146 - "$hook_path") 145 + if [ "$is_new" = true ]; then 146 + log "Created hook for $new_repo_path" 147 + # Check if repo has any commits before pushing 148 + if (cd "$new_repo_path" && git rev-parse HEAD >/dev/null 2>&1); then 149 + # Auto push by simulating a post-receive hook trigger 150 + log "Triggering initial push for $new_repo_path" 151 + (cd "$new_repo_path" && \ 152 + echo "0000000000000000000000000000000000000000 $(git rev-parse HEAD) refs/heads/main" | \ 153 + "$hook_path") 154 + fi 147 155 fi 148 156 else 149 157 log "Hook creation failed for $new_repo_path"