{ lib, config, pkgs, flakeRoot, ... }: let # Path to the config file ollamaConfPath = flakeRoot + "/apps/ai/ollama/ollama.conf"; ollamaConf = builtins.readFile ollamaConfPath; # Simple parser for key="value" format that takes the last value parseConf = lines: lib.foldl' (acc, line) (if builtins.match "^[^=]+=\"[^\"]+\"$" line != null then let parts = builtins.splitString "\"" line; key = builtins.substring 0 (builtins.stringLength parts.0 - 1) parts.0; value = parts.1; in acc // { ${key} = value; } else acc) {} (lib.filter (line: line != "" && ! (lib.hasPrefix "#" line)) (lib.splitString "\n" ollamaConf)); conf = parseConf ollamaConf; in { # Install Ollama package home.packages = with pkgs; [ ollama ]; # Set environment variables home.sessionVariables = { OLLAMA_HOST = conf.OLLAMA_HOST or "http://127.0.0.1:11434"; OLAMA_DEFAULT_MODEL = conf.OLAMA_DEFAULT_MODEL or "codellama:70b"; }; # Configure and enable Ollama service systemd.user.services.ollama = { description = "Ollama service"; wantedBy = [ "default.target" ]; serviceConfig = { ExecStart = "${pkgs.ollama}/bin/ollama serve"; Restart = "on-failure"; Environment = "OLLAMA_HOST=${conf.OLLAMA_HOST or "http://127.0.0.1:11434"}"; }; }; }