DevOps help for Cloud Platform Engineers
  • Welcome!
  • Quick Start Guide
  • About Me
  • CV
  • 🧠DevOps & SRE Foundations
    • DevOps Overview
      • Engineering Fundamentals
      • Implementing DevOps Strategy
      • DevOps Readiness Assessment
      • Lifecycle Management
      • The 12 Factor App
      • Design for Self Healing
      • Incident Management Best Practices (2025)
    • SRE Fundamentals
      • Toil Reduction
      • System Simplicity
      • Real-world Scenarios
        • AWS VM Log Monitoring API
    • Agile Development
      • Team Agreements
        • Definition of Done
        • Definition of Ready
        • Team Manifesto
        • Working Agreement
    • Industry Scenarios
      • Finance and Banking
      • Public Sector (UK/EU)
      • Energy Sector Edge Computing
  • DevOps Practices
    • Platform Engineering
    • FinOps
    • Observability
      • Modern Practices
  • 🚀Modern DevOps Practices
    • Infrastructure Testing
    • Modern Development
    • Database DevOps
  • 🛠️Infrastructure as Code (IaC)
    • Terraform
      • Getting Started - Installation and initial setup [BEGINNER]
      • Cloud Integrations - Provider-specific implementations
        • Azure Scenarios
        • AWS Scenarios
        • GCP Scenarios
      • Testing and Validation - Ensuring infrastructure quality
        • Unit Testing
        • Integration Testing
        • End-to-End Testing
        • Terratest Guide
      • Best Practices - Production-ready implementation strategies
        • State Management
        • Security
        • Code Organization
        • Performance
      • Tools & Utilities - Enhancing the Terraform workflow
        • Terraform Docs
        • TFLint
        • Checkov
        • Terrascan
      • CI/CD Integration - Automating infrastructure deployment
        • GitHub Actions - GitHub-based automation workflows
        • Azure Pipelines - Azure DevOps integration
        • GitLab CI - GitLab-based deployment pipelines
    • Bicep
      • Getting Started - First steps with Bicep [BEGINNER]
      • Template Specs
      • Best Practices - Guidelines for effective Bicep implementations
      • Modules - Building reusable components [INTERMEDIATE]
      • Examples - Sample implementations for common scenarios
      • Advanced Features
      • CI/CD Integration - Automating Bicep deployments
        • GitHub Actions
        • Azure Pipelines
  • 💰Cost Management & FinOps
    • Cloud Cost Optimization
  • 🐳Containers & Orchestration
    • Containerization Overview
    • Docker
      • Dockerfile Best Practices
      • Docker Compose
    • Kubernetes
      • CLI Tools - Essential command-line utilities
        • Kubectl
        • Kubens
        • Kubectx
      • Core Concepts
      • Components
      • Best Practices
        • Pod Security
        • Security Monitoring
        • Resource Limits
      • Advanced Features - Beyond the basics [ADVANCED]
        • Service Mesh
        • Ingress Controllers
          • NGINX
          • Traefik
          • Kong
          • Gloo Edge
      • Troubleshooting - Diagnosing and resolving common issues
        • Pod Troubleshooting Commands
      • Enterprise Architecture
      • Health Management
      • Security & Compliance
      • Virtual Clusters
    • OpenShift
  • Service Mesh & Networking
    • Service Mesh Implementation
  • Architecture Patterns
    • Data Mesh
    • Multi-Cloud Networking
    • Disaster Recovery
    • Chaos Engineering
  • Edge Computing
    • Implementation Guide
    • Serverless Edge
    • IoT Edge Patterns
    • Real-Time Processing
    • Edge AI/ML
    • Security Hardening
    • Observability Patterns
    • Network Optimization
    • Storage Patterns
  • 🔄CI/CD & GitOps
    • CI/CD Overview
    • Continuous Integration
    • Continuous Delivery
      • Deployment Strategies
      • Secrets Management
      • Blue-Green Deployments
      • Deployment Metrics
      • Progressive Delivery
      • Release Management for DevOps/SRE (2025)
    • CI/CD Platforms - Tool selection and implementation
      • Azure DevOps
        • Pipelines
          • Stages
          • Jobs
          • Steps
          • Templates - Reusable pipeline components
          • Extends
          • Service Connections - External service authentication
          • Best Practices for 2025
          • Agents and Runners
          • Third-Party Integrations
          • Azure DevOps CLI
        • Boards & Work Items
      • GitHub Actions
      • GitLab
        • GitLab Runner
        • Real-life scenarios
        • Installation guides
        • Pros and Cons
        • Comparison with alternatives
    • GitOps
      • Modern GitOps Practices
      • GitOps Patterns for Multi-Cloud (2025)
      • Flux
        • Overview
        • Progressive Delivery
        • Use GitOps with Flux, GitHub and AKS
  • Source Control
    • Source Control Overview
    • Git Branching Strategies
    • Component Versioning
    • Kubernetes Manifest Versioning
    • GitLab
    • Creating a Fork
    • Naming Branches
    • Pull Requests
    • Integrating LLMs into Source Control Workflows
  • ☁️Cloud Platforms
    • Cloud Strategy
    • Azure
      • Best Practices
      • Landing Zones
      • Services
      • Monitoring
      • Administration Tools - Platform management interfaces
        • Azure PowerShell
        • Azure CLI
      • Tips & Tricks
    • AWS
      • Authentication
      • Best Practices
      • Tips & Tricks
    • Google Cloud
      • Services
    • Private Cloud
  • 🔐Security & Compliance
    • DevSecOps Overview
    • DevSecOps Pipeline Security
    • DevSecOps
      • Real-life Examples
      • Scanning & Protection - Automated security tooling
        • Dependency Scanning
        • Credential Scanning
        • Container Security Scanning
        • Static Code Analysis
          • Best Practices
          • Tool Integration Guide
          • Pipeline Configuration
      • CI/CD Security
      • Secrets Rotation
    • Supply Chain Security
      • SLSA Framework
      • Binary Authorization
      • Artifact Signing
    • Security Best Practices
      • Threat Modeling
      • Kubernetes Security
    • SecOps
    • Zero Trust Model
    • Cloud Compliance
      • ISO/IEC 27001:2022
      • ISO 22301:2019
      • PCI DSS
      • CSA STAR
    • Security Frameworks
    • SIEM and SOAR
  • Security Architecture
    • Zero Trust Implementation
      • Identity Management
      • Network Security
      • Access Control
  • 🔍Observability & Monitoring
    • Observability Fundamentals
    • Logging
    • Metrics
    • Tracing
    • Dashboards
    • SLOs and SLAs
    • Observability as Code
    • Pipeline Observability
  • 🧪Testing Strategies
    • Testing Overview
    • Modern Testing Approaches
    • End-to-End Testing
    • Unit Testing
    • Performance Testing
      • Load Testing
    • Fault Injection Testing
    • Integration Testing
    • Smoke Testing
  • 🤖AI Integration
    • AIops Overview
      • Workflow Automation
      • Predictive Analytics
      • Code Quality
  • 🧠AI & LLM Integration
    • Overview
    • Claude
      • Installation Guide
      • Project Guides
      • MCP Server Setup
      • LLM Comparison
    • Ollama
      • Installation Guide
      • Configuration
      • Models and Fine-tuning
      • DevOps Usage
      • Docker Setup
      • GPU Setup
      • Open WebUI
    • Copilot
      • Installation Guide
      • VS Code Integration
      • CLI Usage
    • Gemini
      • Installation Guides - Platform-specific setup
        • Linux Installation
        • WSL Installation
        • NixOS Installation
      • Gemini 2.5 Features
      • Roles and Agents
      • NotebookML Guide
      • Cloud Infrastructure Deployment
      • Summary
  • 💻Development Environment
    • Tools Overview
    • DevOps Tools
    • Operating Systems - Development platforms
      • NixOS
        • Installation
        • Nix Language Guide
        • DevEnv with Nix
        • Cloud Deployments
      • WSL2
        • Distributions
        • Terminal Setup
    • Editor Environments
    • CLI Tools
      • Azure CLI
      • PowerShell
      • Linux Commands
      • YAML Tools
  • 📚Programming Languages
    • Python
    • Go
    • JavaScript/TypeScript
    • Java
    • Rust
  • 📖Documentation Best Practices
    • Documentation Strategy
    • Project Documentation
    • Release Notes
    • Static Sites
    • Documentation Templates
    • Real-World Examples
  • 📋Reference Materials
    • Glossary
    • Tool Comparison
    • Recommended Reading
    • Troubleshooting Guide
  • Platform Engineering
    • Implementation Guide
  • FinOps
    • Implementation Guide
  • AIOps
    • LLMOps Guide
  • Development Setup
    • Development Setup
Powered by GitBook
On this page
  • What is nix-anywhere?
  • Prerequisites
  • Setup Project Structure
  • Base flake.nix
  • AWS Deployment
  • 1. Create the AWS host configuration file
  • 2. Provision an EC2 instance
  • 3. Deploy NixOS with nix-anywhere
  • GCP Deployment
  • 1. Create the GCP host configuration file
  • 2. Provision a GCP Instance
  • 3. Deploy NixOS with nix-anywhere
  • Azure Deployment
  • 1. Create the Azure host configuration file
  • 2. Provision an Azure VM
  • 3. Deploy NixOS with nix-anywhere
  • Common Modules: cloud-config.nix
  • Advanced Usage
  • Automatic Rollback on Failure
  • CI/CD Pipeline Integration
  • Troubleshooting
  • References
Edit on GitHub
  1. Development Environment
  2. Operating Systems - Development platforms
  3. NixOS

Cloud Deployments

Deploying NixOS to AWS, GCP, and Azure with nix-anywhere

nix-anywhere is a powerful tool for deploying NixOS systems to remote machines. This guide shows how to use it for provisioning NixOS on major cloud platforms: AWS, GCP, and Azure.

What is nix-anywhere?

nix-anywhere is a tool that lets you deploy NixOS configurations to any machine with SSH access. It has several advantages for cloud deployments:

  • No NixOS Required: Target machines only need a Linux kernel and SSH access

  • Safe Upgrades: Atomic upgrades with automatic rollback on failure

  • Simple Deployment: Single command to deploy your configuration

  • Infrastructure as Code: Fully declarative configuration of your cloud instances

  • Multi-Platform: Works with any cloud provider

Prerequisites

  1. Install Nix with flakes enabled

    # Install Nix if you haven't already
    sh <(curl -L https://nixos.org/nix/install) --daemon
    
    # Enable flakes (add to ~/.config/nix/nix.conf)
    mkdir -p ~/.config/nix
    echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
  2. Install cloud provider CLI tools

    # For AWS
    nix-env -i awscli2
    
    # For GCP
    nix-env -i google-cloud-sdk
    
    # For Azure
    nix-env -i azure-cli

Setup Project Structure

Create a project directory with the following structure:

nixos-cloud-deploy/
├── flake.nix
├── hosts/
│   ├── aws.nix
│   ├── gcp.nix
│   └── azure.nix
└── modules/
    └── cloud-config.nix

Base flake.nix

{
  description = "NixOS Cloud Deployment with nix-anywhere";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    nix-anywhere = {
      url = "github:nix-community/nixos-anywhere";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, nix-anywhere, ... }: {
    nixosConfigurations = {
      aws-instance = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [ ./hosts/aws.nix ];
      };
      
      gcp-instance = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [ ./hosts/gcp.nix ];
      };
      
      azure-instance = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux"; 
        modules = [ ./hosts/azure.nix ];
      };
    };
  };
}

AWS Deployment

1. Create the AWS host configuration file

Create the file hosts/aws.nix:

{ config, lib, pkgs, ... }:

{
  imports = [
    ../modules/cloud-config.nix
  ];
  
  # System configuration
  system.stateVersion = "23.11";
  
  # AWS-specific configuration
  boot.loader.grub.device = lib.mkForce "/dev/nvme0n1";
  fileSystems."/" = { 
    device = "/dev/nvme0n1p1";
    fsType = "ext4";
  };
  
  # Networking
  networking = {
    hostName = "nixos-aws";
    networkmanager.enable = true;
  };
  
  # User configuration
  users.users.admin = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    openssh.authorizedKeys.keys = [ 
      "ssh-ed25519 AAAAC3..." # Replace with your SSH public key
    ];
    initialPassword = "changeme";
  };
  
  # Enable SSH
  services.openssh = {
    enable = true;
    settings.PermitRootLogin = "no";
    settings.PasswordAuthentication = false;
  };
  
  # AWS specific packages
  environment.systemPackages = with pkgs; [
    aws-cli-v2
    ec2-instance-connect
  ];
}

2. Provision an EC2 instance

# Create an EC2 instance with Amazon Linux 2 (minimum req)
aws ec2 run-instances \
  --image-id ami-0c55b159cbfafe1f0 \
  --instance-type t2.micro \
  --key-name your-key-name \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=nixos-instance}]'

3. Deploy NixOS with nix-anywhere

# Get your instance IP
INSTANCE_IP=$(aws ec2 describe-instances \
  --filters "Name=tag:Name,Values=nixos-instance" \
  --query "Reservations[*].Instances[*].PublicIpAddress" \
  --output text)

# Deploy using nix-anywhere
nix run github:nix-community/nixos-anywhere -- \
  --flake .#aws-instance \
  root@$INSTANCE_IP \
  --build-on-remote \
  --password-prompt

GCP Deployment

1. Create the GCP host configuration file

Create the file hosts/gcp.nix:

{ config, lib, pkgs, ... }:

{
  imports = [
    ../modules/cloud-config.nix
  ];
  
  # System configuration
  system.stateVersion = "23.11";
  
  # GCP-specific configuration
  boot.loader.grub.device = lib.mkForce "/dev/sda";
  fileSystems."/" = {
    device = "/dev/sda1";
    fsType = "ext4";
  };
  
  # Networking
  networking = {
    hostName = "nixos-gcp";
    networkmanager.enable = true;
  };
  
  # User configuration
  users.users.admin = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    openssh.authorizedKeys.keys = [ 
      "ssh-ed25519 AAAAC3..." # Replace with your SSH public key
    ];
    initialPassword = "changeme";
  };
  
  # Enable SSH
  services.openssh = {
    enable = true;
    settings.PermitRootLogin = "no";
    settings.PasswordAuthentication = false;
  };
  
  # GCP specific packages
  environment.systemPackages = with pkgs; [
    google-cloud-sdk
  ];
}

2. Provision a GCP Instance

# Create a VM instance with Debian
gcloud compute instances create nixos-instance \
  --image-family=debian-11 \
  --image-project=debian-cloud \
  --machine-type=e2-medium \
  --zone=us-central1-a \
  --metadata="ssh-keys=admin:$(cat ~/.ssh/id_ed25519.pub)"

3. Deploy NixOS with nix-anywhere

# Get your instance IP
INSTANCE_IP=$(gcloud compute instances describe nixos-instance \
  --zone=us-central1-a \
  --format='get(networkInterfaces[0].accessConfigs[0].natIP)')

# Deploy using nix-anywhere
nix run github:nix-community/nixos-anywhere -- \
  --flake .#gcp-instance \
  root@$INSTANCE_IP \
  --build-on-remote \
  --password-prompt

Azure Deployment

1. Create the Azure host configuration file

Create the file hosts/azure.nix:

{ config, lib, pkgs, ... }:

{
  imports = [
    ../modules/cloud-config.nix
  ];
  
  # System configuration
  system.stateVersion = "23.11";
  
  # Azure-specific configuration
  boot.loader.grub.device = lib.mkForce "/dev/sda";
  fileSystems."/" = {
    device = "/dev/sda1";
    fsType = "ext4";
  };
  
  # Networking
  networking = {
    hostName = "nixos-azure";
    networkmanager.enable = true;
  };
  
  # User configuration
  users.users.admin = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    openssh.authorizedKeys.keys = [ 
      "ssh-ed25519 AAAAC3..." # Replace with your SSH public key
    ];
    initialPassword = "changeme";
  };
  
  # Enable SSH
  services.openssh = {
    enable = true;
    settings.PermitRootLogin = "no";
    settings.PasswordAuthentication = false;
  };
  
  # Azure specific packages
  environment.systemPackages = with pkgs; [
    azure-cli
  ];
}

2. Provision an Azure VM

# Set variables
RESOURCE_GROUP="nixos-rg"
VM_NAME="nixos-vm"
LOCATION="eastus"

# Create resource group
az group create --name $RESOURCE_GROUP --location $LOCATION

# Create VM with Ubuntu (minimum req)
az vm create \
  --resource-group $RESOURCE_GROUP \
  --name $VM_NAME \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

3. Deploy NixOS with nix-anywhere

# Get your instance IP
INSTANCE_IP=$(az vm show -d \
  --resource-group $RESOURCE_GROUP \
  --name $VM_NAME \
  --query publicIps \
  --output tsv)

# Deploy using nix-anywhere
nix run github:nix-community/nixos-anywhere -- \
  --flake .#azure-instance \
  azureuser@$INSTANCE_IP \
  --build-on-remote \
  --password-prompt

Common Modules: cloud-config.nix

Create a shared configuration file in modules/cloud-config.nix:

{ config, lib, pkgs, ... }:

{
  # Common cloud configuration for all instances
  
  # Base packages for all instances
  environment.systemPackages = with pkgs; [
    vim
    git
    wget
    curl
    htop
    tmux
    jq
  ];
  
  # Security settings
  security.sudo.wheelNeedsPassword = false;
  
  # Auto-upgrade settings
  system.autoUpgrade = {
    enable = true;
    allowReboot = true;
    channel = "https://nixos.org/channels/nixos-unstable";
  };
  
  # Timezone and locale settings
  time.timeZone = "UTC";
  i18n.defaultLocale = "en_US.UTF-8";
  
  # Automatically collect garbage
  nix.gc = {
    automatic = true;
    dates = "weekly";
    options = "--delete-older-than 14d";
  };
  
  # Enable flakes and nix-command
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
}

Advanced Usage

Automatic Rollback on Failure

nix-anywhere automatically attempts to roll back if the system can't boot after a deployment. You can configure this behavior with:

nix run github:nix-community/nixos-anywhere -- \
  --flake .#aws-instance \
  root@$INSTANCE_IP \
  --build-on-remote \
  --rollback-reboot-timeout 5m \
  --password-prompt

CI/CD Pipeline Integration

For GitOps-style deployments, you can integrate nix-anywhere in a CI/CD pipeline:

# Example GitHub Action
name: Deploy NixOS to Cloud

on:
  push:
    branches: [ main ]
    paths:
      - 'flake.nix'
      - 'hosts/**'
      - 'modules/**'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Nix
        uses: cachix/install-nix-action@v20
        with:
          extra_nix_config: |
            experimental-features = nix-command flakes
      
      - name: Deploy to AWS
        run: |
          echo "${{ secrets.SSH_KEY }}" > id_ed25519
          chmod 600 id_ed25519
          nix run github:nix-community/nixos-anywhere -- \
            --flake .#aws-instance \
            --ssh-key ./id_ed25519 \
            root@${{ secrets.AWS_INSTANCE_IP }} \
            --build-on-remote

Troubleshooting

  1. SSH Connection Issues

    • Ensure the security groups (AWS), firewall rules (GCP), or network security groups (Azure) allow SSH access on port 22

    • Verify your SSH key is correctly added to the authorized_keys list

  2. Disk Device Name Differences

    • Cloud providers may use different device names. AWS NVMe is usually /dev/nvme0n1, GCP generally uses /dev/sda, Azure may use /dev/sda or /dev/sdb

    • Run lsblk after connecting via SSH to determine the correct device name

  3. Deployment Timeouts

    • Increase the timeout for large deployments:

      nix run github:nix-community/nixos-anywhere -- \
        --flake .#aws-instance \
        --build-on-remote \
        --kexec-timeout 5m \
        root@$INSTANCE_IP

References

PreviousDevEnv with NixNextWSL2

Last updated 4 days ago

💻
NixOS Anywhere GitHub
NixOS Manual
Nix Flakes
AWS EC2 Documentation
GCP Compute Engine Documentation
Azure VMs Documentation