Back to articles
DevOps & Systems

How to Configure NixOS Flakes to Clone 100 Identical VPS Servers in 60 Seconds

Discover how to use NixOS flakes to deploy 100 identical VPS servers in 60 seconds, ensuring stable, controllable, and scalable development and production environments. This...

DPTCloud Engineering5 min read
How to Configure NixOS Flakes to Clone 100 Identical VPS Servers in 60 Seconds

Image credit: aitoff · Pixabay Content License

Discover how to use NixOS flakes to deploy 100 identical VPS servers in 60 seconds, ensuring stable, controllable, and scalable development and production environments. This detailed guide provides a sample configuration, execution commands, and result verification to perfectly replicate your NixOS environment across multiple servers, suitable for solutions like NVMe Cloud VPS for Docker, Nginx & Databases or High-Performance Dedicated Servers for Production Clusters.

Overview of NixOS Flakes and Benefits for VPS Management

NixOS is a Linux distribution based on Nix, known for its declarative configuration management and complete reproducibility. Flakes are a new feature in NixOS that allow you to define your entire system environment (servers, applications, services) as source code, making it easy to share and deploy. With flakes, you can create a standardized configuration and then clone it across hundreds of VPS servers in seconds by copying the code and running a simple command. This is especially useful in DevOps environments where you need to quickly deploy identical servers to run applications, services, or Docker clusters.

Prerequisites and Pre-Configuration Setup

To get started, you need the following:

  • A NixOS server as a management host (can be a VPS or personal computer).
  • NixOS installed and updated to a version that supports flakes (at least NixOS 22.11).
  • Public SSH keys added to the target VPS servers.
  • IP addresses and login credentials (username, password, or SSH key) for each VPS.

Note: There is no publicly available documentation that provides a detailed guide for cloning 100 identical VPS servers in 60 seconds, but based on knowledge of NixOS flakes and automation tools like Ansible, this process is feasible. Guides on bulk VPS management with Ansible, such as the article Fleet Management and Bulk VPS Configuration Automation with Ansible, provide a foundation for understanding large-scale deployment.

Basic NixOS Flakes Configuration

First, create a project directory and initialize a flakes. In this directory, create a `flake.nix` file with the following sample content:

{
  description = "Flake configuration for uniform VPS setup";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
    nixos-configs.url = "github:your-username/nixos-configs";
  };
  outputs = { self, nixpkgs, nixos-configs }:
  {
    nixosConfigurations = {
      vps-server = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          nixos-configs.modules.default
          {
            # Basic configuration
            networking.hostName = "vps-server";
            networking.useDHCP = false;
            networking.interfaces.eth0.ipv4.addresses = [ { address = "192.168.1.100"; prefixLength = 24; } ];
            services.openssh.enable = true;
            security.sudo.wheelEnables = [ "all" ];
            services.httpd.enable = true;
            services.httpd.adminAddr = "[email protected]";
            services.httpd.virtualHosts."example.com" = { ... };
          }
        ];
      };
    };
  };
}

Replace `your-username/nixos-configs` with the path to your repository. In the `modules/` directory, create a `default.nix` file to define your configuration modules.

Deployment and Cloning to 100 VPS Servers

To deploy the configuration to multiple servers, use the `nixos-rebuild switch` command on each server. However, to clone 100 servers in 60 seconds, you need an automation script. Below is a simple Bash example:

#!/bin/bash
# List of 100 VPS IPs
VPS_LIST=("192.168.1.101" "192.168.1.102" "192.168.1.103" ... "192.168.1.200")

# SSH configuration
SSH_USER="root"
SSH_KEY=~/.ssh/id_rsa

# Run command on each server
for IP in "${VPS_LIST[@]}"; do
  echo "Deploying to $IP"
  ssh -i "$SSH_KEY" "$SSH_USER@$IP" "nixos-rebuild switch --flake /path/to/your/flake"
done

Run this script on the management server to send the `nixos-rebuild switch` command to each server. With fast network connections and simple configurations, this can be completed in 60 seconds. Note: This requires passwordless SSH to be configured on all servers.

Verification and Troubleshooting

After deployment, verify the results by:

  • Logging into each server and running `nixos-version` to confirm the version.
  • Running `systemctl status httpd` to check if the web service is running.
  • Accessing the website via the IP address to ensure the web configuration works.

If errors occur, check the logs on the server using `journalctl -u nixos-rebuild` and verify SSH connectivity. If the server doesn't receive the command, check the firewall and SSH policies.

References

For more in-depth information on NixOS flakes, refer to the official documentation at https://nixos.org/manual/nixos/stable/#chap-flakes. Additionally, the article on bulk VPS management with Ansible Fleet Management and Bulk VPS Configuration Automation with Ansible provides a foundation for understanding large-scale deployment.

Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478