22 # one of `[ null "rocm" "cuda" ]`
32 src = fetchFromGitHub {
36 hash = "sha256-M2G53DJF/22ZVCAb4jGjyErKO6q2argehHSV7AEef6w=";
37 fetchSubmodules = true;
40 validAccel = lib.assertOneOf "ollama.acceleration" acceleration [ null "rocm" "cuda" ];
42 warnIfNotLinux = api: (lib.warnIfNot stdenv.isLinux
43 "building ollama with `${api}` is only supported on linux; falling back to cpu"
45 enableRocm = validAccel && (acceleration == "rocm") && (warnIfNotLinux "rocm");
46 enableCuda = validAccel && (acceleration == "cuda") && (warnIfNotLinux "cuda");
48 rocmClang = linkFarm "rocm-clang" {
49 llvm = rocmPackages.llvm.clang;
57 rocmPackages.rocsolver
58 rocmPackages.rocsparse
59 rocmPackages.rocm-device-libs
64 cudaToolkit = buildEnv {
65 name = "cuda-toolkit";
66 ignoreCollisions = true; # FIXME: find a cleaner way to do this without ignoring collisions
68 cudaPackages.cudatoolkit
69 cudaPackages.cuda_cudart
73 runtimeLibs = lib.optionals enableRocm [
75 ] ++ lib.optionals enableCuda [
76 linuxPackages.nvidia_x11
79 appleFrameworks = darwin.apple_sdk_11_0.frameworks;
81 appleFrameworks.Accelerate
83 appleFrameworks.MetalKit
84 appleFrameworks.MetalPerformanceShaders
90 buildGo122Module.override { stdenv = overrideCC stdenv gcc12; }
93 preparePatch = patch: hash: fetchpatch {
94 url = "file://${src}/llm/patches/${patch}";
97 extraPrefix = "llm/llama.cpp/";
99 inherit (lib) licenses platforms maintainers;
101 goBuild ((lib.optionalAttrs enableRocm {
102 ROCM_PATH = rocmPath;
103 CLBlast_DIR = "${clblast}/lib/cmake/CLBlast";
104 }) // (lib.optionalAttrs enableCuda {
105 CUDA_LIB_DIR = "${cudaToolkit}/lib";
106 CUDACXX = "${cudaToolkit}/bin/nvcc";
107 CUDAToolkit_ROOT = cudaToolkit;
109 inherit pname version src;
110 vendorHash = "sha256-Lj7CBvS51RqF63c01cOCgY7BCQeCKGu794qzb/S80C0=";
112 nativeBuildInputs = [
114 ] ++ lib.optionals (enableRocm || enableCuda) [
116 ] ++ lib.optionals stdenv.isDarwin
119 buildInputs = lib.optionals enableRocm [
123 rocmPackages.rocsolver
124 rocmPackages.rocsparse
126 ] ++ lib.optionals enableCuda [
127 cudaPackages.cuda_cudart
128 ] ++ lib.optionals stdenv.isDarwin
132 # remove uses of `git` in the `go generate` script
133 # instead use `patch` where necessary
135 # replace a hardcoded use of `g++` with `$CXX`
138 # ollama's patches of llama.cpp's example server
139 # `ollama/llm/generate/gen_common.sh` -> "apply temporary patches until fix is upstream"
140 (preparePatch "01-cache.diff" "sha256-VDwu/iK6taBCyscpndQiOJ3eGqonnLVwmS2rJNMBVGU=")
141 (preparePatch "02-cudaleaks.diff" "sha256-nxsWgrePUMsZBWWQAjqVHWMJPzr1owH1zSJvUU7Q5pA=")
142 (preparePatch "03-load_exception.diff" "sha256-1DfNahFYYxqlx4E4pwMKQpL+XR0bibYnDFGt6dCL4TM=")
143 (preparePatch "04-locale.diff" "sha256-r5nHiP6yN/rQObRu2FZIPBKpKP9yByyZ6sSI2SKj6Do=")
144 (preparePatch "05-fix-clip-free.diff" "sha256-EFZ+QTtZCvstVxYgVdFKHsQqdkL98T0eXOEBOqCrlL4=")
147 # use a patch from the nix store in the `go generate` script
148 substituteInPlace llm/generate/gen_common.sh \
149 --subst-var-by cmakeIncludePatch '${./cmake-include.patch}'
150 # `ollama/llm/generate/gen_common.sh` -> "avoid duplicate main symbols when we link into the cgo binary"
151 substituteInPlace llm/llama.cpp/examples/server/server.cpp \
152 --replace-fail 'int main(' 'int __main('
153 # replace inaccurate version number with actual release version
154 substituteInPlace version/version.go --replace-fail 0.0.0 '${version}'
157 export OLLAMA_SKIP_PATCHING=true
158 # build llama.cpp libraries for ollama
162 # the app doesn't appear functional at the moment, so hide it
163 mv "$out/bin/app" "$out/bin/.ollama-app"
164 '' + lib.optionalString (enableRocm || enableCuda) ''
165 # expose runtime libraries necessary to use the gpu
166 mv "$out/bin/ollama" "$out/bin/.ollama-unwrapped"
167 makeWrapper "$out/bin/.ollama-unwrapped" "$out/bin/ollama" \
168 --suffix LD_LIBRARY_PATH : '/run/opengl-driver/lib:${lib.makeLibraryPath runtimeLibs}' '' + lib.optionalString enableRocm ''\
169 --set-default HIP_PATH ${pkgs.rocmPackages.meta.rocm-hip-libraries}
175 "-X=github.com/jmorganca/ollama/version.Version=${version}"
176 "-X=github.com/jmorganca/ollama/server.mode=release"
180 service = nixosTests.ollama;
181 rocm = pkgs.ollama.override { acceleration = "rocm"; };
182 cuda = pkgs.ollama.override { acceleration = "cuda"; };
183 version = testers.testVersion {
190 changelog = "https://github.com/ollama/ollama/releases/tag/v${version}";
191 description = "Get up and running with large language models locally";
192 homepage = "https://github.com/jmorganca/ollama";
193 license = licenses.mit;
194 platforms = platforms.unix;
195 mainProgram = "ollama";
196 maintainers = with maintainers; [ abysssol dit7ya elohmeier ];