Skip to content

Commit 6645470

Browse files
committed
ollama: new service
1 parent efa9010 commit 6645470

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
[comment]: # (Please add your documentation on top of this line)
4+
5+
@AUTOGEN_OPTIONS@

examples/ollama/devenv.nix

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ ... }:
2+
3+
{
4+
services.ollama = {
5+
enable = true;
6+
address = "0.0.0.0";
7+
port = 11434;
8+
loadModels = [
9+
"llama3.2:3b"
10+
];
11+
# acceleration = "";
12+
};
13+
}

src/modules/services/ollama.nix

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{ config
2+
, lib
3+
, pkgs
4+
, ...
5+
}:
6+
7+
with lib;
8+
9+
let
10+
cfg = config.services.ollama;
11+
inherit (lib) types;
12+
13+
ollamaPackage = cfg.package.override { inherit (cfg) acceleration; };
14+
15+
loadModels = pkgs.writeShellScriptBin "loadModels" ''
16+
total=${toString (builtins.length cfg.loadModels)}
17+
failed=0
18+
19+
for model in ${lib.escapeShellArgs cfg.loadModels}; do
20+
'${lib.getExe ollamaPackage}' pull "$model" &
21+
done
22+
23+
for job in $(jobs -p); do
24+
set +e
25+
wait $job
26+
exit_code=$?
27+
set -e
28+
29+
if [ $exit_code != 0 ]; then
30+
failed=$((failed + 1))
31+
fi
32+
done
33+
34+
if [ $failed != 0 ]; then
35+
echo "error: $failed out of $total attempted model downloads failed" >&2
36+
exit 1
37+
fi
38+
'';
39+
in
40+
{
41+
options = {
42+
services.ollama = {
43+
enable = mkEnableOption "ollama";
44+
package = lib.mkPackageOption pkgs "ollama" { };
45+
46+
address = lib.mkOption {
47+
type = types.str;
48+
default = "127.0.0.1";
49+
example = "[::]";
50+
description = ''
51+
The host address which the ollama server HTTP interface listens to.
52+
'';
53+
};
54+
55+
port = lib.mkOption {
56+
type = types.port;
57+
default = 11434;
58+
example = 11111;
59+
description = ''
60+
Which port the ollama server listens to.
61+
'';
62+
};
63+
64+
loadModels = lib.mkOption {
65+
type = types.listOf types.str;
66+
default = [ ];
67+
description = ''
68+
Download these models using `ollama pull` as soon as `ollama.service` has started.
69+
70+
This creates a systemd unit `ollama-model-loader.service`.
71+
72+
Search for models of your choice from: https://ollama.com/library
73+
'';
74+
};
75+
76+
acceleration = lib.mkOption {
77+
type = types.nullOr (
78+
types.enum [
79+
false
80+
"rocm"
81+
"cuda"
82+
]
83+
);
84+
default = null;
85+
example = "rocm";
86+
description = ''
87+
What interface to use for hardware acceleration.
88+
89+
- `null`: default behavior
90+
- if `nixpkgs.config.rocmSupport` is enabled, uses `"rocm"`
91+
- if `nixpkgs.config.cudaSupport` is enabled, uses `"cuda"`
92+
- otherwise defaults to `false`
93+
- `false`: disable GPU, only use CPU
94+
- `"rocm"`: supported by most modern AMD GPUs
95+
- may require overriding gpu type with `services.ollama.rocmOverrideGfx`
96+
if rocm doesn't detect your AMD gpu
97+
- `"cuda"`: supported by most modern NVIDIA GPUs
98+
'';
99+
};
100+
};
101+
};
102+
103+
config = lib.mkIf cfg.enable {
104+
assertions = [{
105+
assertion = cfg.enable;
106+
message = ''
107+
To use Ollama, you have to enable it. (services.ollama.enable = true;)
108+
'';
109+
}];
110+
111+
env = {
112+
OLLAMA_HOST = "${cfg.address}:${toString cfg.port}";
113+
};
114+
115+
scripts.loadModels.exec = ''
116+
exec ${loadModels}/bin/loadModels "$@"
117+
'';
118+
119+
processes.ollama = {
120+
exec = "${lib.getExe ollamaPackage} serve";
121+
122+
process-compose = {
123+
readiness_probe = {
124+
exec.command = "${pkgs.curl}/bin/curl -f -k ${cfg.address}:${toString cfg.port}";
125+
initial_delay_seconds = 2;
126+
period_seconds = 10;
127+
timeout_seconds = 2;
128+
success_threshold = 1;
129+
failure_threshold = 5;
130+
};
131+
132+
availability.restart = "on_failure";
133+
};
134+
};
135+
};
136+
}

tests/ollama/devenv.nix

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
services.ollama = {
3+
enable = true;
4+
address = "0.0.0.0";
5+
port = 11434;
6+
loadModels = [
7+
"llama3.2:3b"
8+
];
9+
# acceleration = "";
10+
};
11+
}

0 commit comments

Comments
 (0)