29 # one of `[ null false "rocm" "cuda" ]`
33 assert builtins.elem acceleration [
42 # don't forget to invalidate all hashes each update
45 src = fetchFromGitHub {
49 hash = "sha256-K1FYXEP0bTZa8M+V4/SxI+Q+LWs2rsAMZ/ETJCaO7P8=";
50 fetchSubmodules = true;
53 vendorHash = "sha256-hSxcREAujhvzHVNwnRTfhi0MKI3s8HNavER2VLz6SYk=";
55 validateFallback = lib.warnIf (config.rocmSupport && config.cudaSupport) (lib.concatStrings [
56 "both `nixpkgs.config.rocmSupport` and `nixpkgs.config.cudaSupport` are enabled, "
57 "but they are mutually exclusive; falling back to cpu"
58 ]) (!(config.rocmSupport && config.cudaSupport));
60 mode: fallback: (acceleration == mode) || (fallback && acceleration == null && validateFallback);
62 rocmRequested = shouldEnable "rocm" config.rocmSupport;
63 cudaRequested = shouldEnable "cuda" config.cudaSupport;
65 enableRocm = rocmRequested && stdenv.hostPlatform.isLinux;
66 enableCuda = cudaRequested && stdenv.hostPlatform.isLinux;
72 rocmPackages.rocsolver
73 rocmPackages.rocsparse
74 rocmPackages.rocm-device-libs
77 rocmClang = linkFarm "rocm-clang" { llvm = rocmPackages.llvm.clang; };
80 paths = rocmLibs ++ [ rocmClang ];
84 cudaPackages.cuda_cudart
85 cudaPackages.libcublas
86 cudaPackages.cuda_cccl
88 cudaToolkit = buildEnv {
90 paths = map lib.getLib cudaLibs ++ [
91 (lib.getOutput "static" cudaPackages.cuda_cudart)
92 (lib.getBin (cudaPackages.cuda_nvcc.__spliced.buildHost or cudaPackages.cuda_nvcc))
96 metalFrameworks = with darwin.apple_sdk_11_0.frameworks; [
100 MetalPerformanceShaders
105 # ollama embeds llama-cpp binaries which actually run the ai models
106 # these llama-cpp binaries are unaffected by the ollama binary's DT_RUNPATH
107 # LD_LIBRARY_PATH is temporarily required to use the gpu
108 # until these llama-cpp binaries can have their runpath patched
109 "--suffix LD_LIBRARY_PATH : '${addDriverRunpath.driverLink}/lib'"
111 ++ lib.optionals enableRocm [
112 "--suffix LD_LIBRARY_PATH : '${rocmPath}/lib'"
113 "--set-default HIP_PATH '${rocmPath}'"
115 ++ lib.optionals enableCuda [
116 "--suffix LD_LIBRARY_PATH : '${lib.makeLibraryPath (map lib.getLib cudaLibs)}'"
118 wrapperArgs = builtins.concatStringsSep " " wrapperOptions;
121 if enableCuda then buildGoModule.override { stdenv = overrideCC stdenv gcc12; } else buildGoModule;
122 inherit (lib) licenses platforms maintainers;
133 lib.optionalAttrs enableRocm {
134 ROCM_PATH = rocmPath;
135 CLBlast_DIR = "${clblast}/lib/cmake/CLBlast";
137 // lib.optionalAttrs enableCuda { CUDA_LIB_DIR = "${cudaToolkit}/lib"; };
141 ++ lib.optionals enableRocm [ rocmPackages.llvm.bintools ]
142 ++ lib.optionals enableCuda [ cudaPackages.cuda_nvcc ]
143 ++ lib.optionals (enableRocm || enableCuda) [
147 ++ lib.optionals stdenv.hostPlatform.isDarwin metalFrameworks;
150 lib.optionals enableRocm (rocmLibs ++ [ libdrm ])
151 ++ lib.optionals enableCuda cudaLibs
152 ++ lib.optionals stdenv.hostPlatform.isDarwin metalFrameworks;
155 # disable uses of `git` in the `go generate` script
156 # ollama's build script assumes the source is a git repo, but nix removes the git directory
157 # this also disables necessary patches contained in `ollama/llm/patches/`
158 # those patches are applied in `postPatch`
161 # we provide our own deps at runtime
166 # replace inaccurate version number with actual release version
167 substituteInPlace version/version.go --replace-fail 0.0.0 '${version}'
169 # apply ollama's patches to `llama.cpp` submodule
170 for diff in llm/patches/*; do
171 patch -p1 -d llm/llama.cpp < $diff
176 finalAttrs: prevAttrs: {
177 # don't run llama.cpp build in the module fetch phase
183 # disable uses of `git`, since nix removes the git directory
184 export OLLAMA_SKIP_PATCHING=true
185 # build llama.cpp libraries for ollama
191 # the app doesn't appear functional at the moment, so hide it
192 mv "$out/bin/app" "$out/bin/.ollama-app"
194 + lib.optionalString (enableRocm || enableCuda) ''
195 # expose runtime libraries necessary to use the gpu
196 wrapProgram "$out/bin/ollama" ${wrapperArgs}
202 "-X=github.com/ollama/ollama/version.Version=${version}"
203 "-X=github.com/ollama/ollama/server.mode=release"
210 version = testers.testVersion {
215 // lib.optionalAttrs stdenv.hostPlatform.isLinux {
216 inherit ollama-rocm ollama-cuda;
217 service = nixosTests.ollama;
218 service-cuda = nixosTests.ollama-cuda;
219 service-rocm = nixosTests.ollama-rocm;
221 } // lib.optionalAttrs (!enableRocm && !enableCuda) { updateScript = nix-update-script { }; };
225 "Get up and running with large language models locally"
226 + lib.optionalString rocmRequested ", using ROCm for AMD GPU acceleration"
227 + lib.optionalString cudaRequested ", using CUDA for NVIDIA GPU acceleration";
228 homepage = "https://github.com/ollama/ollama";
229 changelog = "https://github.com/ollama/ollama/releases/tag/v${version}";
230 license = licenses.mit;
231 platforms = if (rocmRequested || cudaRequested) then platforms.linux else platforms.unix;
232 mainProgram = "ollama";
233 maintainers = with maintainers; [