Commit ae0783dd authored by ljxfstorm's avatar ljxfstorm
Browse files

gemini-cli-bin: 0.34.0 -> 0.36.0

Upstream switched from the standalone JS release to a bundle zip, splitting the `gemini.js` into multiple chunks.
Migrate to `fetchzip` and move Nix-specific modifications into a loop over chunks in `patchPhase`.
parent ab25d4ab
Loading
Loading
Loading
Loading
+34 −18
Original line number Diff line number Diff line
{
  lib,
  stdenvNoCC,
  fetchurl,
  fetchzip,
  nodejs,
  sysctl,
  writableTmpDirAsHomeHook,
@@ -11,15 +11,14 @@
}:
stdenvNoCC.mkDerivation (finalAttrs: {
  pname = "gemini-cli-bin";
  version = "0.34.0";
  version = "0.36.0";

  src = fetchurl {
    url = "https://github.com/google-gemini/gemini-cli/releases/download/v${finalAttrs.version}/gemini.js";
    hash = "sha256-Qfol6zATjVK6d4gfA6ql3aVwjqRE7NAYqg5YTyEVDHk=";
  src = fetchzip {
    url = "https://github.com/google-gemini/gemini-cli/releases/download/v${finalAttrs.version}/gemini-cli-bundle.zip";
    hash = "sha256-wu+QZ5roBNY1mwtte+7opKFBRdOCXONW95UEJ7M3gJI=";
    stripRoot = false;
  };

  dontUnpack = true;

  strictDeps = true;

  nativeBuildInputs = [
@@ -31,24 +30,41 @@ stdenvNoCC.mkDerivation (finalAttrs: {
    ripgrep
  ];

  installPhase = ''
    runHook preInstall
  patchPhase = ''
    runHook prePatch

    local dest="$out/lib/gemini/gemini.js"
    install -Dm644 "$src" "$dest"
    # chunk filenames contain unpredictable hashes, use glob to iterate
    for chunk in ./chunk-*.js; do

      # disable auto-update
    sed -i '/enableAutoUpdate: {/,/}/ s/default: true/default: false/' "$dest"
      if grep -q 'enableAutoUpdate: {' "$chunk"; then
        sed -i '/enableAutoUpdate: {/,/}/ s/default: true/default: false/' "$chunk"
      fi

      # use `ripgrep` from `nixpkgs`, more dependencies but prevent downloading incompatible binary on NixOS
      # this workaround can be removed once the following upstream issue is resolved:
      # https://github.com/google-gemini/gemini-cli/issues/11438
    substituteInPlace "$dest" \
      if grep -q 'await resolveExistingRgPath();' "$chunk"; then
        substituteInPlace "$chunk" \
          --replace-fail 'const existingPath = await resolveExistingRgPath();' 'const existingPath = "${lib.getExe ripgrep}";'
      fi

    done

    runHook postPatch
  '';

  installPhase = ''
    runHook preInstall

    local bundleDir="$out/lib/gemini"

    mkdir -p "$bundleDir"
    cp -aT . "$bundleDir"

    makeWrapper "${lib.getExe nodejs}" "$out/bin/gemini" \
      --add-flags "--no-warnings=DEP0040" \
      --add-flags "$dest"
      --add-flags "$bundleDir/gemini.js"

    runHook postInstall
  '';