NixOS in the Cloud

For those wanting to run NixOS in the cloud, I figured out how to install it on UpCloud

Note: UpCloud doesn't natively support NixOS so we have to convert another OS into NixOS. In this example we'll use Debian as our starting point.

1. Create an account on UpCloud

2. Go to https://hub.upcloud.com/deploy and choose your data center location, plan (IMPORTANT: NixOS needs at least 4 GB of memory on UpCloud), storage size, and select Debian for the Operating system:


3. Add your SSH public key


4. Set the server name to "nixos" or something descriptive since UpCloud will still show the Debian logo in their UI. Then click "Deploy"


5. Once the compute instance is ready (top-left circle turns green), login via SSH using the provided IPv4 address: ssh -i id_ed25519 root@<IP address>

6. Since our goal is NixOS, we're going to immediately boot into a Live ISO in memory:

curl -sL https://github.com/nix-community/nixos-images/releases/latest/download/nixos-kexec-installer-noninteractive-x86_64-linux.tar.gz | tar -xzf- -C /root && /root/kexec/run

7. This will reboot our machine into a NixOS Live ISO running in memory. Wait 30 seconds then close your terminal and reconnect via SSH: ssh -i id_ed25519 root@<IP address>


8. With NixOS running in memory, we can wipe the hard drive and create a new partition:

cat > /tmp/disk-config.nix <<EOF
{ lib, ... }:
{
  disko.devices = {
    disk.disk1 = {
      device = lib.mkDefault "/dev/vda";
      content = {
        type = "table";
        format = "msdos";
        partitions = [
          {
            part-type = "primary";
            fs-type = "btrfs";
            name = "root";
            bootable = true;
            content = {
              type = "filesystem";
              format = "btrfs";
              mountpoint = "/";
              mountOptions = [ "compress=zstd" ];
            };
          }
        ];
      };
    };
  };
}
EOF

nix --experimental-features "nix-command flakes" run github:nix-community/disko/latest -- --mode destroy,format,mount /tmp/disk-config.nix --yes-wipe-all-disks

9. Now run nixos-generate-config --root /mnt

10. Next, configure NixOS (IMPORTANT: replace "ssh-ed25519 ..." with your own SSH key):

cat > /mnt/etc/nixos/configuration.nix <<EOF
{ config, lib, pkgs, ... }:

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

  boot.loader.grub = {
    enable = true;
    device = "/dev/vda";
  };

  networking.hostName = "nixos";

  networking.networkmanager.enable = true;

  time.timeZone = "America/Los_Angeles";

  i18n.defaultLocale = "en_US.UTF-8";

  environment.systemPackages = with pkgs; [
    openssh
    nano
    wget
  ];

  services.openssh = {
    enable = true;
    settings.PasswordAuthentication = false;
    settings.KbdInteractiveAuthentication = false;
    settings.PermitRootLogin = "yes";
  };

  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 ..."
  ];

  networking.firewall.enable = false;

  system.stateVersion = "24.11";
}
EOF

11. Update nixpkgs:

nix-channel --add https://nixos.org/channels/nixpkgs-unstable
nix-channel --update

12. Install NixOS: nixos-install

13. Set the root password when prompted.

14. Then type reboot

15. Wait a minute, then login via SSH: ssh -i id_ed25519 root@<IP address> (note: you may get a warning about a host key verification failure - this is normal, follow the instructions to remove the old entry then repeat your SSH login attempt)

16. As a last step, you should make sure NixOS' package manager, nix, is up to date by running this in your ssh terminal: nix-env --install --file '<nixpkgs>' --attr nix cacert -I nixpkgs=channel:nixpkgs-unstable && systemctl daemon-reload && systemctl restart nix-daemon

Enjoy!

Comments

Popular Posts