2024-02-23 04:41:25 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
2024-02-27 23:30:38 +00:00
|
|
|
inherit (lib) types;
|
2023-12-29 00:46:01 +00:00
|
|
|
|
|
|
|
cfg = config.services.ollama;
|
2024-02-23 04:41:25 +00:00
|
|
|
ollamaPackage = cfg.package.override {
|
|
|
|
inherit (cfg) acceleration;
|
2024-02-28 21:47:27 +00:00
|
|
|
linuxPackages = config.boot.kernelPackages // {
|
2024-02-27 23:30:38 +00:00
|
|
|
nvidia_x11 = config.hardware.nvidia.package;
|
|
|
|
};
|
2024-02-23 04:41:25 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2023-12-29 00:46:01 +00:00
|
|
|
options = {
|
|
|
|
services.ollama = {
|
2024-03-14 06:57:42 +00:00
|
|
|
enable = lib.mkEnableOption "ollama server for local large language models";
|
|
|
|
package = lib.mkPackageOption pkgs "ollama" { };
|
2024-01-20 13:36:55 +00:00
|
|
|
listenAddress = lib.mkOption {
|
2024-02-27 23:30:38 +00:00
|
|
|
type = types.str;
|
2024-01-20 13:36:55 +00:00
|
|
|
default = "127.0.0.1:11434";
|
2024-03-14 06:57:42 +00:00
|
|
|
example = "0.0.0.0:11111";
|
|
|
|
description = ''
|
|
|
|
The address which the ollama server HTTP interface binds and listens to.
|
2024-01-20 13:36:55 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-02-23 04:41:25 +00:00
|
|
|
acceleration = lib.mkOption {
|
2024-02-27 23:30:38 +00:00
|
|
|
type = types.nullOr (types.enum [ "rocm" "cuda" ]);
|
2024-02-23 04:41:25 +00:00
|
|
|
default = null;
|
|
|
|
example = "rocm";
|
2024-03-14 06:57:42 +00:00
|
|
|
description = ''
|
|
|
|
What interface to use for hardware acceleration.
|
2024-02-23 04:41:25 +00:00
|
|
|
|
|
|
|
- `rocm`: supported by modern AMD GPUs
|
|
|
|
- `cuda`: supported by modern NVIDIA GPUs
|
|
|
|
'';
|
|
|
|
};
|
2024-03-14 06:57:42 +00:00
|
|
|
environmentVariables = lib.mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
HOME = "/tmp";
|
|
|
|
OLLAMA_LLM_LIBRARY = "cpu";
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Set arbitrary environment variables for the ollama service.
|
|
|
|
|
|
|
|
Be aware that these are only seen by the ollama server (systemd service),
|
|
|
|
not normal invocations like `ollama run`.
|
|
|
|
Since `ollama run` is mostly a shell around the ollama server, this is usually sufficient.
|
|
|
|
'';
|
|
|
|
};
|
2023-12-29 00:46:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2024-03-14 06:57:42 +00:00
|
|
|
systemd.services.ollama = {
|
|
|
|
description = "Server for local large language models";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
environment = cfg.environmentVariables // {
|
|
|
|
HOME = "%S/ollama";
|
|
|
|
OLLAMA_MODELS = "%S/ollama/models";
|
|
|
|
OLLAMA_HOST = cfg.listenAddress;
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${lib.getExe ollamaPackage} serve";
|
|
|
|
WorkingDirectory = "%S/ollama";
|
|
|
|
StateDirectory = [ "ollama" ];
|
|
|
|
DynamicUser = true;
|
2023-12-29 00:46:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-02-23 04:41:25 +00:00
|
|
|
environment.systemPackages = [ ollamaPackage ];
|
2023-12-29 00:46:01 +00:00
|
|
|
};
|
|
|
|
|
2024-02-23 04:41:25 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ abysssol onny ];
|
2023-12-29 00:46:01 +00:00
|
|
|
}
|