From 39154b4333a2beb556478675a434d6ce1f2fe8b6 Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Wed, 20 Dec 2023 12:44:46 +0100 Subject: [PATCH] tabnine: import nixpkgs update script --- flake.nix | 2 +- packages/tabnine.nix | 20 ------------- packages/tabnine/default.nix | 55 +++++++++++++++++++++++++++++++++++ packages/tabnine/sources.json | 17 +++++++++++ packages/tabnine/update.sh | 44 ++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 21 deletions(-) delete mode 100644 packages/tabnine.nix create mode 100644 packages/tabnine/default.nix create mode 100644 packages/tabnine/sources.json create mode 100755 packages/tabnine/update.sh diff --git a/flake.nix b/flake.nix index 723de98..9c04922 100644 --- a/flake.nix +++ b/flake.nix @@ -32,7 +32,7 @@ agilebits-op = import ./packages/agilebits-op.nix { inherit pkgs lib; }; cliclick = import ./packages/cliclick.nix { inherit pkgs lib; }; phantomjs = import ./packages/phantomjs.nix { inherit pkgs lib; }; - tabnine = import ./packages/tabnine.nix { inherit pkgs lib; }; + tabnine = import ./packages/tabnine { inherit pkgs lib; }; } else { }; in { diff --git a/packages/tabnine.nix b/packages/tabnine.nix deleted file mode 100644 index fbb9195..0000000 --- a/packages/tabnine.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ pkgs, lib, ... }: - -pkgs.tabnine.overrideAttrs ( - oa: rec { - # https://update.tabnine.com/bundles/version - version = "4.53.0"; - - src = pkgs.fetchurl { - url = "https://update.tabnine.com/bundles/${version}/aarch64-apple-darwin/TabNine.zip"; - sha256 = "01c1rxf6k5wv4758g9m8n6mngp07kr8xbskb5liz1x8cljcs6f72"; - }; - - meta = with lib; { - homepage = "https://tabnine.com"; - description = "Smart Compose for code that uses deep learning to help you write code faster"; - license = licenses.unfree; - platforms = [ "aarch64-darwin" ]; - }; - } -) diff --git a/packages/tabnine/default.nix b/packages/tabnine/default.nix new file mode 100644 index 0000000..4326994 --- /dev/null +++ b/packages/tabnine/default.nix @@ -0,0 +1,55 @@ +{ pkgs, lib, ... }: + +# Based on: https://github.com/NixOS/nixpkgs/blob/91a00709aebb3602f172a0bf47ba1ef013e34835/pkgs/development/tools/tabnine/default.nix + +let + stdenv = pkgs.stdenv; + + sources = lib.importJSON ./sources.json; + + platform = + if (builtins.hasAttr stdenv.hostPlatform.system sources.platforms) then + builtins.getAttr (stdenv.hostPlatform.system) sources.platforms + else + throw "Not supported on ${stdenv.hostPlatform.system}"; +in +stdenv.mkDerivation { + pname = "tabnine"; + inherit (sources) version; + + src = pkgs.fetchurl { + url = "https://update.tabnine.com/bundles/${sources.version}/${platform.name}/TabNine.zip"; + inherit (platform) hash; + }; + + dontBuild = true; + + # Work around the "unpacker appears to have produced no directories" + # case that happens when the archive doesn't have a subdirectory. + sourceRoot = "."; + + nativeBuildInputs = [ pkgs.unzip ]; + + installPhase = '' + runHook preInstall + + install -Dm755 TabNine $out/bin/TabNine + install -Dm755 TabNine-deep-cloud $out/bin/TabNine-deep-cloud + install -Dm755 TabNine-deep-local $out/bin/TabNine-deep-local + install -Dm755 WD-TabNine $out/bin/WD-TabNine + + runHook postInstall + ''; + + passthru = { + platform = platform.name; + updateScript = ./update.sh; + }; + + meta = with lib; { + homepage = "https://tabnine.com"; + description = "Smart Compose for code that uses deep learning to help you write code faster"; + license = licenses.unfree; + platforms = builtins.attrNames sources.platforms; + }; +} diff --git a/packages/tabnine/sources.json b/packages/tabnine/sources.json new file mode 100644 index 0000000..190c5a9 --- /dev/null +++ b/packages/tabnine/sources.json @@ -0,0 +1,17 @@ +{ + "version": "4.64.0", + "platforms": { + "x86_64-linux": { + "name": "x86_64-unknown-linux-musl", + "hash": "sha256-AruVHP8UweT2zyOHphanvLFwOAsHiK/u98jL9axeQTI=" + }, + "aarch64-darwin": { + "name": "aarch64-apple-darwin", + "hash": "sha256-p06j62PaypAyeQEoFwntKQ6qSRT8Gi4NZ/Tqw+V4Ms4=" + }, + "x86_64-darwin": { + "name": "x86_64-apple-darwin", + "hash": "sha256-7wFdY0AzaHNLxlrQxfl55BWVcvGeqCa0dTM2zMgCK8Y=" + } + } +} diff --git a/packages/tabnine/update.sh b/packages/tabnine/update.sh new file mode 100755 index 0000000..285ec8c --- /dev/null +++ b/packages/tabnine/update.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p curlMinimal jq +#shellcheck shell=bash + +# Based on: https://github.com/NixOS/nixpkgs/blob/91a00709aebb3602f172a0bf47ba1ef013e34835/pkgs/development/tools/tabnine/update.sh + +set -euo pipefail + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) + +function prefetch-sri() { + nix-prefetch-url "$1" 2>/dev/null | + xargs nix --experimental-features nix-command hash to-sri --type sha256 +} + +declare -A platforms=( + [x86_64-unknown-linux-musl]="x86_64-linux" + [x86_64-apple-darwin]="x86_64-darwin" + [aarch64-apple-darwin]="aarch64-darwin" +) + +old_version="$(jq -r '.version' "$SCRIPT_DIR/sources.json")" +new_version="$(curl -sS https://update.tabnine.com/bundles/version)" + +echo "Updating $old_version -> $new_version" + +sources_tmp="$(mktemp)" +trap 'rm -f "$sources_tmp"' EXIT + +cat <"$sources_tmp" +{ + "version": "$new_version", + "platforms": {} +} +EOF + +for platform in "${!platforms[@]}"; do + url="https://update.tabnine.com/bundles/${new_version}/${platform}/TabNine.zip" + hash="$(prefetch-sri "$url")" + nix_platform="${platforms[$platform]}" + cat <<<"$(jq --arg nix_platform "$nix_platform" --arg platform "$platform" --arg hash "$hash" '.platforms += {($nix_platform): {name: $platform, hash: $hash}}' "$sources_tmp")" >"$sources_tmp" +done + +cp "$sources_tmp" "$SCRIPT_DIR/sources.json"