From bea3a3f50c29836b0c6b6f3a29bbe4fbc734996e Mon Sep 17 00:00:00 2001 From: Matteo Pacini Date: Sun, 7 Jul 2024 21:54:26 +0100 Subject: [PATCH] miru: added passthru.updateScript --- pkgs/by-name/mi/miru/darwin.nix | 8 ++++- pkgs/by-name/mi/miru/linux.nix | 10 ++++-- pkgs/by-name/mi/miru/package.nix | 21 +++++++++++-- pkgs/by-name/mi/miru/update.sh | 52 ++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100755 pkgs/by-name/mi/miru/update.sh diff --git a/pkgs/by-name/mi/miru/darwin.nix b/pkgs/by-name/mi/miru/darwin.nix index c93a20453bba..e9939f35cb41 100644 --- a/pkgs/by-name/mi/miru/darwin.nix +++ b/pkgs/by-name/mi/miru/darwin.nix @@ -7,9 +7,15 @@ pname, version, meta, + passthru, }: stdenvNoCC.mkDerivation rec { - inherit pname version meta; + inherit + pname + version + meta + passthru + ; src = fetchurl { url = "https://github.com/ThaUnknown/miru/releases/download/v${version}/mac-Miru-${version}-mac.zip"; diff --git a/pkgs/by-name/mi/miru/linux.nix b/pkgs/by-name/mi/miru/linux.nix index 43f59abd1a8b..aa36804c3089 100644 --- a/pkgs/by-name/mi/miru/linux.nix +++ b/pkgs/by-name/mi/miru/linux.nix @@ -5,15 +5,21 @@ pname, version, meta, + passthru, }: appimageTools.wrapType2 rec { - inherit pname version meta; + inherit + pname + version + meta + passthru + ; src = fetchurl { url = "https://github.com/ThaUnknown/miru/releases/download/v${version}/linux-Miru-${version}.AppImage"; name = "${pname}-${version}.AppImage"; - sha256 = "sha256-wnqCKnZKt0Fj8TasdRVzI558W7aIB5FLkcDEiZfz3ZQ="; + hash = "sha256-wnqCKnZKt0Fj8TasdRVzI558W7aIB5FLkcDEiZfz3ZQ="; }; extraInstallCommands = diff --git a/pkgs/by-name/mi/miru/package.nix b/pkgs/by-name/mi/miru/package.nix index 0b7a2e1d8755..a63389875d34 100644 --- a/pkgs/by-name/mi/miru/package.nix +++ b/pkgs/by-name/mi/miru/package.nix @@ -32,8 +32,25 @@ let instead of flat out closing MPV. ''; }; + passthru = { + updateScript = ./update.sh; + }; in if stdenv.isDarwin then - callPackage ./darwin.nix { inherit pname version meta; } + callPackage ./darwin.nix { + inherit + pname + version + meta + passthru + ; + } else - callPackage ./linux.nix { inherit pname version meta; } + callPackage ./linux.nix { + inherit + pname + version + meta + passthru + ; + } diff --git a/pkgs/by-name/mi/miru/update.sh b/pkgs/by-name/mi/miru/update.sh new file mode 100755 index 000000000000..e2d2cba736a6 --- /dev/null +++ b/pkgs/by-name/mi/miru/update.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p curl jq gnused + +set -euo pipefail + +ROOT="$(dirname "$(readlink -f "$0")")" +if [[ ! "$(basename $ROOT)" == "miru" || ! -f "$ROOT/package.nix" ]]; then + echo "error: Not in the miru folder" >&2 + exit 1 +fi + +PACKAGE_NIX="$ROOT/package.nix" +LINUX_NIX="$ROOT/linux.nix" +DARWIN_NIX="$ROOT/darwin.nix" + +MIRU_LATEST_VER="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "https://api.github.com/repos/ThaUnknown/miru/releases/latest" | jq -r .tag_name | sed 's/^v//')" +MIRU_CURRENT_VER="$(grep -oP 'version = "\K[^"]+' "$PACKAGE_NIX")" + +if [[ "$MIRU_LATEST_VER" == "null" ]]; then + echo "error: could not fetch miru latest version from GitHub API" >&2 + exit 1 +fi + +if [[ "$MIRU_LATEST_VER" == "$MIRU_CURRENT_VER" ]]; then + echo "miru is up-to-date" + exit 0 +fi + +get_hash() { + # $1: URL + nix-hash --to-sri --type sha256 "$(nix-prefetch-url --type sha256 "$1")" +} + +replace_hash_in_file() { + # $1: file + # $2: new hash + sed -i "s#hash = \".*\"#hash = \"$2\"#g" "$1" +} + +replace_version_in_file() { + # $1: file + # $2: new version + sed -i "s#version = \".*\";#version = \"$2\";#g" "$1" +} + +MIRU_LINUX_HASH="$(get_hash "https://github.com/ThaUnknown/miru/releases/download/v${MIRU_LATEST_VER}/linux-Miru-${MIRU_LATEST_VER}.AppImage")" +MIRU_DARWIN_HASH="$(get_hash "https://github.com/ThaUnknown/miru/releases/download/v${MIRU_LATEST_VER}/mac-Miru-${MIRU_LATEST_VER}-mac.zip")" + +replace_hash_in_file "$LINUX_NIX" "$MIRU_LINUX_HASH" +replace_hash_in_file "$DARWIN_NIX" "$MIRU_DARWIN_HASH" + +replace_version_in_file "$PACKAGE_NIX" "$MIRU_LATEST_VER"