nixfiles/pkgs/nyastodon/update.sh
2024-07-04 08:36:44 +02:00

113 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bundix coreutils diffutils nix-prefetch-git gnused jq prefetch-yarn-deps yarn-lock-converter
set -e
URL=https://woof.rip/emily/nyastodon.git
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--URL)
URL="$2"
shift # past argument
shift # past value
;;
--ver)
VERSION="$2"
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
if [[ -n "$POSITIONAL" ]]; then
echo "Usage: update.sh [--url URL] [--ver VERSION] [--rev REVISION] [--patches PATCHES]"
echo "If URL is not provided, it defaults to https://git.bsd.gay/fef/nyastodon.git"
echo "If VERSION is not provided, it defaults to the latest git revision."
echo "PATCHES, if provided, should be one or more Nix expressions separated by spaces."
exit 1
fi
rm -f gemset.nix source.nix
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
WORK_DIR=$(mktemp -d)
# Check that working directory was created.
if [[ -z "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
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
if [[ -z "$REVISION" ]]; then
echo "Fetching latest source code from $URL"
JSON=$(nix-prefetch-git "$URL" 2> $WORK_DIR/nix-prefetch-git.out)
REVISION=$(echo "$JSON" | jq -r .rev)
else
echo "Fetching source code $REVISION"
JSON=$(nix-prefetch-git "$URL" "$REVISION" 2> $WORK_DIR/nix-prefetch-git.out)
fi
if [[ -z "$VERSION" ]]; then
VERSION=$REVISION
fi
HASH=$(echo "$JSON" | jq -r .hash)
cat > source.nix << EOF
# This file was generated by pkgs.mastodon.updateScript.
{ fetchgit, applyPatches, patches ? [] }:
let
version = "$VERSION";
in
(
applyPatches {
src = fetchgit {
url = "$URL";
rev = "$REVISION";
hash = "$HASH";
};
patches = patches ++ [$PATCHES];
}) // {
inherit version;
yarnHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}
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"
echo "" >> gemset.nix # Create trailing newline to please EditorConfig checks
#echo "Need to convert the yarn.lock, otherwise prefetch-yarn-deps will fail to parse it"
## HACK: run yarn-lock-converter, prefetch-yarn-deps doesn't handle versions that aren't quoted
#time yarn-lock-converter -i "$SOURCE_DIR/yarn.lock" -o "yarn-converted.lock"
#echo "done converting yarn.lock"
#
#echo "Creating yarn-hash.nix from yarn-converted.lock"
#YARN_HASH="$(prefetch-yarn-deps "yarn-converted.lock")"
#YARN_HASH="$(nix hash to-sri --type sha256 "$YARN_HASH")"
#sed -i "s#sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=#$YARN_HASH#g" source.nix