1#!/usr/bin/env nix-shell
2#! nix-shell -i bash -p common-updater-scripts curl jq git gnused gnugrep libplist undmg
3set -euo pipefail
4
5
6# executing this script without arguments will
7# - find the newest stable spotify version avaiable on snapcraft (https://snapcraft.io/spotify)
8# - read the current spotify version from the current nix expression
9# - update the nix expression if the versions differ
10# - try to build the updated version, exit if that fails
11# - give instructions for upstreaming
12
13# Please test the update manually before pushing. There have been errors before
14# and because the service is proprietary and a paid account is necessary to do
15# anything with spotify automatic testing is not possible.
16
17# As an optional argument you can specify the snapcraft channel to update to.
18# Default is `stable` and only stable updates should be pushed to nixpkgs. For
19# testing you may specify `candidate` or `edge`.
20
21
22channel="${1:-stable}" # stable/candidate/edge
23nixpkgs="$(git rev-parse --show-toplevel)"
24
25update_linux() {
26 nix_file="$nixpkgs/pkgs/by-name/sp/spotify/linux.nix"
27 #
28 # find the newest stable spotify version available on snapcraft
29 #
30
31 # create bash array from snap info
32 snap_info=($(
33 curl -s -H 'X-Ubuntu-Series: 16' \
34 "https://api.snapcraft.io/api/v1/snaps/details/spotify?channel=$channel" \
35 | jq --raw-output \
36 '.revision,.download_sha512,.version,.last_updated'
37 ))
38
39 # "revision" is the actual version identifier on snapcraft, the "version" is
40 # just for human consumption. Revision is just an integer that gets increased
41 # by one every (stable or unstable) release.
42 revision="${snap_info[0]}"
43 # We need to escape the slashes
44 hash="$(nix-hash --to-sri --type sha512 ${snap_info[1]} | sed 's|/|\\/|g')"
45 upstream_version="${snap_info[2]}"
46 last_updated="${snap_info[3]}"
47 echo "Latest $channel release for Spotify on Linux is $upstream_version from $last_updated."
48 #
49 # read the current spotify version from the currently *committed* nix expression
50 #
51
52 current_nix_version=$(
53 grep 'version\s*=' "$nix_file" \
54 | sed -Ene 's/.*"(.*)".*/\1/p'
55 )
56
57 echo "Current Spotify for Linux version in Nixpkgs: $current_nix_version"
58
59 #
60 # update the nix expression if the versions differ
61 #
62
63 if [[ "$current_nix_version" = "$upstream_version" ]]; then
64 echo "Spotify on Linux is already up-to-date"
65 return
66 fi
67
68 echo "Updating Spotify on Linux from ${current_nix_version} to ${upstream_version}, released on ${last_updated}"
69
70 # search-and-replace revision, hash and version
71 sed --regexp-extended \
72 -e 's/rev\s*=\s*"[0-9]+"\s*;/rev = "'"${revision}"'";/' \
73 -e 's/hash\s*=\s*"[^"]*"\s*;/hash = "'"${hash}"'";/' \
74 -e 's/version\s*=\s*".*"\s*;/version = "'"${upstream_version}"'";/' \
75 -i "$nix_file"
76}
77
78update_macos() {
79 nix_file="$nixpkgs/pkgs/by-name/sp/spotify/darwin.nix"
80
81 tmp_dir=$(mktemp -d)
82 trap 'rm -rf "$tmp_dir"' EXIT
83
84 pushd $tmp_dir
85
86 x86_64_url="https://download.scdn.co/Spotify.dmg"
87 aarch64_url="https://download.scdn.co/SpotifyARM64.dmg"
88
89 curl -OL $aarch64_url
90 undmg SpotifyARM64.dmg
91 upstream_version=$(plistutil -i Spotify.app/Contents/Info.plist -f json -o - | jq -r '.CFBundleVersion')
92
93 popd
94
95 current_nix_version=$(
96 grep 'version\s*=' "$nix_file" \
97 | sed -Ene 's/.*"(.*)".*/\1/p'
98 )
99
100 if [[ "$current_nix_version" != "$upstream_version" ]]; then
101 archive_url="https://web.archive.org/save"
102 archived_x86_64_url=$(curl -s -I -L -o /dev/null "$archive_url/$x86_64_url" -w '%{url_effective}')
103 archived_aarch64_url=$(curl -s -I -L -o /dev/null "$archive_url/$aarch64_url" -w '%{url_effective}')
104
105 update-source-version "pkgsCross.x86_64-darwin.spotify" "$upstream_version" "" "$archived_x86_64_url" \
106 --file=$nix_file \
107 --ignore-same-version
108
109 update-source-version "pkgsCross.aarch64-darwin.spotify" "$upstream_version" "" "$archived_aarch64_url" \
110 --file=$nix_file \
111 --ignore-same-version
112 fi
113}
114
115update_linux
116update_macos