This guide covers setting up Google Gemini on NixOS, a purely functional Linux distribution that offers reproducible system configurations through the Nix package manager.
Advantages of NixOS for Gemini Deployments
NixOS provides several benefits for DevOps professionals working with AI tools like Gemini:
Reproducible environments: Identical deployment across all systems
Declarative configuration: System configuration as code
Isolated dependencies: Prevent conflicts between different Python versions or libraries
Rollbacks: Easy recovery if something breaks
Development shells: Isolated environments for different AI projects
Installation Methods
Method 1: Using Nix Flakes (Recommended)
provide a modern, reproducible approach to Nix packages.
First, ensure flakes are enabled in your NixOS configuration:
# In configuration.nix
{ config, pkgs, ... }:
{
systemd.services.gemini-agent = {
description = "Gemini AI Agent Service";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
User = "gemini-service";
WorkingDirectory = "/var/lib/gemini-agent";
ExecStart = "${pkgs.python311.withPackages (ps: with ps; [google-generativeai])}/bin/python /var/lib/gemini-agent/agent.py";
Restart = "on-failure";
};
environment = {
# Use agenix or similar for production secrets
GOOGLE_API_KEY = "!cat /run/secrets/gemini-api-key";
};
};
users.users.gemini-service = {
isSystemUser = true;
group = "gemini-service";
home = "/var/lib/gemini-agent";
createHome = true;
};
users.groups.gemini-service = {};
}
Verification & Testing
Test your setup with a simple script:
#!/usr/bin/env python
import google.generativeai as genai
import os
# Configure the API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Test the API with a NixOS-related prompt
model = genai.GenerativeModel('gemini-pro')
result = model.generate_content("Explain how Nix's reproducibility benefits DevOps teams.")
print(result.text)