nix-hashes.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. name: 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. - "flake.lock"
  12. - ".github/workflows/nix-hashes.yml"
  13. pull_request:
  14. paths:
  15. - "bun.lock"
  16. - "package.json"
  17. - "packages/*/package.json"
  18. - "flake.lock"
  19. - ".github/workflows/nix-hashes.yml"
  20. jobs:
  21. nix-hashes:
  22. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
  23. runs-on: blacksmith-4vcpu-ubuntu-2404
  24. env:
  25. TITLE: node_modules hashes
  26. steps:
  27. - name: Checkout repository
  28. uses: actions/checkout@v6
  29. with:
  30. token: ${{ secrets.GITHUB_TOKEN }}
  31. fetch-depth: 0
  32. ref: ${{ github.head_ref || github.ref_name }}
  33. repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
  34. - name: Setup git committer
  35. id: committer
  36. uses: ./.github/actions/setup-git-committer
  37. with:
  38. opencode-app-id: ${{ vars.OPENCODE_APP_ID }}
  39. opencode-app-secret: ${{ secrets.OPENCODE_APP_SECRET }}
  40. - name: Setup Nix
  41. uses: nixbuild/nix-quick-install-action@v34
  42. - name: Pull latest changes
  43. env:
  44. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  45. run: |
  46. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  47. git pull --rebase --autostash origin "$BRANCH"
  48. - name: Compute all node_modules hashes
  49. run: |
  50. set -euo pipefail
  51. HASH_FILE="nix/hashes.json"
  52. SYSTEMS="x86_64-linux aarch64-linux x86_64-darwin aarch64-darwin"
  53. if [ ! -f "$HASH_FILE" ]; then
  54. mkdir -p "$(dirname "$HASH_FILE")"
  55. echo '{"nodeModules":{}}' > "$HASH_FILE"
  56. fi
  57. for SYSTEM in $SYSTEMS; do
  58. echo "Computing hash for ${SYSTEM}..."
  59. BUILD_LOG=$(mktemp)
  60. trap 'rm -f "$BUILD_LOG"' EXIT
  61. # The updater derivations use fakeHash, so they will fail and reveal the correct hash
  62. UPDATER_ATTR=".#packages.x86_64-linux.${SYSTEM}_node_modules"
  63. nix build "$UPDATER_ATTR" --no-link 2>&1 | tee "$BUILD_LOG" || true
  64. CORRECT_HASH="$(grep -E 'got:\s+sha256-[A-Za-z0-9+/=]+' "$BUILD_LOG" | awk '{print $2}' | head -n1 || true)"
  65. if [ -z "$CORRECT_HASH" ]; then
  66. CORRECT_HASH="$(grep -A2 'hash mismatch' "$BUILD_LOG" | grep 'got:' | awk '{print $2}' | sed 's/sha256:/sha256-/' || true)"
  67. fi
  68. if [ -z "$CORRECT_HASH" ]; then
  69. echo "Failed to determine correct node_modules hash for ${SYSTEM}."
  70. cat "$BUILD_LOG"
  71. exit 1
  72. fi
  73. echo " ${SYSTEM}: ${CORRECT_HASH}"
  74. jq --arg sys "$SYSTEM" --arg h "$CORRECT_HASH" \
  75. '.nodeModules[$sys] = $h' "$HASH_FILE" > "${HASH_FILE}.tmp"
  76. mv "${HASH_FILE}.tmp" "$HASH_FILE"
  77. done
  78. echo "All hashes computed:"
  79. cat "$HASH_FILE"
  80. - name: Commit ${{ env.TITLE }} changes
  81. env:
  82. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  83. run: |
  84. set -euo pipefail
  85. HASH_FILE="nix/hashes.json"
  86. echo "Checking for changes..."
  87. summarize() {
  88. local status="$1"
  89. {
  90. echo "### Nix $TITLE"
  91. echo ""
  92. echo "- ref: ${GITHUB_REF_NAME}"
  93. echo "- status: ${status}"
  94. } >> "$GITHUB_STEP_SUMMARY"
  95. if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
  96. echo "- run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" >> "$GITHUB_STEP_SUMMARY"
  97. fi
  98. echo "" >> "$GITHUB_STEP_SUMMARY"
  99. }
  100. FILES=("$HASH_FILE")
  101. STATUS="$(git status --short -- "${FILES[@]}" || true)"
  102. if [ -z "$STATUS" ]; then
  103. echo "No changes detected."
  104. summarize "no changes"
  105. exit 0
  106. fi
  107. echo "Changes detected:"
  108. echo "$STATUS"
  109. git add "${FILES[@]}"
  110. git commit -m "chore: update nix node_modules hashes"
  111. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  112. git pull --rebase --autostash origin "$BRANCH"
  113. git push origin HEAD:"$BRANCH"
  114. echo "Changes pushed successfully"
  115. summarize "committed $(git rev-parse --short HEAD)"