11 votes

Tips to use NixOS on a server?

I see some people using NixOs on their servers. I would like to try it out to self host some services and learn about NixOs.

I use hetzner and they have an NixOs iso available so I can just use that to install NixOs. But how do people manage remote instances of NixOs? They would just use ansible or something like it, to run nix on the host, or is there a better way?

Thanks

5 comments

  1. Wulfsta
    (edited )
    Link
    I have a VPS that I run NixOS on, I'll share my configs in a few hours. Edit: still plan on doing this, just got busy and didn't have the chance to. Edit 2: See this topic.

    I have a VPS that I run NixOS on, I'll share my configs in a few hours.

    Edit: still plan on doing this, just got busy and didn't have the chance to.

    Edit 2: See this topic.

    5 votes
  2. [2]
    spit-evil-olive-tips
    Link
    If you're starting out with NixOS, just SSH in (or use the VNC console, if you lock yourself out of SSH), hand-edit your configuration.nix, run nixos-rebuild test and so on. As you get more into...

    If you're starting out with NixOS, just SSH in (or use the VNC console, if you lock yourself out of SSH), hand-edit your configuration.nix, run nixos-rebuild test and so on.

    As you get more into NixOS, there's two things, you may wish to automate one or both of them - management of your /etc/nixos/ directory, as well as automatically running nixos-rebuild. I do the former using a Git repo that I replicate using Syncthing, and do the latter entirely manually. All my NixOS boxes (desktops, laptops, a home server, and a cloud server) are "pets" and not "cattle", so I haven't had the desire to automatically upgrade them or apply configuration changes.

    If you run into any issues with Hetzner, I run my NixOS server on Vultr and have been happy with it. You can also easily run a VM locally using VirtualBox etc. That wouldn't get you closer to your goal of self-hosting services, but if you run into any issues during installation, it may be helpful to troubleshoot if they're Hetzner-specific or not.

    If it helps get you started, here's a copy-paste of my installation notes for disk partitioning that creates an encrypted & compressed root-on-ZFS. You can certainly use a less fun filesystem like ext4 or xfs, too. Or, if you don't want the encryption, leave off the encryption/keyformat/keylocation options.

    These notes assume UEFI since all my bare-metal machines use it. I assume Hetzner, like Vultr, does MBR boot for their VMs instead of UEFI, in which case you'll want mklabel msdos instead and skip set 1 esp on.

    parted /dev/sdx
    mklabel gpt
    mkpart primary 0% 256mb
    mkpart primary 256mb 100%
    set 1 esp on
    
    mkfs.fat /dev/sdx1
    
    zpool create -f
      -o ashift=12
      -O mountpoint=none
      -O relatime=on
      -O normalization=formD
      -O encryption=aes-256-gcm
      -O keyformat=passphrase
      -O keylocation=prompt
      -O acltype=posixacl
      -O xattr=sa
      -O compression=lz4
      -R /mnt tank /dev/sdx2
    
    zfs create -o mountpoint=legacy tank/root
    
    mount -t zfs tank/root /mnt
    
    for x in home nix var tmp; do zfs create -o mountpoint=legacy tank/$x; mkdir /mnt/$x; mount -t zfs tank/$x /mnt/$x; done
    
    mkdir /mnt/boot
    mount /dev/sdx1 /mnt/boot
    
    nixos-generate-config --root /mnt
    

    And if you haven't found it already, bookmark The Big Options Page.

    4 votes
    1. simao
      Link Parent
      Think I will skip the zfs for now, but thanks for the tips that was exactly what I was looking for!

      Think I will skip the zfs for now, but thanks for the tips that was exactly what I was looking for!

      1 vote
  3. DataWraith
    Link
    I haven't experimented with NixOS on the server yet, but there's a dedicated tool, NixOps, for deploying nix packages to (among other things) cloud servers.

    I haven't experimented with NixOS on the server yet, but there's a dedicated tool, NixOps, for deploying nix packages to (among other things) cloud servers.

    3 votes