Skip to content

Cloud Deployment

Guide to deploying this configuration on AWS EC2 and GCP GCE.


📋 Planned - Cloud deployment is designed but not yet fully implemented.

This document describes the planned cloud deployment strategy.


This configuration will support cloud deployment with:

  • AWS EC2 instances
  • GCP GCE instances
  • Cloud-init integration
  • Automated provisioning
  • Remote management

AWS EC2 (nix/modules/cloud/ec2-base.nix - placeholder):

{ config, pkgs, lib, ... }:
{
# EC2-specific configuration
boot.loader.grub.device = "/dev/xvda";
services.cloud-init.enable = true;
networking.firewall.allowedTCPPorts = [ 22 ];
# EC2 metadata service
services.amazon-ssm-agent.enable = true;
}

GCP GCE (nix/modules/cloud/gce-base.nix - placeholder):

{ config, pkgs, lib, ... }:
{
# GCE-specific configuration
boot.loader.grub.device = "/dev/sda";
services.cloud-init.enable = true;
# GCP guest agent
services.google-guest-agent.enable = true;
}

Terminal window
# Build custom AMI with nixos-generators
nix build .#nixosConfigurations.ec2-instance.config.system.build.amazonImage
# Upload to AWS
aws ec2 import-image --disk-containers file://image.json
Terminal window
# Create instance with Terraform
terraform init
terraform apply
# Or with AWS CLI
aws ec2 run-instances \
--image-id ami-xxxxx \
--instance-type t3.medium \
--key-name your-key \
--user-data file://cloud-init.yaml
Terminal window
# Deploy configuration to EC2
nixos-rebuild switch --flake .#ec2-instance \
--target-host ec2-user@instance-ip \
--build-host localhost

Terminal window
# Build GCE image
nix build .#nixosConfigurations.gce-instance.config.system.build.googleComputeImage
# Upload to GCP
gcloud compute images create nixos-image \
--source-uri gs://bucket/image.tar.gz
Terminal window
# Create instance
gcloud compute instances create nixos-vm \
--image nixos-image \
--machine-type n1-standard-2 \
--zone us-central1-a
Terminal window
# Deploy to GCE
nixos-rebuild switch --flake .#gce-instance \
--target-host user@instance-ip \
--build-host localhost

hosts/ec2-web.nix
{ config, pkgs, ... }:
{
imports = [
../nix/modules/common.nix
../nix/modules/linux-base.nix
../nix/modules/cloud/ec2-base.nix
];
# Web server
services.nginx.enable = true;
# Auto-updates
system.autoUpgrade.enable = true;
# Monitoring
services.prometheus.exporters.node.enable = true;
}
cloud-init.yaml
#cloud-config
users:
- name: admin
groups: wheel
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAA...
write_files:
- path: /etc/nixos/flake.nix
content: |
# Nix configuration
runcmd:
- git clone https://github.com/user/Config.git /etc/nixos
- nixos-rebuild switch --flake /etc/nixos#cloud-instance

terranix/aws-ec2.nix
{ config, lib, ... }:
{
resource.aws_instance.nixos = {
ami = "ami-xxxxx"; # NixOS AMI
instance_type = "t3.medium";
user_data = ''
#!/bin/bash
git clone https://github.com/user/Config.git /etc/nixos
nixos-rebuild switch --flake /etc/nixos#ec2-instance
'';
};
}
Terminal window
# Generate Terraform JSON
terranix terranix/ > config.tf.json
# Deploy
terraform init
terraform apply

Terminal window
# Install nixos-generators
nix-shell -p nixos-generators
# Build AWS AMI
nixos-generate -f amazon -c ./configuration.nix
# Build GCP image
nixos-generate -f gce -c ./configuration.nix
# Build Azure image
nixos-generate -f azure -c ./configuration.nix
# Build ISO
nixos-generate -f iso -c ./configuration.nix
# In flake.nix
outputs = { self, nixpkgs, nixos-generators, ... }: {
images = {
aws = nixos-generators.nixosGenerate {
system = "x86_64-linux";
format = "amazon";
modules = [
./nix/modules/common.nix
./nix/modules/cloud/ec2-base.nix
];
};
gcp = nixos-generators.nixosGenerate {
system = "x86_64-linux";
format = "gce";
modules = [
./nix/modules/common.nix
./nix/modules/cloud/gce-base.nix
];
};
};
};

terranix/asg.nix
resource.aws_launch_template.nixos = {
image_id = "ami-xxxxx";
instance_type = "t3.medium";
user_data = base64encode(''
#!/bin/bash
nixos-rebuild switch --flake github:user/Config#ec2-web
'');
};
resource.aws_autoscaling_group.nixos = {
launch_template = {
id = "\${aws_launch_template.nixos.id}";
};
min_size = 2;
max_size = 10;
};

# EC2 with CloudWatch
services.amazon-cloudwatch-agent = {
enable = true;
config = {
logs = {
logs_collected = {
files = {
collect_list = [
{
file_path = "/var/log/syslog";
log_group_name = "/aws/ec2/nixos";
}
];
};
};
};
};
};
# Prometheus exporters
services.prometheus.exporters = {
node.enable = true;
systemd.enable = true;
};
# Open firewall for Prometheus
networking.firewall.allowedTCPPorts = [ 9100 9558 ];

  • nixos-generators integration
  • AWS AMI builds
  • GCP image builds
  • Automated image uploads
  • EC2 deployment tested
  • GCE deployment tested
  • Cloud-init integration
  • Remote rebuild
  • Terranix integration
  • Auto-scaling groups
  • Load balancer support
  • Monitoring/logging
  • High availability
  • Disaster recovery
  • Cost optimization
  • Security hardening

# Hardened configuration
security.sudo.wheelNeedsPassword = true;
services.openssh.settings.PasswordAuthentication = false;
services.openssh.settings.PermitRootLogin = "no";
# Automatic updates
system.autoUpgrade = {
enable = true;
allowReboot = true;
dates = "04:00";
};
# Use spot instances where appropriate
# Implement auto-scaling based on metrics
# Schedule non-prod instances




Status: 📋 Planned - Contribute on GitHub