57 lines
1.9 KiB
Nix
57 lines
1.9 KiB
Nix
{ lib, config, pkgs, flakeRoot, ... }:
|
|
|
|
let
|
|
# Path to the config file (relative to your flake or Home Manager root)
|
|
ollamaConfPath = flakeRoot + "/assets/conf/apps/ai/ollama/ollama.conf";
|
|
# Read and parse the config file (assuming it's in shell variable format)
|
|
ollamaConf = builtins.readFile ollamaConfPath;
|
|
|
|
# Split the file into lines and filter out comments/empty lines
|
|
lines = lib.filter (line: line != "" && ! (lib.hasPrefix "#" line))
|
|
(lib.splitString "\n" ollamaConf);
|
|
|
|
# Extract the last value for a given key
|
|
extractValue = key:
|
|
let
|
|
# Find all lines matching the key
|
|
matches = lib.map (
|
|
line: builtins.match ("^${key}=\"([^\"]+)\"$") line
|
|
) lines;
|
|
# Filter out null matches and get the last one
|
|
nonNullMatches = lib.filter (match: match != null) matches;
|
|
in
|
|
if nonNullMatches != [] then
|
|
# Return the first capture group of the last match
|
|
builtins.elemAt (builtins.elemAt (lib.elemAt nonNullMatches (lib.length nonNullMatches - 1)) 0) 1
|
|
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";
|
|
};
|
|
|
|
# Optional: Start Ollama service (if using NixOS)
|
|
systemd.user.services.ollama = {
|
|
description = "Ollama service";
|
|
wantedBy = [ "multi-user.target" ];
|
|
serviceConfig = {
|
|
ExecStart = "${pkgs.ollama}/bin/ollama serve";
|
|
Restart = "on-failure";
|
|
Environment = [
|
|
"OLLAMA_HOST=${if ollamaHost != null then ollamaHost else "http://127.0.0.1:11434"}"
|
|
];
|
|
};
|
|
};
|
|
}
|