Merge pull request #305076 from abysssol/ollama-sandbox

nixos/ollama: add options to bypass sandboxing
This commit is contained in:
Pol Dellaiera 2024-04-20 10:49:28 +02:00 committed by GitHub
commit 31805d0367
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,6 +21,8 @@ in
example = "/home/foo"; example = "/home/foo";
description = '' description = ''
The home directory that the ollama service is started in. The home directory that the ollama service is started in.
See also `services.ollama.writablePaths` and `services.ollama.sandbox`.
''; '';
}; };
models = lib.mkOption { models = lib.mkOption {
@ -29,6 +31,37 @@ in
example = "/path/to/ollama/models"; example = "/path/to/ollama/models";
description = '' description = ''
The directory that the ollama service will read models from and download new models to. The directory that the ollama service will read models from and download new models to.
See also `services.ollama.writablePaths` and `services.ollama.sandbox`
if downloading models or other mutation of the filesystem is required.
'';
};
sandbox = lib.mkOption {
type = types.bool;
default = true;
example = false;
description = ''
Whether to enable systemd's sandboxing capabilities.
This sets [`DynamicUser`](
https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#DynamicUser=
), which runs the server as a unique user with read-only access to most of the filesystem.
See also `services.ollama.writablePaths`.
'';
};
writablePaths = lib.mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "/home/foo" "/mnt/foo" ];
description = ''
Paths that the server should have write access to.
This sets [`ReadWritePaths`](
https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#ReadWritePaths=
), which allows specified paths to be written to through the default sandboxing.
See also `services.ollama.sandbox`.
''; '';
}; };
listenAddress = lib.mkOption { listenAddress = lib.mkOption {
@ -59,8 +92,8 @@ in
type = types.attrsOf types.str; type = types.attrsOf types.str;
default = { }; default = { };
example = { example = {
HOME = "/tmp";
OLLAMA_LLM_LIBRARY = "cpu"; OLLAMA_LLM_LIBRARY = "cpu";
HIP_VISIBLE_DEVICES = "0,1";
}; };
description = '' description = ''
Set arbitrary environment variables for the ollama service. Set arbitrary environment variables for the ollama service.
@ -87,7 +120,8 @@ in
ExecStart = "${lib.getExe ollamaPackage} serve"; ExecStart = "${lib.getExe ollamaPackage} serve";
WorkingDirectory = cfg.home; WorkingDirectory = cfg.home;
StateDirectory = [ "ollama" ]; StateDirectory = [ "ollama" ];
DynamicUser = true; DynamicUser = cfg.sandbox;
ReadWritePaths = cfg.writablePaths;
}; };
}; };