30 # one of `[ null false "rocm" "cuda" ]`
34 assert builtins.elem acceleration [
43 # don't forget to invalidate all hashes each update
46 src = fetchFromGitHub {
50 hash = "sha256-X1xGo054+MpThvZzeg/F+IBczY1wn/gTjcq+j1JztDg=";
51 fetchSubmodules = true;
54 vendorHash = "sha256-xz9v91Im6xTLPzmYoVecdF7XiPKBZk3qou1SGokgPXQ=";
56 validateFallback = lib.warnIf (config.rocmSupport && config.cudaSupport) (lib.concatStrings [
57 "both `nixpkgs.config.rocmSupport` and `nixpkgs.config.cudaSupport` are enabled, "
58 "but they are mutually exclusive; falling back to cpu"
59 ]) (!(config.rocmSupport && config.cudaSupport));
61 mode: fallback: (acceleration == mode) || (fallback && acceleration == null && validateFallback);
63 rocmRequested = shouldEnable "rocm" config.rocmSupport;
64 cudaRequested = shouldEnable "cuda" config.cudaSupport;
66 enableRocm = rocmRequested && stdenv.hostPlatform.isLinux;
67 enableCuda = cudaRequested && stdenv.hostPlatform.isLinux;
73 rocmPackages.rocsolver
74 rocmPackages.rocsparse
75 rocmPackages.rocm-device-libs
78 rocmClang = linkFarm "rocm-clang" { llvm = rocmPackages.llvm.clang; };
81 paths = rocmLibs ++ [ rocmClang ];
85 cudaPackages.cuda_cudart
86 cudaPackages.libcublas
87 cudaPackages.cuda_cccl
90 # Extract the major version of CUDA. e.g. 11 12
91 cudaMajorVersion = lib.versions.major cudaPackages.cuda_cudart.version;
93 cudaToolkit = buildEnv {
94 # ollama hardcodes the major version in the Makefile to support different variants.
95 # - https://github.com/ollama/ollama/blob/v0.4.4/llama/Makefile#L17-L18
96 name = "cuda-merged-${cudaMajorVersion}";
97 paths = map lib.getLib cudaLibs ++ [
98 (lib.getOutput "static" cudaPackages.cuda_cudart)
99 (lib.getBin (cudaPackages.cuda_nvcc.__spliced.buildHost or cudaPackages.cuda_nvcc))
103 cudaPath = lib.removeSuffix "-${cudaMajorVersion}" cudaToolkit;
105 metalFrameworks = with darwin.apple_sdk_11_0.frameworks; [
109 MetalPerformanceShaders
114 # ollama embeds llama-cpp binaries which actually run the ai models
115 # these llama-cpp binaries are unaffected by the ollama binary's DT_RUNPATH
116 # LD_LIBRARY_PATH is temporarily required to use the gpu
117 # until these llama-cpp binaries can have their runpath patched
118 "--suffix LD_LIBRARY_PATH : '${addDriverRunpath.driverLink}/lib'"
120 ++ lib.optionals enableRocm [
121 "--suffix LD_LIBRARY_PATH : '${rocmPath}/lib'"
122 "--set-default HIP_PATH '${rocmPath}'"
124 ++ lib.optionals enableCuda [
125 "--suffix LD_LIBRARY_PATH : '${lib.makeLibraryPath (map lib.getLib cudaLibs)}'"
127 wrapperArgs = builtins.concatStringsSep " " wrapperOptions;
130 if enableCuda then buildGoModule.override { stdenv = overrideCC stdenv gcc12; } else buildGoModule;
131 inherit (lib) licenses platforms maintainers;
142 lib.optionalAttrs enableRocm {
143 ROCM_PATH = rocmPath;
144 CLBlast_DIR = "${clblast}/lib/cmake/CLBlast";
147 // lib.optionalAttrs enableCuda {
148 CUDA_PATH = cudaPath;
156 ++ lib.optionals enableRocm [
157 rocmPackages.llvm.bintools
160 ++ lib.optionals enableCuda [ cudaPackages.cuda_nvcc ]
161 ++ lib.optionals (enableRocm || enableCuda) [
165 ++ lib.optionals stdenv.hostPlatform.isDarwin metalFrameworks;
168 lib.optionals enableRocm (rocmLibs ++ [ libdrm ])
169 ++ lib.optionals enableCuda cudaLibs
170 ++ lib.optionals stdenv.hostPlatform.isDarwin metalFrameworks;
173 # ollama's build script is unable to find hipcc
178 # replace inaccurate version number with actual release version
179 substituteInPlace version/version.go --replace-fail 0.0.0 '${version}'
183 finalAttrs: prevAttrs: {
184 # don't run llama.cpp build in the module fetch phase
190 # build llama.cpp libraries for ollama
191 make -j $NIX_BUILD_CORES
194 postInstall = lib.optionalString stdenv.hostPlatform.isLinux ''
195 # copy libggml_*.so and runners into lib
196 # https://github.com/ollama/ollama/blob/v0.4.4/llama/make/gpu.make#L90
198 cp -r dist/*/lib/* $out/lib/
203 # the app doesn't appear functional at the moment, so hide it
204 mv "$out/bin/app" "$out/bin/.ollama-app"
206 + lib.optionalString (enableRocm || enableCuda) ''
207 # expose runtime libraries necessary to use the gpu
208 wrapProgram "$out/bin/ollama" ${wrapperArgs}
214 "-X=github.com/ollama/ollama/version.Version=${version}"
215 "-X=github.com/ollama/ollama/server.mode=release"
222 version = testers.testVersion {
227 // lib.optionalAttrs stdenv.hostPlatform.isLinux {
228 inherit ollama-rocm ollama-cuda;
229 service = nixosTests.ollama;
230 service-cuda = nixosTests.ollama-cuda;
231 service-rocm = nixosTests.ollama-rocm;
233 } // lib.optionalAttrs (!enableRocm && !enableCuda) { updateScript = nix-update-script { }; };
237 "Get up and running with large language models locally"
238 + lib.optionalString rocmRequested ", using ROCm for AMD GPU acceleration"
239 + lib.optionalString cudaRequested ", using CUDA for NVIDIA GPU acceleration";
240 homepage = "https://github.com/ollama/ollama";
241 changelog = "https://github.com/ollama/ollama/releases/tag/v${version}";
242 license = licenses.mit;
243 platforms = if (rocmRequested || cudaRequested) then platforms.linux else platforms.unix;
244 mainProgram = "ollama";
245 maintainers = with maintainers; [