about summary refs log tree commit diff
path: root/nixpkgs/pkgs/by-name/ta/tabby/package.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2024-03-22 16:41:59 +0100
committerAlyssa Ross <hi@alyssa.is>2024-03-22 16:41:59 +0100
commit46a88117a05c3469af5d99433af140c3de8ca088 (patch)
treed7f0557756d8f07a3081b3498c05ddc5a8ad429d /nixpkgs/pkgs/by-name/ta/tabby/package.nix
parente97457545cea0b2ca421da257c83d8f1ef451d85 (diff)
parenta343533bccc62400e8a9560423486a3b6c11a23b (diff)
downloadnixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar.gz
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar.bz2
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar.lz
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar.xz
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.tar.zst
nixlib-46a88117a05c3469af5d99433af140c3de8ca088.zip
Merge commit 'a343533bccc62400e8a9560423486a3b6c11a23b'
Diffstat (limited to 'nixpkgs/pkgs/by-name/ta/tabby/package.nix')
-rw-r--r--nixpkgs/pkgs/by-name/ta/tabby/package.nix146
1 files changed, 122 insertions, 24 deletions
diff --git a/nixpkgs/pkgs/by-name/ta/tabby/package.nix b/nixpkgs/pkgs/by-name/ta/tabby/package.nix
index b89434a8fc48..fa52d372ab9a 100644
--- a/nixpkgs/pkgs/by-name/ta/tabby/package.nix
+++ b/nixpkgs/pkgs/by-name/ta/tabby/package.nix
@@ -1,36 +1,132 @@
-{ lib
+{ config
+, lib
+, rustPlatform
 , fetchFromGitHub
-, gcc12
-, cmake
+, stdenv
+
 , git
 , openssl
 , pkg-config
 , protobuf
-, rustPlatform
-, addOpenGLRunpath
-, cudatoolkit
-, nvidia ? true
+
+, llama-cpp
+
+, cudaSupport ? config.cudaSupport
+, cudaPackages ? { }
+
+, rocmSupport ? config.rocmSupport
+
+, darwin
+, metalSupport ? stdenv.isDarwin && stdenv.isAarch64
+
+  # one of [ null "cpu" "rocm" "cuda" "metal" ];
+, acceleration ? null
 }:
 
-rustPlatform.buildRustPackage rec {
-  version = "0.7.0";
+let
+  inherit (lib) optional optionals flatten;
+  # References:
+  # https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ll/llama-cpp/package.nix
+  # https://github.com/NixOS/nixpkgs/blob/master/pkgs/tools/misc/ollama/default.nix
+
   pname = "tabby";
+  version = "0.8.3";
+
+
+  availableAccelerations = flatten [
+    (optional cudaSupport "cuda")
+    (optional rocmSupport "rocm")
+    (optional metalSupport "metal")
+  ];
+
+  warnIfMultipleAccelerationMethods = configured: (let
+    len = builtins.length configured;
+    result = if len == 0 then "cpu" else (builtins.head configured);
+  in
+    lib.warnIf (len > 1) ''
+      building tabby with multiple acceleration methods enabled is not
+      supported; falling back to `${result}`
+    ''
+    result
+  );
+
+  # If user did not not override the acceleration attribute, then try to use one of
+  # - nixpkgs.config.cudaSupport
+  # - nixpkgs.config.rocmSupport
+  # - metal if (stdenv.isDarwin && stdenv.isAarch64)
+  # !! warn if multiple acceleration methods are enabled and default to the first one in the list
+  featureDevice = if (builtins.isNull acceleration) then (warnIfMultipleAccelerationMethods availableAccelerations) else acceleration;
+
+  warnIfNotLinux = api: (lib.warnIfNot stdenv.isLinux
+    "building tabby with `${api}` is only supported on linux; falling back to cpu"
+    stdenv.isLinux);
+  warnIfNotDarwinAarch64 = api: (lib.warnIfNot (stdenv.isDarwin && stdenv.isAarch64)
+    "building tabby with `${api}` is only supported on Darwin-aarch64; falling back to cpu"
+    (stdenv.isDarwin && stdenv.isAarch64));
+
+  validAccel = lib.assertOneOf "tabby.featureDevice" featureDevice [ "cpu" "rocm" "cuda" "metal" ];
+
+  # TODO(ghthor): there is a bug here where featureDevice could be cuda, but enableCuda is false
+  #  The would result in a startup failure of the service module.
+  enableRocm = validAccel && (featureDevice == "rocm") && (warnIfNotLinux "rocm");
+  enableCuda = validAccel && (featureDevice == "cuda") && (warnIfNotLinux "cuda");
+  enableMetal = validAccel && (featureDevice == "metal") && (warnIfNotDarwinAarch64 "metal");
+
+  # We have to use override here because tabby doesn't actually tell llama-cpp
+  # to use a specific device type as it is relying on llama-cpp only being
+  # built to use one type of device.
+  #
+  # See: https://github.com/TabbyML/tabby/blob/v0.8.3/crates/llama-cpp-bindings/include/engine.h#L20
+  #
+  llamaccpPackage = llama-cpp.override {
+    rocmSupport = enableRocm;
+    cudaSupport = enableCuda;
+    metalSupport = enableMetal;
+  };
+
+  # TODO(ghthor): some of this can be removed
+  darwinBuildInputs = [ llamaccpPackage ]
+  ++ optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
+    Foundation
+    Accelerate
+    CoreVideo
+    CoreGraphics
+  ]
+  ++ optionals enableMetal [ Metal MetalKit ]);
+
+  cudaBuildInputs = [ llamaccpPackage ];
+  rocmBuildInputs = [ llamaccpPackage ];
+
+  LLAMA_CPP_LIB = "${llamaccpPackage.outPath}/lib";
+
+in
+rustPlatform.buildRustPackage {
+  inherit pname version;
+  inherit featureDevice;
 
   src = fetchFromGitHub {
     owner = "TabbyML";
     repo = "tabby";
     rev = "v${version}";
-    hash = "sha256-BTPJWvqO4IuQAiUEER9PYfu4aQsz5RI77WsA/gQu5Jc=";
+    hash = "sha256-+5Q5XKfh7+g24y2hBqJC/jNEoRytDdcRdn838xc7c8w=";
     fetchSubmodules = true;
   };
 
-  cargoHash = "sha256-Du0ya9J+0tz72mSid5If0VFX2lLC7YtwNQ/MALpFv2M=";
+  cargoLock = {
+    lockFile = ./Cargo.lock;
+    outputHashes = {
+      "tree-sitter-c-0.20.6" = "sha256-Etl4s29YSOxiqPo4Z49N6zIYqNpIsdk/Qd0jR8jdvW4=";
+      "tree-sitter-cpp-0.20.3" = "sha256-UrQ48CoUMSHmlHzOMu22c9N4hxJtHL2ZYRabYjf5byA=";
+    };
+  };
 
   # https://github.com/TabbyML/tabby/blob/v0.7.0/.github/workflows/release.yml#L39
   cargoBuildFlags = [
     "--release"
     "--package" "tabby"
-  ] ++ lib.optional nvidia [
+  ] ++ optionals enableRocm [
+    "--features" "rocm"
+  ] ++ optionals enableCuda [
     "--features" "cuda"
   ];
 
@@ -40,23 +136,24 @@ rustPlatform.buildRustPackage rec {
     pkg-config
     protobuf
     git
-    cmake
-    gcc12
-
-  ] ++ lib.optional nvidia [
-    addOpenGLRunpath
+  ] ++ optionals enableCuda [
+    # TODO: Replace with autoAddDriverRunpath
+    # once https://github.com/NixOS/nixpkgs/pull/275241 has been merged
+    cudaPackages.autoAddOpenGLRunpathHook
   ];
 
   buildInputs = [ openssl ]
-    ++ lib.optional nvidia cudatoolkit
+  ++ optionals stdenv.isDarwin darwinBuildInputs
+  ++ optionals enableCuda cudaBuildInputs
+  ++ optionals enableRocm rocmBuildInputs
   ;
 
-  postInstall = ''
-    ${if nvidia then ''
-    addOpenGLRunpath "$out/bin/tabby"
-    '' else ''
-    ''}
-  '';
+  env = lib.mergeAttrsList [
+    { inherit LLAMA_CPP_LIB; }
+    # Work around https://github.com/NixOS/nixpkgs/issues/166205
+    (lib.optionalAttrs stdenv.cc.isClang { NIX_LDFLAGS = "-l${stdenv.cc.libcxx.cxxabi.libName}"; })
+  ];
+  patches = [ ./0001-nix-build-use-nix-native-llama-cpp-package.patch ];
 
   # Fails with:
   # file cannot create directory: /var/empty/local/lib64/cmake/Llama
@@ -69,5 +166,6 @@ rustPlatform.buildRustPackage rec {
     mainProgram = "tabby";
     license = licenses.asl20;
     maintainers = [ maintainers.ghthor ];
+    broken = stdenv.isDarwin && !stdenv.isAarch64;
   };
 }