Skip to content

Commit 1e692fd

Browse files
committed
Add tool to calculate Nix hash
1 parent fbd2ca8 commit 1e692fd

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

.github/scripts/nix-hashes.sh

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Script to calculate celq nixpkg hashes for a new version
5+
# Usage: .github/scripts/nix_hashes.sh <new_version>
6+
7+
NEW_VERSION="${1:-}"
8+
9+
if [ -z "$NEW_VERSION" ]; then
10+
echo "Error: Please provide a version number"
11+
echo "Usage: $0 <new_version>"
12+
echo "Example: $0 0.3.0"
13+
exit 1
14+
fi
15+
16+
NIX_FILE="nix/celq.nix"
17+
18+
if [ ! -f "$NIX_FILE" ]; then
19+
echo "Error: $NIX_FILE not found"
20+
exit 1
21+
fi
22+
23+
echo "================================================"
24+
echo "Calculating hashes for celq v$NEW_VERSION"
25+
echo "================================================"
26+
echo
27+
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
38+
BACKUP_FILE="$NIX_FILE.backup"
39+
mv "$NIX_FILE" "$BACKUP_FILE"
40+
mv "$TMP_FILE" "$NIX_FILE"
41+
42+
# Function to restore original file on exit
43+
cleanup() {
44+
if [ -f "$BACKUP_FILE" ]; then
45+
mv "$BACKUP_FILE" "$NIX_FILE"
46+
rm -f "$NIX_FILE.bak" "$TMP_FILE.bak" 2>/dev/null || true
47+
echo
48+
echo "✓ Original file restored"
49+
fi
50+
}
51+
trap cleanup EXIT
52+
53+
# Step 2: Calculate fetchCrate hash
54+
echo "Step 2: Calculating fetchCrate hash..."
55+
echo "(This may take a moment...)"
56+
57+
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)
60+
set -e
61+
62+
if [ -z "$FETCH_HASH" ]; then
63+
# If the build didn't fail, extract current hash
64+
FETCH_HASH=$(grep -oP 'sha256 = "sha256-\K[^"]+' "$NIX_FILE" | head -1)
65+
fi
66+
67+
if [ -z "$FETCH_HASH" ]; then
68+
echo "❌ Failed to extract fetchCrate hash"
69+
echo "Build output:"
70+
echo "$BUILD_OUTPUT"
71+
exit 1
72+
fi
73+
74+
FETCH_HASH_FORMATTED="sha256-$FETCH_HASH"
75+
echo "✓ fetchCrate hash: $FETCH_HASH_FORMATTED"
76+
echo
77+
78+
# Update the temporary file with the correct fetchCrate hash
79+
sed -i.bak "s|sha256 = \"sha256-[^\"]*\";|sha256 = \"$FETCH_HASH_FORMATTED\";|" "$NIX_FILE"
80+
81+
# Step 3: Calculate cargoHash
82+
echo "Step 3: Calculating cargoHash..."
83+
echo "(This will trigger a build error to extract the correct hash...)"
84+
85+
# Set dummy hash
86+
sed -i.bak 's/cargoHash = "sha256-[^"]*";/cargoHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";/' "$NIX_FILE"
87+
88+
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+
set -e
92+
93+
if [ -z "$CARGO_HASH" ]; then
94+
echo "❌ Failed to extract cargoHash"
95+
echo "Build output:"
96+
echo "$BUILD_OUTPUT"
97+
exit 1
98+
fi
99+
100+
CARGO_HASH_FORMATTED="sha256-$CARGO_HASH"
101+
echo "✓ cargoHash: $CARGO_HASH_FORMATTED"
102+
echo
103+
104+
# Output results
105+
echo "================================================"
106+
echo "Hash Calculation Results for celq v$NEW_VERSION"
107+
echo "================================================"
108+
echo
109+
echo "Update the following values in nix/celq.nix:"
110+
echo
111+
echo " version = \"$NEW_VERSION\";"
112+
echo
113+
echo " sha256 = \"$FETCH_HASH_FORMATTED\";"
114+
echo
115+
echo " cargoHash = \"$CARGO_HASH_FORMATTED\";"
116+
echo
117+
echo "================================================"
118+
119+
# Export for GitHub Actions
120+
if [ -n "${GITHUB_OUTPUT:-}" ]; then
121+
echo "FETCH_HASH=$FETCH_HASH_FORMATTED" >> "$GITHUB_OUTPUT"
122+
echo "CARGO_HASH=$CARGO_HASH_FORMATTED" >> "$GITHUB_OUTPUT"
123+
fi
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Calculate celq nixpkg hashes
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
new_version:
7+
description: 'New version to calculate hashes for (e.g., 0.3.0)'
8+
required: true
9+
type: string
10+
11+
jobs:
12+
calculate-hashes:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Install Nix
20+
uses: cachix/install-nix-action@v26
21+
with:
22+
nix_path: nixpkgs=channel:nixos-unstable
23+
extra_nix_config: |
24+
experimental-features = nix-command flakes
25+
26+
- name: Make script executable
27+
run: chmod +x .github/scripts/nix-hashes.sh
28+
29+
- name: Calculate hashes
30+
id: hashes
31+
run: .github/scripts/nix-hashes.sh "${{ inputs.new_version }}"

0 commit comments

Comments
 (0)