summary refs log tree commit diff
path: root/pkgs/servers/web-apps/plausible/update.sh
blob: 3abf3ee616fc034cd1c068e38d0b7b722e322de2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p jq nix-prefetch-github yarn yarn2nix-moretea.yarn2nix moreutils

# NOTE: please check on new releases which steps aren't necessary anymore!
# Currently the following things are done:
#
# * Add correct `name`/`version` field to `package.json`, otherwise `yarn2nix` fails to
#   find required dependencies.
# * Keep `tailwindcss` on version 2.0.1-compat (on `yarn` it will be upgraded due to the `^`).
#   This is needed to make sure the entire build still works with `postcss-7` (needed
#   by plausible).
# * Adjust `file:`-dependencies a bit for the structure inside a Nix build.
# * Update hashes for the tarball & the fixed-output drv with all `mix`-dependencies.
# * Generate `yarn.lock` & `yarn.nix` in a temporary directory.

set -euxo pipefail

dir="$(realpath $(dirname "$0"))"
export latest="$(curl -q https://api.github.com/repos/plausible/analytics/releases/latest \
  | jq -r '.tag_name')"
nix_version="$(cut -c2- <<< "$latest")"

if [[ "$(nix-instantiate -A plausible.version --eval --json | jq -r)" = "$nix_version" ]];
then
  echo "Already using version $latest, skipping"
  exit 0
fi

SRC="https://raw.githubusercontent.com/plausible/analytics/${latest}"

package_json="$(curl -qf "$SRC/assets/package.json")"
export fixed_tailwind_version="$(jq '.dependencies.tailwindcss' -r <<< "$package_json" | sed -e 's,^^,,g')"

echo "$package_json" \
  | jq '. + {"name":"plausible","version": $ENV.latest} | .dependencies.tailwindcss = $ENV.fixed_tailwind_version' \
  | sed -e 's,../deps/,../../tmp/deps/,g' \
  > $dir/package.json

tarball_meta="$(nix-prefetch-github plausible analytics --rev "$latest")"
tarball_hash="$(nix to-base32 sha256-$(jq -r '.sha256' <<< "$tarball_meta"))"
tarball_path="$(nix-build -E 'with import ./. {}; { p }: fetchFromGitHub (builtins.fromJSON p)' --argstr p "$tarball_meta")"
fake_hash="$(nix-instantiate --eval -A lib.fakeSha256 | xargs echo)"

sed -i "$dir/default.nix" \
  -e 's,version = ".*",version = "'"$nix_version"'",' \
  -e '/^  src = fetchFromGitHub/,+4{;s/sha256 = "\(.*\)"/sha256 = "'"$tarball_hash"'"/}' \
  -e '/^  mixFodDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$fake_hash"'"/}'

mix_hash="$(nix to-base32 $(nix-build -A plausible.mixFodDeps 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo || true))"

sed -i "$dir/default.nix" -e '/^  mixFodDeps =/,+3{;s/sha256 = "\(.*\)"/sha256 = "'"$mix_hash"'"/}'

tmp_setup_dir="$(mktemp -d)"
trap "rm -rf $tmp_setup_dir" EXIT

cp -r $tarball_path/* $tmp_setup_dir/
cp -r "$(nix-build -A plausible.mixFodDeps)" "$tmp_setup_dir/deps"
chmod -R u+rwx "$tmp_setup_dir"

pushd $tmp_setup_dir/assets
jq < package.json '.dependencies.tailwindcss = "'"$fixed_tailwind_version"'"' | sponge package.json
yarn
yarn2nix > "$dir/yarn.nix"
cp yarn.lock "$dir/yarn.lock"
popd

nix-build -A plausible