nixos/ollama: add option for hardware acceleration

This commit is contained in:
abysssol 2024-02-22 23:41:25 -05:00
parent 70e834c19b
commit b8d8c1f207

View file

@ -1,9 +1,14 @@
{ config, lib, pkgs, ... }: let { config, lib, pkgs, ... }:
let
inherit (lib.types) nullOr enum;
cfg = config.services.ollama; cfg = config.services.ollama;
ollamaPackage = cfg.package.override {
in { inherit (cfg) acceleration;
linuxPackages.nvidia_x11 = config.hardware.nvidia.package;
};
in
{
options = { options = {
services.ollama = { services.ollama = {
enable = lib.mkEnableOption ( enable = lib.mkEnableOption (
@ -16,12 +21,22 @@ in {
Specifies the bind address on which the ollama server HTTP interface listens. Specifies the bind address on which the ollama server HTTP interface listens.
''; '';
}; };
acceleration = lib.mkOption {
type = nullOr (enum [ "rocm" "cuda" ]);
default = null;
example = "rocm";
description = lib.mdDoc ''
Specifies the interface to use for hardware acceleration.
- `rocm`: supported by modern AMD GPUs
- `cuda`: supported by modern NVIDIA GPUs
'';
};
package = lib.mkPackageOption pkgs "ollama" { }; package = lib.mkPackageOption pkgs "ollama" { };
}; };
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
systemd = { systemd = {
services.ollama = { services.ollama = {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
@ -33,7 +48,7 @@ in {
OLLAMA_HOST = cfg.listenAddress; OLLAMA_HOST = cfg.listenAddress;
}; };
serviceConfig = { serviceConfig = {
ExecStart = "${lib.getExe cfg.package} serve"; ExecStart = "${lib.getExe ollamaPackage} serve";
WorkingDirectory = "/var/lib/ollama"; WorkingDirectory = "/var/lib/ollama";
StateDirectory = [ "ollama" ]; StateDirectory = [ "ollama" ];
DynamicUser = true; DynamicUser = true;
@ -41,10 +56,8 @@ in {
}; };
}; };
environment.systemPackages = [ cfg.package ]; environment.systemPackages = [ ollamaPackage ];
}; };
meta.maintainers = with lib.maintainers; [ onny ]; meta.maintainers = with lib.maintainers; [ abysssol onny ];
} }