Back to articles
Security & Anti-DDoS

Guide to Create Disposable NixOS VPS with Auto-Destroy After 30-Second Base Snapshot Restore for Testing & Security

Detailed guide to create a Disposable VPS using NixOS with auto-restore from base snapshot and auto-destroy after 30 seconds, ideal for testing and security environments.

DPTCloud Engineering4 min read
Guide to Create Disposable NixOS VPS with Auto-Destroy After 30-Second Base Snapshot Restore for Testing & Security

Image credit: StockSnap · Pixabay Content License

Create Disposable VPS with NixOS: Restore Snapshot and Auto-Destroy in 30 Seconds

In development and security testing environments, having a virtual private server (VPS) that can be created, used briefly, and then automatically deleted after completing a task is extremely useful. With NixOS, you can build a Disposable VPS that automatically restores from a base snapshot and auto-destroys after 30 seconds following task completion. This guide will walk you through setting up this environment on a VPS platform at DPTCloud, ensuring high security and efficiency.

Introduction to Disposable VPS with NixOS

A Disposable VPS is a temporary virtual server that exists only for a short duration, is set up from a standard state (base snapshot), performs a task (e.g., testing, running code, analysis), and then automatically reverts to its initial state or is completely destroyed. With NixOS, a declarative operating system that supports complete rollback, creating such a disposable environment is fully feasible.

We will use NixOS to build a VPS that can:

  • Restore from a base snapshot after each boot.
  • Automatically restore configuration and data after task completion.
  • Auto-destroy after 30 seconds if no activity occurs (or after task completion).
  • Ensure high security by not storing sensitive data.

Setting Up Disposable VPS with NixOS

To set this up, you need a VPS running NixOS. If you don't have one, install NixOS from ISO or use a pre-installed version at DPTCloud.

1. Create Base Snapshot

First, create a base snapshot of your NixOS system. This will be the standard state that the VPS restores to after each boot.

Run the following command on your NixOS server:

sudo nixos-rebuild switch --flake .#vps --use-remote-sudo
sudo nix-collect-garbage -d
sudo nixos-rebuild switch --flake .#vps --use-remote-sudo

Alternatively, define the base configuration using NixOS modules. For example:

configuration.nix
{
  imports = [ ./hardware-configuration.nix ];

  boot.kernelPackages = pkgs.linuxPackages_latest;

  services.openssh.enable = true;
  services.httpd.enable = false;
  services.sshd.enable = true;

  # Do not store sensitive data
  users.users.root = {
    shell = pkgs.bash;
  };
}

2. Create Auto-Destroy Task After 30 Seconds

We will use systemd to create a service that runs after boot, performs a task, and then auto-destroys after 30 seconds.

Create the file /etc/systemd/system/disposable.service:

[Unit]
Description=Disposable VPS Task Runner
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo 'Running test task...'; sleep 20; echo 'Task completed. Rebooting...'; reboot"
TimeoutStartSec=30
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Set permissions and enable the service:

sudo systemctl enable disposable.service
sudo systemctl start disposable.service

3. Restore Base Snapshot After Each Boot

To ensure the VPS always reverts to its original state, use NixOS rollback or automatic snapshot restoration.

Create the file /etc/nixos/configuration.nix with the following content:

boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/sda";
boot.kernel.sysctl."kernel.sysrq" = "1";

# Restore base snapshot after each boot
systemd.services.revert-snapshot = {
  enable = true;
  description = "Revert to base snapshot";
  serviceConfig = {
    Type = "oneshot";
    ExecStart = "/bin/bash -c 'nixos-rebuild switch --flake .#base --use-remote-sudo && nix-collect-garbage -d'";
  };
};

Run and Verify

Reboot the VPS to test the auto-restore and auto-destroy functionality:

sudo reboot

After 30 seconds, the VPS will automatically reboot and restore to the base snapshot. No data will be retained.

Security Enhancements

To further strengthen security, combine with the following solutions:

Troubleshooting

  • Snapshot not restoring: Check the /etc/nixos/configuration.nix file and ensure the configuration is correct.
  • Service not running: Check with systemctl status disposable.service.
  • Not auto-destroying: Verify TimeoutStartSec in the service file.

References

Success! You have successfully created a Disposable VPS with NixOS that automatically restores from a base snapshot and auto-destroys after 30 seconds, ideal for testing and security environments.

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