Skip to content

Instantly share code, notes, and snippets.

@cristianoliveira
Created October 5, 2024 18:53
Show Gist options
  • Save cristianoliveira/ebe252285a2b297b60756b8052f980cb to your computer and use it in GitHub Desktop.
Save cristianoliveira/ebe252285a2b297b60756b8052f980cb to your computer and use it in GitHub Desktop.
bash script generate nix package hashes
#!/usr/bin/env bash
# First get version from the latest git tag
VERSION="$(git describe --tags --abbrev=0)"
NIX_FILE="nix/package.nix"
echo "Bumping version to $VERSION"
# replace version = "local-2024-09-16"; within "$NIX_FILE"
sed -i "s/version = \".*\";/version = \"$VERSION\";/" "$NIX_FILE"
sed -i 's/sha256-.*=//g' "$NIX_FILE"
## nix build and pipe the error to a build.log file
rm -f build.log
nix build . 2> build.log
# format
# specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
# got: sha256-crOHNrO4KkHn3kKGlwitirBpsgtAGXpDKBORmpVUKFs=
SHA256=$(grep "got:" build.log | grep -o "sha256-.*=" | cut -d'-' -f2)
echo "git hash SHA256: $SHA256"
sed -i "s# hash = \".*\";# hash = \"sha256-$SHA256\";#" "$NIX_FILE"
nix build . 2> build.log
SHA256=$(grep "got:" build.log | grep -o "sha256-.*=" | cut -d'-' -f2)
echo "cargo hash SHA256: $SHA256"
sed -i "s#cargoHash = \".*\";#cargoHash = \"sha256-$SHA256\";#" "$NIX_FILE"
echo "Building nix derivation"
nix build .
rm -f build.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment