Skip to content

Commit 2234063

Browse files
committed
Another attempt at calculating Nix hashes
1 parent 1e692fd commit 2234063

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

.github/scripts/nix-hashes.sh

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,42 @@ echo "Calculating hashes for celq v$NEW_VERSION"
2525
echo "================================================"
2626
echo
2727

28-
# Create temporary file
29-
TMP_FILE="$NIX_FILE.tmp"
30-
cp "$NIX_FILE" "$TMP_FILE"
31-
32-
# Update version in temporary file
33-
sed -i.bak "s/version = \"[^\"]*\";/version = \"$NEW_VERSION\";/" "$TMP_FILE"
34-
echo "Step 1: Updated version to $NEW_VERSION in temporary file"
35-
echo
36-
37-
# Replace main file temporarily
28+
# Backup the original file first
3829
BACKUP_FILE="$NIX_FILE.backup"
39-
mv "$NIX_FILE" "$BACKUP_FILE"
40-
mv "$TMP_FILE" "$NIX_FILE"
30+
cp "$NIX_FILE" "$BACKUP_FILE"
4131

4232
# Function to restore original file on exit
4333
cleanup() {
4434
if [ -f "$BACKUP_FILE" ]; then
45-
mv "$BACKUP_FILE" "$NIX_FILE"
46-
rm -f "$NIX_FILE.bak" "$TMP_FILE.bak" 2>/dev/null || true
35+
cp "$BACKUP_FILE" "$NIX_FILE"
36+
rm -f "$BACKUP_FILE"
37+
rm -f "$NIX_FILE.bak" 2>/dev/null || true
4738
echo
4839
echo "✓ Original file restored"
4940
fi
5041
}
5142
trap cleanup EXIT
5243

44+
# Step 1: Update version
45+
echo "Step 1: Updating version to $NEW_VERSION"
46+
sed -i.bak "s/version = \"[^\"]*\";/version = \"$NEW_VERSION\";/" "$NIX_FILE"
47+
rm -f "$NIX_FILE.bak"
48+
echo
49+
5350
# Step 2: Calculate fetchCrate hash
5451
echo "Step 2: Calculating fetchCrate hash..."
5552
echo "(This may take a moment...)"
5653

5754
set +e
58-
BUILD_OUTPUT=$(nix build .#celq 2>&1)
59-
FETCH_HASH=$(echo "$BUILD_OUTPUT" | grep -oP "got:\s+sha256-\K[A-Za-z0-9+/=]+" | head -1)
55+
BUILD_OUTPUT=$(nix build . 2>&1)
56+
BUILD_EXIT=$?
6057
set -e
6158

59+
# Extract hash from build output
60+
FETCH_HASH=$(echo "$BUILD_OUTPUT" | grep -oP "got:\s+sha256-\K[A-Za-z0-9+/=]+" | head -1)
61+
6262
if [ -z "$FETCH_HASH" ]; then
63-
# If the build didn't fail, extract current hash
63+
# If the build didn't fail with hash error, extract current hash
6464
FETCH_HASH=$(grep -oP 'sha256 = "sha256-\K[^"]+' "$NIX_FILE" | head -1)
6565
fi
6666

@@ -75,21 +75,26 @@ FETCH_HASH_FORMATTED="sha256-$FETCH_HASH"
7575
echo "✓ fetchCrate hash: $FETCH_HASH_FORMATTED"
7676
echo
7777

78-
# Update the temporary file with the correct fetchCrate hash
78+
# Update the file with the correct fetchCrate hash
7979
sed -i.bak "s|sha256 = \"sha256-[^\"]*\";|sha256 = \"$FETCH_HASH_FORMATTED\";|" "$NIX_FILE"
80+
rm -f "$NIX_FILE.bak"
8081

8182
# Step 3: Calculate cargoHash
8283
echo "Step 3: Calculating cargoHash..."
8384
echo "(This will trigger a build error to extract the correct hash...)"
8485

85-
# Set dummy hash
86+
# Set dummy hash to force error
8687
sed -i.bak 's/cargoHash = "sha256-[^"]*";/cargoHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";/' "$NIX_FILE"
88+
rm -f "$NIX_FILE.bak"
8789

8890
set +e
89-
BUILD_OUTPUT=$(nix build .#celq 2>&1)
90-
CARGO_HASH=$(echo "$BUILD_OUTPUT" | grep -oP "got:\s+sha256-\K[A-Za-z0-9+/=]+" | tail -1)
91+
BUILD_OUTPUT=$(nix build . 2>&1)
92+
BUILD_EXIT=$?
9193
set -e
9294

95+
# Extract cargoHash from error message
96+
CARGO_HASH=$(echo "$BUILD_OUTPUT" | grep -oP "got:\s+sha256-\K[A-Za-z0-9+/=]+" | tail -1)
97+
9398
if [ -z "$CARGO_HASH" ]; then
9499
echo "❌ Failed to extract cargoHash"
95100
echo "Build output:"

0 commit comments

Comments
 (0)