update-nix-hashes.yml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. name: Update Nix Hashes
  2. permissions:
  3. contents: write
  4. on:
  5. workflow_dispatch:
  6. push:
  7. paths:
  8. - "bun.lock"
  9. - "package.json"
  10. - "packages/*/package.json"
  11. - ".github/workflows/update-nix-hashes.yml"
  12. pull_request:
  13. paths:
  14. - "bun.lock"
  15. - "package.json"
  16. - "packages/*/package.json"
  17. - ".github/workflows/update-nix-hashes.yml"
  18. jobs:
  19. compute-node-modules-hash:
  20. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
  21. strategy:
  22. fail-fast: false
  23. matrix:
  24. include:
  25. - system: x86_64-linux
  26. host: blacksmith-4vcpu-ubuntu-2404
  27. - system: aarch64-linux
  28. host: blacksmith-4vcpu-ubuntu-2404-arm
  29. - system: x86_64-darwin
  30. host: macos-15-intel
  31. - system: aarch64-darwin
  32. host: macos-latest
  33. runs-on: ${{ matrix.host }}
  34. env:
  35. SYSTEM: ${{ matrix.system }}
  36. steps:
  37. - name: Checkout repository
  38. uses: actions/checkout@v6
  39. with:
  40. token: ${{ secrets.GITHUB_TOKEN }}
  41. fetch-depth: 0
  42. ref: ${{ github.head_ref || github.ref_name }}
  43. repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
  44. - name: Setup Nix
  45. uses: nixbuild/nix-quick-install-action@v34
  46. - name: Compute node_modules hash
  47. run: |
  48. set -euo pipefail
  49. DUMMY="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
  50. HASH_FILE="nix/hashes.json"
  51. OUTPUT_FILE="hash-${SYSTEM}.txt"
  52. export NIX_KEEP_OUTPUTS=1
  53. export NIX_KEEP_DERIVATIONS=1
  54. BUILD_LOG=$(mktemp)
  55. TMP_JSON=$(mktemp)
  56. trap 'rm -f "$BUILD_LOG" "$TMP_JSON"' EXIT
  57. if [ ! -f "$HASH_FILE" ]; then
  58. mkdir -p "$(dirname "$HASH_FILE")"
  59. echo '{"nodeModules":{}}' > "$HASH_FILE"
  60. fi
  61. # Set dummy hash to force nix to rebuild and reveal correct hash
  62. jq --arg system "$SYSTEM" --arg value "$DUMMY" \
  63. '.nodeModules = (.nodeModules // {}) | .nodeModules[$system] = $value' "$HASH_FILE" > "$TMP_JSON"
  64. mv "$TMP_JSON" "$HASH_FILE"
  65. MODULES_ATTR=".#packages.${SYSTEM}.default.node_modules"
  66. DRV_PATH="$(nix eval --raw "${MODULES_ATTR}.drvPath")"
  67. echo "Building node_modules for ${SYSTEM} to discover correct hash..."
  68. echo "Attempting to realize derivation: ${DRV_PATH}"
  69. REALISE_OUT=$(nix-store --realise "$DRV_PATH" --keep-failed 2>&1 | tee "$BUILD_LOG" || true)
  70. BUILD_PATH=$(echo "$REALISE_OUT" | grep "^/nix/store/" | head -n1 || true)
  71. CORRECT_HASH=""
  72. if [ -n "$BUILD_PATH" ] && [ -d "$BUILD_PATH" ]; then
  73. echo "Realized node_modules output: $BUILD_PATH"
  74. CORRECT_HASH=$(nix hash path --sri "$BUILD_PATH" 2>/dev/null || true)
  75. fi
  76. # Try to extract hash from build log
  77. if [ -z "$CORRECT_HASH" ]; then
  78. CORRECT_HASH="$(grep -E 'got:\s+sha256-[A-Za-z0-9+/=]+' "$BUILD_LOG" | awk '{print $2}' | head -n1 || true)"
  79. fi
  80. if [ -z "$CORRECT_HASH" ]; then
  81. CORRECT_HASH="$(grep -A2 'hash mismatch' "$BUILD_LOG" | grep 'got:' | awk '{print $2}' | sed 's/sha256:/sha256-/' || true)"
  82. fi
  83. # Try to hash from kept failed build directory
  84. if [ -z "$CORRECT_HASH" ]; then
  85. KEPT_DIR=$(grep -oE "build directory.*'[^']+'" "$BUILD_LOG" | grep -oE "'/[^']+'" | tr -d "'" | head -n1 || true)
  86. if [ -z "$KEPT_DIR" ]; then
  87. KEPT_DIR=$(grep -oE '/nix/var/nix/builds/[^ ]+' "$BUILD_LOG" | head -n1 || true)
  88. fi
  89. if [ -n "$KEPT_DIR" ] && [ -d "$KEPT_DIR" ]; then
  90. HASH_PATH="$KEPT_DIR"
  91. [ -d "$KEPT_DIR/build" ] && HASH_PATH="$KEPT_DIR/build"
  92. if [ -d "$HASH_PATH/node_modules" ]; then
  93. CORRECT_HASH=$(nix hash path --sri "$HASH_PATH" 2>/dev/null || true)
  94. fi
  95. fi
  96. fi
  97. if [ -z "$CORRECT_HASH" ]; then
  98. echo "Failed to determine correct node_modules hash for ${SYSTEM}."
  99. cat "$BUILD_LOG"
  100. exit 1
  101. fi
  102. echo "$CORRECT_HASH" > "$OUTPUT_FILE"
  103. echo "Hash for ${SYSTEM}: $CORRECT_HASH"
  104. - name: Upload hash artifact
  105. uses: actions/upload-artifact@v6
  106. with:
  107. name: hash-${{ matrix.system }}
  108. path: hash-${{ matrix.system }}.txt
  109. retention-days: 1
  110. commit-node-modules-hashes:
  111. needs: compute-node-modules-hash
  112. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
  113. runs-on: blacksmith-4vcpu-ubuntu-2404
  114. env:
  115. TITLE: node_modules hashes
  116. steps:
  117. - name: Checkout repository
  118. uses: actions/checkout@v6
  119. with:
  120. token: ${{ secrets.GITHUB_TOKEN }}
  121. fetch-depth: 0
  122. ref: ${{ github.head_ref || github.ref_name }}
  123. repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
  124. - name: Configure git
  125. run: |
  126. git config --global user.email "action@github.com"
  127. git config --global user.name "Github Action"
  128. - name: Pull latest changes
  129. env:
  130. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  131. run: |
  132. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  133. git pull --rebase --autostash origin "$BRANCH"
  134. - name: Download all hash artifacts
  135. uses: actions/download-artifact@v7
  136. with:
  137. pattern: hash-*
  138. merge-multiple: true
  139. - name: Merge hashes into hashes.json
  140. run: |
  141. set -euo pipefail
  142. HASH_FILE="nix/hashes.json"
  143. if [ ! -f "$HASH_FILE" ]; then
  144. mkdir -p "$(dirname "$HASH_FILE")"
  145. echo '{"nodeModules":{}}' > "$HASH_FILE"
  146. fi
  147. echo "Merging hashes into ${HASH_FILE}..."
  148. shopt -s nullglob
  149. files=(hash-*.txt)
  150. if [ ${#files[@]} -eq 0 ]; then
  151. echo "No hash files found, nothing to update"
  152. exit 0
  153. fi
  154. EXPECTED_SYSTEMS="x86_64-linux aarch64-linux x86_64-darwin aarch64-darwin"
  155. for sys in $EXPECTED_SYSTEMS; do
  156. if [ ! -f "hash-${sys}.txt" ]; then
  157. echo "WARNING: Missing hash file for $sys"
  158. fi
  159. done
  160. for f in "${files[@]}"; do
  161. system="${f#hash-}"
  162. system="${system%.txt}"
  163. hash=$(cat "$f")
  164. if [ -z "$hash" ]; then
  165. echo "WARNING: Empty hash for $system, skipping"
  166. continue
  167. fi
  168. echo " $system: $hash"
  169. jq --arg sys "$system" --arg h "$hash" \
  170. '.nodeModules = (.nodeModules // {}) | .nodeModules[$sys] = $h' "$HASH_FILE" > "${HASH_FILE}.tmp"
  171. mv "${HASH_FILE}.tmp" "$HASH_FILE"
  172. done
  173. echo "All hashes merged:"
  174. cat "$HASH_FILE"
  175. - name: Commit ${{ env.TITLE }} changes
  176. env:
  177. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  178. run: |
  179. set -euo pipefail
  180. HASH_FILE="nix/hashes.json"
  181. echo "Checking for changes..."
  182. summarize() {
  183. local status="$1"
  184. {
  185. echo "### Nix $TITLE"
  186. echo ""
  187. echo "- ref: ${GITHUB_REF_NAME}"
  188. echo "- status: ${status}"
  189. } >> "$GITHUB_STEP_SUMMARY"
  190. if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
  191. echo "- run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" >> "$GITHUB_STEP_SUMMARY"
  192. fi
  193. echo "" >> "$GITHUB_STEP_SUMMARY"
  194. }
  195. FILES=("$HASH_FILE")
  196. STATUS="$(git status --short -- "${FILES[@]}" || true)"
  197. if [ -z "$STATUS" ]; then
  198. echo "No changes detected."
  199. summarize "no changes"
  200. exit 0
  201. fi
  202. echo "Changes detected:"
  203. echo "$STATUS"
  204. git add "${FILES[@]}"
  205. git commit -m "Update $TITLE"
  206. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  207. git pull --rebase --autostash origin "$BRANCH"
  208. git push origin HEAD:"$BRANCH"
  209. echo "Changes pushed successfully"
  210. summarize "committed $(git rev-parse --short HEAD)"