Articles > Deploying NixOS to Digital Ocean

Deploying NixOS to Digital Ocean

14.04.2024 by Matic Utsumi Gačar

NixOS is not a currently supported Linux distribution on Digital Ocean, but that won't stop us from deploying it there.

Introduction

Digital Ocean is a popular cloud hosting provider for Linux servers, databases and much more. When it comes to choosing a Linux distribution for your server, there are many things to consider such as package format, update/upgrade cycles, maintenance overhead, etc.

NixOS is not just a great disto for desktop use, but has lots of features that make it a good choice for servers as well, such as declarative configuration of the entire system, reproducability and reliability. I personally started using it for production servers a about year ago and never looked back, giving it a try is highly recommended!

Spinning up a NixOS droplet takes only around 5 minutes and is a great way to try it out without any commitment or time/cost investment.

Creating a droplet

Droplets are basically virtual Linux servers that we can SSH into and manage ourselves. Let's create a new one and make it run NixOS.

Select Manage > Droplets from the navigation sidebar and click the big Create Droplet button. As for the droplet region, simply select one close to you. The next step is selecting which Linux distribution we want, however as you might have noticed, NixOS is currently not officially supported.

NixOS is currently not available as a deploy option.

Thankfully, there's a cool project called NixOS Infect that allows us to convert an existing live Linux install to a NixOS system on the fly. I recommend going to the project's Github page and checking which image is confirmed to be working from the table.

elitak/nixos-infectInstall nixos over the existing OS in a DigitalOcean droplet

At the time of writing, Ubuntu 22.04 x64 seemed to be working and was available on DO, so that's what I selected. I don't think the distro matters in this case, since it will get converted to NixOS anyways.

DistributionNameStatusTest date
............
Ubuntu20.04 x64success2022-03-23
Ubuntu22.04 x64success2023-06-05
Ubuntu22.10 x64failure2023-06-05
Ubuntu23.10 x64failure2023-11-16
............
Select a tested distro from the list.

After selecting the image, I'll select the 6$/mo droplet and upload my SSH public key to login with ease. If you're not familiar with SSH keys, I recommend reading up on them or using the password login.

Click Advanced Options > Add Initialization scripts (free) and paste in the code from the NixOS infect documentation:

#cloud-config

runcmd:
  - curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | PROVIDER=digitalocean NIX_CHANNEL=nixos-23.05 bash 2>&1 | tee /tmp/infect.log

The above is an init script that will run NixOS infect as soon as the droplet is initialized. Finally, it's time to hit that big Create Droplet button. After a few seconds you should see the droplet running and ready on the dashboard.

Control panel after spinning up the droplet.

Configuring the system

Once the droplet is created, click on the IPv4 field to copy the address to the system clipboard, open a terminal and connect to the droplet. After connecting, ensure it worked by checking the system info.

[shiro@pc:~]# ssh [email protected]
[root@droplet:~]# uname -a
Linux droplet 6.1.69 #1-NixOS SMP PREEMPT_DYNAMIC Wed Dec 20 16:00:29 UTC 2023 x86_64 GNU/Linux

If it says NixOS instead of Ubuntu like the above, you are now running a NixOS system!
Let's try adding some packages by rebuilding the system, since there's currently no installed editor, we'll drop into a nix-shell.

[root@droplet:~]# cd /etc/nixos/
[root@droplet:/etc/nixos]# nix-shell -p neovim
[nix-shell:/etc/nixos]# nvim configuration.nix
/etc/nixos/configuration.nix
{ pkgs, ... }: { imports = [ ./hardware-configuration.nix ./networking.nix # generated at runtime by nixos-infect ]; environment.systemPackages = with pkgs; [ neovim cowsay ]; # ... rest of the config file }

After saving the file and closing the editor, all that's left to do is rebuilding the system and switching to the new version that includes the new packages we specified. After switching, the packages should be available as globally installed system packages.

[nix-shell:/etc/nixos]# exit
[root@droplet:~]# nixos-rebuild switch
[root@droplet:~]# echo "NixOS in the cloud!" | cowsay
 _____________________
< NixOS in the cloud! >
 ---------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Congratulations, now you can further customize you system and levarage the full power of NixOS in the cloud.

Check the code on Github