Deploying NixOS on Cloud VPS: Immutable Server Configuration with a Single Configuration File
A guide to deploying NixOS on a Cloud VPS using immutable server configuration with a single configuration file, ensuring consistency, easy maintenance, and fast recovery.
In the modern world of virtual servers and cloud services, deploying an operating system consistently, easily maintainable, and quickly recoverable is essential. NixOS – an operating system based on the Nix package manager – offers a unique approach: immutable server configuration. In this guide, we will explore how to deploy NixOS on a Cloud VPS (a high-RAM virtual private server) and set up the entire system using only a single configuration file.
Introduction to NixOS and Immutable Configuration
NixOS is not just an operating system; it's a powerful configuration management system. Instead of modifying the system step-by-step like traditional operating systems (e.g., installing packages, editing configuration files), NixOS uses a configuration.nix file to describe the entire system state. When you apply the configuration, NixOS creates a completely new environment, ensuring the system is always in the desired state – this is the principle of immutable configuration.
The main advantages of this method include:
- Consistency: All servers are configured identically.
- Easy maintenance: Only edit the configuration file, without worrying about the current system state.
- Fast recovery: Restore the previous system state by reapplying the configuration file.
- Reproducibility: Easily create new servers with identical configurations.
Preparation and Deploying NixOS on Cloud VPS
To deploy NixOS on a Cloud VPS, you will need:
- A Cloud VPS account (you can use services like High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways).
- A working computer (a server or workstation).
- SSH access to the VPS.
The deployment process includes three main steps:
- Initialize the NixOS system on the VPS.
- Create the
configuration.nixfile. - Apply the configuration and reboot the system.
Immutable Server Configuration with a Single File
The configuration.nix file is the center of the entire system. Below is a basic example:
{ config, pkgs, ... }:
{
imports = [
./hardware-configuration.nix
];
boot.loader.grub.enable = true;
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/sda";
networking.hostName = "nixos-server";
networking.useDHCP = false;
networking.interfaces.eth0.addresses = [ { address = "192.168.1.100"; prefixLength = 24; } ];
services.openssh.enable = true;
services.httpd.enable = true;
services.httpd.adminAddr = "[email protected]";
services.httpd.documentRoot = pkgs.stdenv.mkDerivation {
name = "webroot";
src = ./www;
buildCommand = ''
mkdir -p $out
cp -r * $out
'';
};
users.users.admin = {
isNormalUser = true;
extraGroups = [ "wheel" ];
shell = pkgs.bash;
};
environment.systemPackages = with pkgs; [
vim
curl
git
];
}
In this example, we configure:
- The hostname as
nixos-server. - Static IP network configuration.
- Enable SSH for remote access.
- Run a web service (HTTP) with content from the
wwwdirectory. - Add an admin user and install basic tools like
vim,curl, andgit.
Verification and Troubleshooting
After applying the configuration, you can verify the system using the following commands:
nixos-rebuild switch: Apply the new configuration.nixos-rebuild test: Test the configuration without changing the system.nixos-rebuild switch --flake .#nixos-config: Use flake to manage configuration (if applicable).
If issues arise, you can:
- Revert to the previous state using
nixos-rebuild switch --rollback. - Check system logs with
journalctlorcat /var/log/nixos.log. - Refer to the official NixOS documentation at https://nixos.org/manual/nixos/stable/.
References
Related infrastructure & engineering guides: Building an Automated AI Code Reviewer on GitLab CI/CD and a Dedicated VPS: Cost-Optimized Solution for Businesses
High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-4E
10% VAT included
- CPU: 4 vCPU
- RAM: 8 GB
- SSD: 50 GB NVMe
