nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 89 lines 3.2 kB view raw
1#!/usr/bin/env nix-shell 2#!nix-shell -i bash -p curl git wget jq common-updater-scripts yarn-berry_4 yarn-berry_4.yarn-berry-fetcher tomlq nix-prefetch-github 3 4set -eu -o pipefail 5set -x 6 7TMPDIR=/tmp/anki-update-script 8 9cleanup() { 10 if [ -e $TMPDIR/.done ]; then 11 rm -rf "$TMPDIR" 12 else 13 echo 14 read -p "Script exited prematurely. Do you want to delete the temporary directory $TMPDIR ? " -n 1 -r 15 echo 16 if [[ $REPLY =~ ^[Yy]$ ]]; then 17 rm -rf "$TMPDIR" 18 fi 19 fi 20} 21 22trap cleanup EXIT 23 24if [[ "$#" > 0 ]]; then 25 tag="$1" 26else 27 tag="$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s 'https://api.github.com/repos/ankitects/anki/releases' | jq -r 'map(select(.prerelease == false)) | .[0].tag_name')" 28fi 29tag_sha="$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s "https://api.github.com/repos/ankitects/anki/git/ref/tags/$tag" | jq -r '.object.sha')" 30rev="$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s "https://api.github.com/repos/ankitects/anki/git/tags/$tag_sha" | jq -r '.object.sha')" 31 32nixpkgs="$(git rev-parse --show-toplevel)" 33scriptDir="$nixpkgs/pkgs/by-name/an/anki" 34 35ver=$(nix-instantiate --eval -E "(import \"$nixpkgs\" { config = {}; overlays = []; }).anki.version" | tr -d '"') 36 37if [[ "$tag" == "$ver" ]]; then 38 echo "Latest version is $tag, already $ver, skipping update" 39 exit 0 40fi 41echo "Updating from $ver to $tag" 42 43mkdir -p $TMPDIR 44 45curl -o $TMPDIR/yarn.lock "https://raw.githubusercontent.com/ankitects/anki/refs/tags/$tag/yarn.lock" 46 47echo "Generating missing-hashes.json" 48yarn-berry-fetcher missing-hashes $TMPDIR/yarn.lock > $TMPDIR/missing-hashes.json 49yarnHash=$(yarn-berry-fetcher prefetch $TMPDIR/yarn.lock $TMPDIR/missing-hashes.json) 50 51echo "Copying missing-hashes.json back into nixpkgs" 52cp $TMPDIR/missing-hashes.json "$scriptDir/missing-hashes.json" 53 54sed -i -E "s|yarnHash = \".*\"|yarnHash = \"$yarnHash\"|" "$scriptDir/package.nix" 55 56echo "yarnHash updated" 57echo "Regenerating uv-deps.json" 58 59curl -o $TMPDIR/uv.lock "https://raw.githubusercontent.com/ankitects/anki/refs/tags/$tag/uv.lock" 60 61# Extract all urls to pre-compute hashes so we can download whatever uv needs for its cache. 62# We skip pyqt because the derivation uses the nixos packaged ones for 63# native-library compatibility. 64tq -f $TMPDIR/uv.lock --output json '.' | jq '.. | objects | .url | select(. != null)' -cr | \ 65 grep -Ev "PyQt|pyqt" \ 66 > $TMPDIR/uv.urls 67 68echo '[' > $TMPDIR/uv-deps.json 69for url in $(cat $TMPDIR/uv.urls); do 70 urlHash="$(nix-prefetch-url --type sha256 "$url")" 71 echo '{"url": "'$url'", "hash": "'$(nix-hash --type sha256 --to-sri $urlHash)'"},' >> $TMPDIR/uv-deps.json 72done 73# strip final trailing comma 74sed '$s/,$//' -i $TMPDIR/uv-deps.json 75echo ']' >> $TMPDIR/uv-deps.json 76 77# and jq format it on the way into nixpkgs too 78jq '.' $TMPDIR/uv-deps.json > "$scriptDir/uv-deps.json" 79echo "Wrote uv-deps.json" 80 81# github as well 82 83srcHash="$(nix-prefetch-github ankitects anki --fetch-submodules --rev "$tag" --json | jq -r '.hash')" 84 85sed -i "s|version = \".*\";|version = \"$tag\";|" "$scriptDir/package.nix" 86sed -i "s|rev = \".*\";|rev = \"$rev\";|" "$scriptDir/package.nix" 87sed -i "s|srcHash = \".*\";|srcHash = \"$srcHash\";|" "$scriptDir/package.nix" 88 89touch $TMPDIR/.done