Running your NixOS desktop in the cloud

Fancy playing a bit with NixOS? You can download a VirtualBox image from the NixOS download page which allow you to run in a virtual machine on your local machine. However, you can also try it out on Amazon EC2. In this post, I'll show you how to create an EC2 instance, that we can use to login graphically, basically a desktop machine in the cloud! With Amazon EC2 you can pay per hour, so it won't cost you much, you can pause the machine, or even give it extra resources when you need.

Amazon EC2 instances are created based on Amazon Machine Image (AMI), which hold the base installation of the OS you want to use, in this case NixOS.

Here is the current list of the available AMIs, taken from the NixOps repository. NixOps is a tool to support multi-machine NixOS deployments. Using NixOps you can provision whole networks of NixOS machines, running on a cloud provider such as Amazon EC2. The AWS Console also allows searching all public AMIs.

  "eu-west-1".ebs = "ami-f703e180";
  "eu-west-1".s3  = "ami-4900e23e";
  "eu-west-1".hvm = "ami-8b00e2fc";
  "us-east-1".ebs = "ami-5bc39832";
  "us-east-1".s3  = "ami-21c09b48";
  "us-east-1".hvm = "ami-d1c09bb8";
  "us-west-1".ebs = "ami-80f9cfc5";
  "us-west-1".s3  = "ami-6ee6d02b";
  "us-west-1".hvm = "ami-04e6d041";
  "us-west-2".ebs = "ami-7429b144";
  "us-west-2".s3  = "ami-482cb478";
  "us-west-2".hvm = "ami-9a2cb4aa";

Let's create a small machine, instance type m1.small, in the us-east-1 region, with persistent disks (Amazon calls this Elastic Block Storage, EBS), We need ami-5bc39832 for this. Amazon explains very well how to create new instances using the AWS Console, so I'll skip explaining basic EC2 usage. For more information on the different EC2 instance types look here. Another great site to compare costs can be found here

The NixOS AMIs are shared with all users. If you want to know how we build the image, look here.

Once your EC2 instance is in running state, you can SSH into it:

$ ssh root@<ip-of-your-instance>

To make sure you get to be on the latest and greatest stable branch of NixOS, run the following command:

$ nix-channel --update 

In NixOS you can define modules to share pieces of configuration with others. There is a module available in Nix that contains the configuration for running a terminal server, using x11vnc. Edit /etc/nixos/configuration.nix so that it contains:

  imports = [
    # Set all Amazon EC2 related configuration

    # Make our instance into a terminal server

  # Use xfce as desktop manager, and make it default
  services.xserver.desktopManager.xfce.enable = true;
  services.xserver.desktopManager.default = "xfce";

This is the configuration of your new machine. Look at the NixOS manual for other possible configuration options and modules. To activate this configuration, run:

$ nixos-rebuild switch

This will build all dependencies needed for this configuration, including all packages, libraries, kernels and configuration files. It will use the binary cache, to look for pre-built packages, saving you the time of building everything from scratch (which you could do, if you feel like waiting a lot).

Once nixos-rebuild switch has finished and mentioned it activated the configuration successfully, you have a working terminal server in the cloud. We'll need an account to login to:

$ useradd -m guest
$ passwd guest

Try your favorite VNC client (I use TigerVNC), and connect to <ip> port 5900 and use the user and password you have just created.

Note: if you can't connect to the instance, you will need to open up port 22 (SSH) and 5900 (VNC) in the EC2 security group you applied to your instance.

The obligatory screenshot:

Desktop in the cloud