Introduced env file for API key

This commit is contained in:
2026-02-28 10:44:03 +01:00
parent 4d2b087d16
commit 2f9f0b6aaa
5 changed files with 442 additions and 468 deletions
+40 -53
View File
@@ -1655,68 +1655,55 @@ It ensures the Ollama CLI is available system-wide for interacting with local mo
It automatically pulls and prepares selected coding models (e.g., Qwen2.5-Coder and StarCoder2) at system activation.
#+begin_src nix :tangle home/apps/ai.nix :noweb tangle :mkdirp yes
{ config, pkgs, lib, flakeRoot, ... }:
{ config, pkgs, lib, ... }:
let
# Read environment variables from file
envVars = lib.genAttrs (builtins.splitStrings "\n" (builtins.readFile (toString ./assets/conf/apps/ai.env)))
(nameValue: builtins.splitString "=" nameValue);
in
{
# Install ZED and Ollama (Vulkan for CPU/AMD, use `ollama` for NVIDIA or `ollama-rocm` for AMD ROCm)
# Install ZED and Ollama
home.packages = [
pkgs.ollama-vulkan # For Vulkan (CPU/AMD). For NVIDIA: pkgs.ollama. For AMD ROCm: pkgs.ollama-rocm
pkgs.zed
];
# Environment variables for ZED and Ollama
home.sessionVariables = {
OLLAMA_HOST = "http://127.0.0.1:11434";
MISTRAL_API_KEY = "CWo91GHwIClzLj6bCLQ69IioSi54PpTZ"; # Replace with your actual Mistral API key
OLLAMA_HOST = envVars.OLLAMA_HOST or "http://127.0.0.1:11434";
MISTRAL_API_KEY = envVars.MISTRAL_API_KEY or "";
};
# Configure Ollama as a user service (starts with login)
home.services.ollama = {
enable = true;
package = pkgs.ollama-vulkan;
onStart = ''
# Start Ollama server
${pkgs.ollama-vulkan}/bin/ollama serve > /dev/null 2>&1 &
sleep 5 # Wait for server to start
# Pull coding and chat models at startup
${pkgs.ollama-vulkan}/bin/ollama pull codellama:70b # Best for coding
${pkgs.ollama-vulkan}/bin/ollama pull mixtral:8x7b # Best for chat
# To pull additional models, uncomment or add lines below:
# ${pkgs.ollama-vulkan}/bin/ollama pull llama3:8b # General-purpose
# ${pkgs.ollama-vulkan}/bin/ollama pull qwen2.5-coder:7b # Multilingual coding
# ${pkgs.ollama-vulkan}/bin/ollama pull qwen2.5-coder:32b # Larger coding model
# ${pkgs.ollama-vulkan}/bin/ollama pull starcoder2:15b # Alternative for code
'';
# Run Ollama as a user service
systemd.user.services.ollama = {
description = "Ollama service for local AI models";
wantedBy = [ "default.target" ];
after = [ "network.target" ];
serviceConfig = {
Type = "forking";
ExecStart = ''
${pkgs.ollama-vulkan}/bin/ollama serve
'';
ExecStartPost = ''
sleep 5
${pkgs.ollama-vulkan}/bin/ollama pull codellama:70b
${pkgs.ollama-vulkan}/bin/ollama pull mixtral:8x7b
'';
Restart = "on-failure";
};
};
# Configure ZED to use Ollama and Mistral API
home.file.".config/zed/settings.json".text = lib.mkForce ''
{
"mistral": {
"apiKey": "$MISTRAL_API_KEY", # Uses the environment variable set above
"defaultModel": "mistral-pro" # Default model for Mistral API calls
},
"ollama": {
"endpoint": "$OLLAMA_HOST", # Connects to local Ollama instance
"defaultModel": "codellama:70b" # Default model for Ollama plugin
},
# Add other ZED plugin configurations here if needed
}
'';
# --- Notes ---
# 1. Pulling Additional Models:
# To pull more models later, run:
# ollama pull <model-name>
# Example: ollama pull llama3:8b
# 2. Switching GPU Backends:
# - For NVIDIA: Replace `ollama-vulkan` with `ollama` (uses CUDA)
# - For AMD: Use `ollama-rocm` and ensure ROCm is installed
# 3. ZED Plugin Setup:
# - Install the Ollama and Mistral plugins in ZED via the plugin marketplace
# - The Ollama plugin will use the models pulled above
# - The Mistral plugin will use the MISTRAL_API_KEY for cloud access
# 4. Custom Prompts:
# To add custom prompts for Ollama, create a prompts.json file or
# configure prompts directly in the ZED Ollama plugin settings
# 5. Resource Management:
# Ollama runs as a user service and stops when you log out
# To run Ollama persistently, consider a systemd user service with `systemctl --user`
# Configure ZED using the external config file with substituted variables
home.file.".config/zed/settings.json".text = lib.mkForce (
builtins.readFile (toString ./assets/conf/apps/ai.conf)
// ''
{
"mistral": {
"apiKey": "${envVars.MISTRAL_API_KEY}"
},
"ollama": {
"endpoint": "${envVars.OLLAMA_HOST}"
}
}
''
);
}
#+end_src