nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p nix curl jq common-updater-scripts
3
4set -eu -o pipefail
5
6cd "${0%/*}"/../../../../../
7
8
9readonly plugin_name="$1"
10readonly api_response="$(curl --silent "https://grafana.com/api/plugins/${plugin_name}")"
11readonly latest_version="$(echo "$api_response" | jq -r .version)"
12
13update() {
14 local system="${2:+--system=$2}"
15 local pkg="$(echo "$api_response" | jq -e .packages.\""$1"\")"
16 if echo "$pkg" | jq -er .sha256 > /dev/null; then
17 local hash="$(echo "$pkg" | jq -er .sha256)"
18 else
19 # Some packages only have an md5 hash. Download the file for hash computation.
20 local urlPath="$(echo "$pkg" | jq -er .downloadUrl)"
21 local hash="$(nix-prefetch-url --type sha256 --name "$plugin_name" "https://grafana.com$urlPath")"
22 fi
23 hash="$(nix --extra-experimental-features nix-command hash to-sri --type sha256 "$hash")"
24 # Set version number to a random number first to force update to happen.
25 #
26 # `update-source-version` will exit early if it considers the version number to be the same.
27 # However we have already downloaded the file and computed the hash, so it makes sense to set
28 # the newly computed information unconditionally.
29 #
30 # As an example of a workflow that was made more complicated than strictly necessary is my own
31 # attempts to improve this script where I spent multiple hours investigating why the update
32 # script would refuse to update a hash that I intentionally malformed (in hopes to test the
33 # operation of this script.)
34 update-source-version $system "grafanaPlugins.${plugin_name}" $RANDOM "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
35 update-source-version $system "grafanaPlugins.${plugin_name}" "$latest_version" "$hash"
36}
37
38if echo "$api_response" | jq -e .packages.any > /dev/null; then
39 # the package contains an "any" package, so there should be only one zipHash.
40 update "any"
41else
42 update "linux-amd64" "x86_64-linux"
43 update "linux-arm64" "aarch64-linux"
44 update "darwin-amd64" "x86_64-darwin"
45 update "darwin-arm64" "aarch64-darwin"
46fi