From f606c72fac5d55f5ad87637cea4d0bfc2aee0cd8 Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Tue, 18 Mar 2025 19:28:42 +0000 Subject: [PATCH] homepage-dashboard: add update script Since we now need to patch dependencies through pnpm, and that results in a changed lock file that carries hashes for updated dependencies, it seems sensible to script the generation of the patch such that bots can do it, and the process is somewhat documented. --- pkgs/servers/homepage-dashboard/default.nix | 4 +- pkgs/servers/homepage-dashboard/update.sh | 66 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100755 pkgs/servers/homepage-dashboard/update.sh diff --git a/pkgs/servers/homepage-dashboard/default.nix b/pkgs/servers/homepage-dashboard/default.nix index fd121b7a5b53..76a08bfec7e6 100644 --- a/pkgs/servers/homepage-dashboard/default.nix +++ b/pkgs/servers/homepage-dashboard/default.nix @@ -11,7 +11,6 @@ lib, nixosTests, enableLocalIcons ? false, - nix-update-script, }: let dashboardIcons = fetchFromGitHub { @@ -42,6 +41,7 @@ stdenv.mkDerivation (finalAttrs: { # This patch ensures that the cache implementation respects the env # variable `NIXPKGS_HOMEPAGE_CACHE_DIR`, which is set by default in the # wrapper below. + # The patch is automatically generated by the `update.sh` script. patches = [ ./prerender_cache_path.patch ]; pnpmDeps = pnpm_10.fetchDeps { @@ -101,7 +101,7 @@ stdenv.mkDerivation (finalAttrs: { tests = { inherit (nixosTests) homepage-dashboard; }; - updateScript = nix-update-script { }; + updateScript = ./update.sh; }; meta = { diff --git a/pkgs/servers/homepage-dashboard/update.sh b/pkgs/servers/homepage-dashboard/update.sh new file mode 100755 index 000000000000..4b5069626263 --- /dev/null +++ b/pkgs/servers/homepage-dashboard/update.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env nix-shell +#!nix-shell -I nixpkgs=./. -i bash -p curl jq git pnpm_10 sd +# shellcheck shell=bash +set -euo pipefail +nixpkgs="$(pwd)" +cd $(readlink -e $(dirname "${BASH_SOURCE[0]}")) + +# Generate the patch file that makes homepage-dashboard aware of the NIXPKGS_HOMEPAGE_CACHE_DIR environment variable. +# Generating the patch this way ensures that both the patch is included, and the lock file is updated. +generate_patch() { + local version; version="$1" + echo "Generating homepage-dashboard patch" + + git clone -b "v$version" https://github.com/gethomepage/homepage.git src + pushd src + + pnpm install + pnpm patch next + sd \ + 'this.serverDistDir = ctx.serverDistDir;' \ + 'this.serverDistDir = require("path").join((process.env.NIXPKGS_HOMEPAGE_CACHE_DIR || "/var/cache/homepage-dashboard"), "homepage");' \ + node_modules/.pnpm_patches/next*/dist/server/lib/incremental-cache/file-system-cache.js + pnpm patch-commit node_modules/.pnpm_patches/next* + + git add -A . + git diff -p --staged > ../prerender_cache_path.patch + + popd + rm -rf src +} + +# Update the hash of the homepage-dashboard source code in the Nix expression. +update_homepage_dashboard_source() { + local version; version="$1" + echo "Updating homepage-dashboard source" + + old_hash="$(nix eval --json --impure --expr "(import $nixpkgs/default.nix {}).homepage-dashboard.src.outputHash" | jq -r)" + old_version="$(nix eval --json --impure --expr "(import $nixpkgs/default.nix {}).homepage-dashboard.version" | jq -r)" + new_hash="$(nix-build --impure --expr "let src = (import $nixpkgs/default.nix {}).homepage-dashboard.src; in (src.overrideAttrs or (f: src // f src)) (_: { version = \"$version\"; outputHash = \"\"; outputHashAlgo = \"sha256\"; })" 2>&1 | tr -s ' ' | grep -Po "got: \K.+$")" || true + + sed -i "s|${old_hash}|${new_hash}|g" default.nix + sed -i "s|${old_version}|${version}|g" default.nix +} + +# Update the hash of the homepage-dashboard pnpm dependencies in the Nix expression. +update_pnpm_deps_hash() { + echo "Updating homepage-dashboard pnpm deps hash" + + old_hash="$(nix eval --json --impure --expr "(import $nixpkgs/default.nix {}).homepage-dashboard.pnpmDeps.outputHash" | jq -r)" + new_hash="$(nix-build --impure --expr "let src = (import $nixpkgs/default.nix {}).homepage-dashboard.pnpmDeps; in (src.overrideAttrs or (f: src // f src)) (_: { outputHash = \"\"; outputHashAlgo = \"sha256\"; })" 2>&1 | tr -s ' ' | grep -Po "got: \K.+$")" || true + + sed -i "s|${old_hash}|${new_hash}|g" default.nix +} + +LATEST_TAG="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} https://api.github.com/repos/gethomepage/homepage/releases/latest | jq -r '.tag_name')" +LATEST_VERSION="$(expr "$LATEST_TAG" : 'v\(.*\)')" +CURRENT_VERSION="$(grep -Po "version = \"\K[^\"]+" default.nix)" + +if [[ "$CURRENT_VERSION" == "$LATEST_VERSION" ]]; then + echo "homepage-dashboard is up to date: ${CURRENT_VERSION}" + exit 0 +fi + +update_homepage_dashboard_source "$LATEST_VERSION" +generate_patch "$LATEST_VERSION" +update_pnpm_deps_hash