nix-infra/common/pkgs/mastodon/update.sh

101 lines
2.8 KiB
Bash
Raw Normal View History

2023-12-17 20:05:44 +00:00
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bundix coreutils diffutils nix-prefetch-github gnused jq prefetch-yarn-deps
2023-04-30 10:49:28 +00:00
set -e
2023-12-17 20:05:44 +00:00
OWNER=mastodon
REPO=mastodon
2023-04-30 10:49:28 +00:00
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
2023-12-17 20:05:44 +00:00
--owner)
OWNER="$2"
2023-04-30 10:49:28 +00:00
shift # past argument
shift # past value
;;
2023-12-17 20:05:44 +00:00
--repo)
REPO="$2"
2023-04-30 10:49:28 +00:00
shift # past argument
shift # past value
;;
--rev)
REVISION="$2"
shift # past argument
shift # past value
;;
--patches)
PATCHES="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1")
shift # past argument
;;
esac
done
2023-12-17 20:05:44 +00:00
if [[ -n "$POSITIONAL" ]]; then
echo "Usage: update.sh [--owner OWNER] [--repo REPO] [--rev REVISION] [--patches PATCHES]"
echo "OWNER and REPO must be paths on github."
echo "If OWNER and REPO are not provided, it defaults they default to mastodon and mastodon."
echo "PATCHES, if provided, should be one or more Nix expressions separated by spaces."
2023-04-30 10:49:28 +00:00
exit 1
fi
2023-12-17 20:05:44 +00:00
rm -f gemset.nix source.nix
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
2023-04-30 10:49:28 +00:00
WORK_DIR=$(mktemp -d)
# Check that working directory was created.
2023-12-17 20:05:44 +00:00
if [[ -z "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
2023-04-30 10:49:28 +00:00
echo "Could not create temporary directory"
exit 1
fi
# Delete the working directory on exit.
function cleanup {
# Report errors, if any, from nix-prefetch-git
grep "fatal" $WORK_DIR/nix-prefetch-git.out >/dev/stderr || true
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
2023-12-17 20:05:44 +00:00
echo "Fetching source code $REVISION"
JSON=$(nix-prefetch-github "$OWNER" "$REPO" --rev "$REVISION" 2> $WORK_DIR/nix-prefetch-git.out)
HASH=$(echo "$JSON" | jq -r .hash)
2023-04-30 10:49:28 +00:00
cat > source.nix << EOF
# This file was generated by pkgs.mastodon.updateScript.
2023-12-17 20:05:44 +00:00
{ fetchFromGitHub, applyPatches, patches ? [] }:
let
version = "$REVISION";
in
(
applyPatches {
src = fetchFromGitHub {
owner = "$OWNER";
repo = "$REPO";
rev = "\${version}";
hash = "$HASH";
};
patches = patches ++ [$PATCHES];
}) // {
inherit version;
yarnHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
2023-04-30 10:49:28 +00:00
}
EOF
SOURCE_DIR="$(nix-build --no-out-link -E '(import <nixpkgs> {}).callPackage ./source.nix {}')"
echo "Creating gemset.nix"
bundix --lockfile="$SOURCE_DIR/Gemfile.lock" --gemfile="$SOURCE_DIR/Gemfile"
2023-12-17 20:05:44 +00:00
echo "" >> gemset.nix # Create trailing newline to please EditorConfig checks
echo "Creating yarn-hash.nix"
YARN_HASH="$(prefetch-yarn-deps "$SOURCE_DIR/yarn.lock")"
YARN_HASH="$(nix hash to-sri --type sha256 "$YARN_HASH")"
sed -i "s/sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=/$YARN_HASH/g" source.nix