|
| 1 | +# Based on https://github.com/shivaraj-bh/ollama-flake/blob/main/services/ollama.nix |
| 2 | +{ pkgs, lib, name, config, ... }: |
| 3 | +let |
| 4 | + inherit (lib) types; |
| 5 | + ollamaPackage = pkgs.ollama.override { |
| 6 | + inherit (config) acceleration; |
| 7 | + linuxPackages = config.boot.kernelPackages // { |
| 8 | + nvidia_x11 = config.hardware.nvidia.package; |
| 9 | + }; |
| 10 | + }; |
| 11 | +in |
| 12 | +{ |
| 13 | + options = { |
| 14 | + enable = lib.mkEnableOption "Enable the Ollama service"; |
| 15 | + package = lib.mkOption { |
| 16 | + type = types.package; |
| 17 | + default = ollamaPackage; |
| 18 | + description = "The Ollama package to use"; |
| 19 | + }; |
| 20 | + port = lib.mkOption { |
| 21 | + type = types.port; |
| 22 | + default = 11434; |
| 23 | + description = "The port on which the Ollama service's REST API will listen"; |
| 24 | + }; |
| 25 | + host = lib.mkOption { |
| 26 | + type = types.str; |
| 27 | + default = "127.0.0.1"; |
| 28 | + example = "0.0.0.0"; |
| 29 | + description = "The host on which the Ollama service's REST API will listen"; |
| 30 | + }; |
| 31 | + dataDir = lib.mkOption { |
| 32 | + type = types.str; |
| 33 | + default = "./data/ollama"; |
| 34 | + description = '' |
| 35 | + The directory containing the Ollama models. |
| 36 | + Sets the `OLLAMA_MODELS` environment variable. |
| 37 | + ''; |
| 38 | + }; |
| 39 | + keepAlive = lib.mkOption { |
| 40 | + type = types.str; |
| 41 | + default = "5m"; |
| 42 | + description = '' |
| 43 | + The duration that models stay loaded in memory. |
| 44 | + Sets the `OLLAMA_KEEP_ALIVE` environment variable. |
| 45 | +
|
| 46 | + Note: Use a duration string like "5m" for 5 minutes. Or "70" for 70 seconds. |
| 47 | + ''; |
| 48 | + example = "70"; |
| 49 | + }; |
| 50 | + models = lib.mkOption { |
| 51 | + type = types.listOf types.str; |
| 52 | + default = [ ]; |
| 53 | + description = '' |
| 54 | + The models to load post start. |
| 55 | + Search for models of your choice from: https://ollama.com/library |
| 56 | + ''; |
| 57 | + }; |
| 58 | + acceleration = lib.mkOption { |
| 59 | + type = types.nullOr (types.enum [ false "rocm" "cuda" ]); |
| 60 | + default = null; |
| 61 | + example = "rocm"; |
| 62 | + description = '' |
| 63 | + What interface to use for hardware acceleration. |
| 64 | +
|
| 65 | + - `null`: default behavior |
| 66 | + - if `nixpkgs.config.rocmSupport` is enabled, uses `"rocm"` |
| 67 | + - if `nixpkgs.config.cudaSupport` is enabled, uses `"cuda"` |
| 68 | + - otherwise defaults to `false` |
| 69 | + - `false`: disable GPU, only use CPU |
| 70 | + - `"rocm"`: supported by most modern AMD GPUs |
| 71 | + - `"cuda"`: supported by most modern NVIDIA GPUs |
| 72 | + ''; |
| 73 | + }; |
| 74 | + environment = lib.mkOption { |
| 75 | + type = types.attrsOf types.str; |
| 76 | + default = { }; |
| 77 | + example = { |
| 78 | + OLLAMA_DEBUG = "1"; |
| 79 | + }; |
| 80 | + description = '' |
| 81 | + Extra environment variables passed to the `ollama-server` process. |
| 82 | + ''; |
| 83 | + }; |
| 84 | + |
| 85 | + outputs.settings = lib.mkOption { |
| 86 | + type = types.deferredModule; |
| 87 | + internal = true; |
| 88 | + readOnly = true; |
| 89 | + default = { |
| 90 | + processes = { |
| 91 | + "${name}" = |
| 92 | + let |
| 93 | + startScript = pkgs.writeShellApplication { |
| 94 | + name = "ollama-server"; |
| 95 | + text = '' |
| 96 | + if [ ! -d ${config.dataDir} ]; then |
| 97 | + echo "Creating directory ${config.dataDir}" |
| 98 | + mkdir -p ${config.dataDir} |
| 99 | + fi |
| 100 | +
|
| 101 | + ${lib.getExe config.package} serve |
| 102 | + ''; |
| 103 | + }; |
| 104 | + in |
| 105 | + { |
| 106 | + command = startScript; |
| 107 | + |
| 108 | + environment = { |
| 109 | + OLLAMA_MODELS = config.dataDir; |
| 110 | + OLLAMA_HOST = "${config.host}:${toString config.port}"; |
| 111 | + OLLAMA_KEEP_ALIVE = config.keepAlive; |
| 112 | + } // config.environment; |
| 113 | + |
| 114 | + readiness_probe = { |
| 115 | + http_get = { |
| 116 | + host = config.host; |
| 117 | + port = config.port; |
| 118 | + }; |
| 119 | + initial_delay_seconds = 2; |
| 120 | + period_seconds = 10; |
| 121 | + timeout_seconds = 4; |
| 122 | + success_threshold = 1; |
| 123 | + failure_threshold = 5; |
| 124 | + }; |
| 125 | + namespace = name; |
| 126 | + availability.restart = "on_failure"; |
| 127 | + }; |
| 128 | + |
| 129 | + "${name}-models" = { |
| 130 | + command = pkgs.writeShellApplication { |
| 131 | + name = "ollama-models"; |
| 132 | + text = '' |
| 133 | + set -x |
| 134 | + OLLAMA_HOST=${config.host}:${toString config.port} |
| 135 | + export OLLAMA_HOST |
| 136 | + models="${lib.concatStringsSep " " config.models}" |
| 137 | + for model in $models |
| 138 | + do |
| 139 | + ${lib.getExe config.package} pull "$model" |
| 140 | + done |
| 141 | + ''; |
| 142 | + }; |
| 143 | + namespace = name; |
| 144 | + depends_on."${name}".condition = "process_healthy"; |
| 145 | + }; |
| 146 | + }; |
| 147 | + }; |
| 148 | + }; |
| 149 | + }; |
| 150 | +} |
0 commit comments