update-nix-hashes.yml 11 KB

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