{ lib, config, pkgs, flakeRoot, ... }: let # Path to the config file ollamaConfPath = flakeRoot + "/assets/conf/apps/ai/ollama/ollama.conf"; ollamaConf = builtins.readFile ollamaConfPath; # Helper function to extract values from key="value" lines extractValue = key: let # Split into lines and filter out comments/empty lines lines = lib.filter (line: line != "" && ! (lib.hasPrefix "#" line)) (lib.splitString "\n" ollamaConf); # Find the last matching line matchingLines = lib.filter (line: builtins.match ("^${key}=\"") line != null) lines; lastLine = if matchingLines != [] then lib.elemAt matchingLines (lib.length matchingLines - 1) else null; in if lastLine != null then # Extract everything after the =" builtins.substring (builtins.stringLength (builtins.match ("^${key}=\"") lastLine) + 1) (builtins.stringLength lastLine - 1) # Remove trailing quote lastLine else null; ollamaHost = extractValue "OLLAMA_HOST"; ollamaDefaultModel = extractValue "OLLAMA_DEFAULT_MODEL"; in { # Install Ollama home.packages = with pkgs; [ ollama ]; # Configure Ollama environment variables home.sessionVariables = { OLLAMA_HOST = if ollamaHost != null then ollamaHost else "http://127.0.0.1:11434"; OLAMA_DEFAULT_MODEL = if ollamaDefaultModel != null then ollamaDefaultModel else "codellama:70b"; }; # Start Ollama service systemd.user.services.ollama = { serviceConfig = { Description = "Ollama service"; WantedBy = [ "default.target" ]; # Changed from multi-user.target ExecStart = "${pkgs.ollama}/bin/ollama serve"; Restart = "on-failure"; Environment = "OLLAMA_HOST=${if ollamaHost != null then ollamaHost else "http://127.0.0.1:11434"'"; }; }; }