Installing NixOS on Vultr
2019.04.25 Nix NixOS Linux Infrastructurecontents
- Spin up a server from the web UI
- Install NixOS onto the server's storage
- Create a snapshot
- Deploy new servers with the snapshot
This is a method of installing NixOS on a virtual private server (VPS) provided by the cloud host, Vultr.
NixOS is a build system, package manager, and lazily evaluated functional DSL
for building and deploying a complete Linux operating system declaratively. All
the system's configuration files are kept in the immutable Nix store, meaning
that they are no longer stateful, and can also be provisioned declaratively,
and rolled back with ease. The whole system configuration is kept in one file,
configuration.nix
.
This method installs NixOS, without nixos-infect
or
NixOps, instead using Vultr's provided NixOS
installation ISO, and generating a snapshot which can be stored for free, and
re-used to spin up as many VPS servers as you like. There is some manual set-up
required in order to create the snapshot, but this only has to be done once;
the initial set up can't be automated since the install process used to
generate the snapshot needs to be done using Vultr's KVM console, with SSH
being initially unavailable.
Overall, I find this to be a simple method of installing NixOS onto server in the cloud. There is potential to automate the deployment of new servers using the generated snapshots, which should be possible over SSH, and using the Vultr API, though this isn't covered here.
Spin up a server from the web UI¶
-
Under Server Type, choose the ISO Library tab, and select the NixOS 18.09 ISO image.
-
Under Server Size, choose a size.
I deployed a 10GB SSD VPS server to do this, which is tiny, but the resulting snapshot is then also 10GB, and can then be used to provision any server Vultr provides, with 10 GB or larger SSD storage, by later resizing the file system.
Make sure to select a server with IPv4 networking.
-
Click Deploy Now.
Install NixOS onto the server's storage¶
-
Connect to the server by the KVM console (View Console). There is no need to log in with a user name or password.
-
Curl the script located here, e.g.
curl https://eipi.xyz/vultr.sh | bash
You have to type the curl command in manually since copy and paste doesn't work on the KVM console.
The script does the following:
-
Partition and format the storage, including a 2 GB swap partition to allow building on servers with a small amount of RAM.
-
Install a basic
configuration.nix
, enough to log in to the machine. -
Install NixOS.
After a short time retrieving caches and building it will finish.
The script was originally adapted from Vultr's documentation.
-
Create a snapshot¶
-
This should be done by selecting Settings > Custom ISO and Remove ISO on the web interface, which restarts the server at the same time as unmounting the install ISO.
-
Create the snapshot, under Snapshots, giving the snapshot a label of your choice. This will take a little time.
-
Once the snapshot is complete, you can destroy the server.
Deploy new servers with the snapshot¶
-
Deploy a new server, selecting Snapshot in the Server Type section, and selecting your snapshot.
-
Connect via SSH, using username
root
and passqqcTch4m
. -
Replace
/etc/nixos/configuration.nix
with your own configuration, and run:nixos-rebuild switch
Preferably include a public key or other authentication for a user, so you are able to log in.
-
Resize the file system if the new server's storage is larger than the snapshot, as follows:
#!/bin/sh # resize partition table ( # delete partition 3 echo d echo 3 # recreate partition 3 echo n echo echo echo # write echo w ) | fdisk /dev/vda resize2fs /dev/vda3 echo "Done resising the filesystem, please reboot."
You can copy and paste the above via SSH, or curl from here, e.g:
curl https://eipi.xyz/vultr-resize.sh | bash
-
Reboot and log in to your new NixOS system.