|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# WARNING: AI Slop |
| 5 | +# Update sha256 values for fetchurl Packages files in a Nix file. |
| 6 | +# Default target: OS-image/default.nix |
| 7 | +# Requires: nix-prefetch-url, nix (for nix hash convert) |
| 8 | + |
| 9 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" |
| 10 | +target_file="$repo_root/OS-image/default.nix" |
| 11 | +dry_run=false |
| 12 | +list_only=false |
| 13 | + |
| 14 | +usage() { |
| 15 | + cat <<EOF |
| 16 | +Usage: $(basename "$0") [-f <nix-file>] [--dry-run] [--list] |
| 17 | +
|
| 18 | +Options: |
| 19 | + -f, --file <path> Path to Nix file (default: OS-image/default.nix) |
| 20 | + --dry-run Show planned replacements, do not modify file |
| 21 | + --list List discovered URLs and current sha256s without fetching |
| 22 | +
|
| 23 | +Examples: |
| 24 | + $(basename "$0") # update all sha256 entries |
| 25 | + $(basename "$0") --dry-run # preview changes only |
| 26 | + $(basename "$0") --list # list URLs and current hashes |
| 27 | + $(basename "$0") -f path/to/file.nix # operate on a custom Nix file |
| 28 | +EOF |
| 29 | +} |
| 30 | + |
| 31 | +while [[ $# -gt 0 ]]; do |
| 32 | + case "$1" in |
| 33 | + -f|--file) |
| 34 | + shift |
| 35 | + [[ $# -gt 0 ]] || { echo "Missing argument for --file" >&2; exit 2; } |
| 36 | + target_file="$1" |
| 37 | + ;; |
| 38 | + --dry-run) |
| 39 | + dry_run=true |
| 40 | + ;; |
| 41 | + --list) |
| 42 | + list_only=true |
| 43 | + ;; |
| 44 | + -h|--help) |
| 45 | + usage |
| 46 | + exit 0 |
| 47 | + ;; |
| 48 | + *) |
| 49 | + echo "Unknown argument: $1" >&2 |
| 50 | + usage |
| 51 | + exit 2 |
| 52 | + ;; |
| 53 | + esac |
| 54 | + shift |
| 55 | +done |
| 56 | + |
| 57 | +if [[ ! -f "$target_file" ]]; then |
| 58 | + echo "Target file not found: $target_file" >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +if [[ "$list_only" != true ]]; then |
| 63 | + command -v nix-prefetch-url >/dev/null 2>&1 || { |
| 64 | + echo "nix-prefetch-url is required" >&2 |
| 65 | + exit 1 |
| 66 | + } |
| 67 | + command -v nix >/dev/null 2>&1 || { |
| 68 | + echo "nix (for 'nix hash convert') is required" >&2 |
| 69 | + exit 1 |
| 70 | + } |
| 71 | +fi |
| 72 | + |
| 73 | +# Extract mapping of sha256 line numbers, current sha256, and URLs from fetchurl blocks |
| 74 | +map_file="$(mktemp)" |
| 75 | +trap 'rm -f "$map_file"' EXIT |
| 76 | + |
| 77 | +awk ' |
| 78 | + BEGIN { in_block=0; url=""; sha=""; expect_url=0 } |
| 79 | + /packagesFile[[:space:]]*=\s*\(fetchurl[[:space:]]*\{/ { in_block=1; url=""; sha=""; expect_url=0; next } |
| 80 | + in_block && /url[[:space:]]*=/ { |
| 81 | + # handle url on same or next line |
| 82 | + expect_url=1 |
| 83 | + if (match($0, /"([^"]+)"/, m)) { url=m[1]; expect_url=0 } |
| 84 | + next |
| 85 | + } |
| 86 | + in_block && expect_url==1 { |
| 87 | + if (match($0, /"([^"]+)"/, m)) { url=m[1]; expect_url=0 } |
| 88 | + } |
| 89 | + in_block && /sha256[[:space:]]*=/ { |
| 90 | + # capture quoted sha |
| 91 | + if (match($0, /sha256[[:space:]]*=\s*"([^"]+)"/, m)) { sha=m[1] } |
| 92 | + # output: line_number|current_sha|url |
| 93 | + printf "%d|%s|%s\n", NR, sha, url |
| 94 | + next |
| 95 | + } |
| 96 | + in_block && /\}\);/ { in_block=0; url=""; sha=""; expect_url=0; next } |
| 97 | +' "$target_file" > "$map_file" |
| 98 | + |
| 99 | +if [[ "$list_only" == true ]]; then |
| 100 | + echo "Discovered fetchurl entries in $target_file:" >&2 |
| 101 | + while IFS='|' read -r lineno cur_sha url; do |
| 102 | + printf -- "- line %s: url=%s\n current sha256=%s\n" "$lineno" "$url" "$cur_sha" |
| 103 | + done < "$map_file" |
| 104 | + exit 0 |
| 105 | +fi |
| 106 | + |
| 107 | +tmp_out="$(mktemp)" |
| 108 | +cp "$target_file" "$tmp_out" |
| 109 | + |
| 110 | +changes=0 |
| 111 | +errors=0 |
| 112 | + |
| 113 | +# Read stamp variables from the Nix file (e.g., noble-updates-stamp, ros2-stamp, fictionlab-stamp) |
| 114 | +declare -A stamps |
| 115 | +while IFS= read -r line; do |
| 116 | + case "$line" in |
| 117 | + *-stamp*=*) |
| 118 | + key_part="${line%%=*}" |
| 119 | + # trim leading/trailing whitespace robustly |
| 120 | + key_part="$(printf '%s' "$key_part" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" |
| 121 | + # extract quoted value |
| 122 | + val_part="${line#*\"}" |
| 123 | + val_part="${val_part%%\"*}" |
| 124 | + if [[ -n "$key_part" && -n "$val_part" ]]; then |
| 125 | + stamps["$key_part"]="$val_part" |
| 126 | + fi |
| 127 | + ;; |
| 128 | + esac |
| 129 | +done < "$target_file" |
| 130 | + |
| 131 | +# Debug: print parsed stamp variables when dry-run or list-only for visibility |
| 132 | +if [[ "$dry_run" == true || "$list_only" == true ]]; then |
| 133 | + for k in "${!stamps[@]}"; do |
| 134 | + echo "Stamp: $k=${stamps[$k]}" >&2 |
| 135 | + done |
| 136 | +fi |
| 137 | + |
| 138 | +exec 3< "$map_file" |
| 139 | +set +e |
| 140 | +while IFS='|' read -r lineno cur_sha url <&3; do |
| 141 | + if [[ -z "$url" ]]; then |
| 142 | + echo "Skipping line $lineno: no URL found" >&2 |
| 143 | + ((errors++)) |
| 144 | + continue |
| 145 | + fi |
| 146 | + |
| 147 | + # Resolve any ${var} placeholders using parsed stamp values |
| 148 | + resolved_url="$url" |
| 149 | + for key in "${!stamps[@]}"; do |
| 150 | + pattern="\${$key}" |
| 151 | + value="${stamps[$key]}" |
| 152 | + echo "Subst: pattern=$pattern value=$value" >&2 |
| 153 | + resolved_url="${resolved_url//${pattern}/$value}" |
| 154 | + done |
| 155 | + echo "Resolved URL: $resolved_url" >&2 |
| 156 | + if [[ "$resolved_url" == *"\${"* ]]; then |
| 157 | + echo "Unresolved placeholder in URL: $url" >&2 |
| 158 | + fi |
| 159 | + |
| 160 | + echo "Prefetching: $resolved_url" >&2 |
| 161 | + if ! nix32_hash=$(nix-prefetch-url "$resolved_url" 2>/dev/null); then |
| 162 | + echo "Failed to prefetch $url" >&2 |
| 163 | + ((errors++)) |
| 164 | + continue |
| 165 | + fi |
| 166 | + echo "Prefetched nix32: $nix32_hash" >&2 |
| 167 | + if ! sri_hash=$(nix hash convert --hash-algo sha256 --from nix32 --to sri "$nix32_hash" 2>/dev/null); then |
| 168 | + echo "Failed to convert hash for $url" >&2 |
| 169 | + ((errors++)) |
| 170 | + continue |
| 171 | + fi |
| 172 | + echo "Converted SRI: $sri_hash" >&2 |
| 173 | + |
| 174 | + if [[ "$dry_run" == true ]]; then |
| 175 | + echo "Would update line $lineno: sha256 = \"$cur_sha\"; -> sha256 = \"$sri_hash\";" >&2 |
| 176 | + else |
| 177 | + # Replace the sha256 line at the exact line number |
| 178 | + if sed -i "${lineno}s|sha256 = \".*\";|sha256 = \"${sri_hash}\";|" "$tmp_out"; then |
| 179 | + echo "Updated line $lineno" >&2 |
| 180 | + ((changes++)) |
| 181 | + else |
| 182 | + echo "Failed to update line $lineno" >&2 |
| 183 | + ((errors++)) |
| 184 | + fi |
| 185 | + fi |
| 186 | +done < "$map_file" |
| 187 | +set -e |
| 188 | + |
| 189 | +if [[ "$dry_run" == true ]]; then |
| 190 | + echo "Dry-run complete. No changes written." >&2 |
| 191 | + exit 0 |
| 192 | +fi |
| 193 | + |
| 194 | +if [[ $changes -gt 0 ]]; then |
| 195 | + mv "$tmp_out" "$target_file" |
| 196 | + echo "Updated $changes sha256 entrie(s) in: $target_file" |
| 197 | +else |
| 198 | + rm -f "$tmp_out" |
| 199 | + echo "No changes made." |
| 200 | +fi |
| 201 | + |
| 202 | +if [[ $errors -gt 0 ]]; then |
| 203 | + echo "Completed with $errors error(s). Some entries may not have been updated." >&2 |
| 204 | +fi |
0 commit comments