DEV Community

Cover image for The Ultimate NixOS Homelab Guide - The Install
Jasper
Jasper

Posted on • Edited on • Originally published at jasperclarke.com

The Ultimate NixOS Homelab Guide - The Install

Finally after 2 weeks here we are. A full, in-depth, step-by-step, ultimate guide to NixOS for homeservers!

This first post wont be too long but will cover the following:

  1. Installing NixOS
  2. Starting a Basic Config

Well without further a due, lets get this series rolling!

Installation

Just about all of the following is straight from the NixOS Manual so if you need any extra help you can check there or ask a comment below this post and I'll try to help out!

Getting a NixOS flash drive

Ok so let's download the ISO and flash it to a spare 8-16GB USB.
First download the nixos-unstable ISO from this link.

Next for Linux users just do the following:

  1. Check lsblk for your USB flash drive, for example mine will be /dev/sdb
  2. Still in the terminal navigate to the folder where the ISO is and run:
sudo dd bs=4M if=nixos-unstable.iso of=/dev/sdb status=progress oflag=sync
Enter fullscreen mode Exit fullscreen mode

Make sure to change nixos-unstable.iso to the file name of the ISO you just downloaded and change /dev/sdb to your flash drives identifier.

For Windows user:

  1. Get Rufus
  2. Install Rufus, select the ISO and your flash drive and click Start, then run in ISO mode. You don't need to change any other settings.

Ok so now that we have our boot-able flash drive let's plug it into our server and turn it on.
I'm sure for anyone reading this you do know how to do stuff like entering BIOS and booting a flash drive but for those who are not familiar just hold DEL or F12 on your keyboard whilst your server is turning on to bring up the boot menu, this key can be different depending on your motherboard so if your having trouble lookup its brand to find the key to open the "Boot Menu".

From there select your flash drive, then when the NixOS installer boots just press enter on the first option.

Setup and Partitioning

Since we are not using the graphical installer we will have to do this all in the terminal. As a quick note you need to have a monitor and keyboard connected to your server for the first part of the installation, afterwords you can simply connect via SSH.

I won't be explaining the next few steps in detail as otherwise this post's retention rate will cease to exist but if your interested what everything is doing check the NixOS Manual.

First thing do sudo -i to enter root, this way you won't have to prefix every command with sudo.

If you have a Ethernet cable for your server make sure it is plugged in otherwise lets setup the Wifi as you will need internet in the installer.

Networking

  1. systemctl start wpa_supplicant
  2. add_network
  3. set_network 0 ssid "myhomenetwork"
  4. set_network 0 psk "mypassword"
  5. set_network 0 key_mgmt WPA-PSK
  6. enable_network 0

If all went well you should see something along the lines of below showing that the connection was successful.

<3>CTRL-EVENT-CONNECTED - Connection to 32:85:ab:ef:24:5c completed [id=0 id_str=]
Enter fullscreen mode Exit fullscreen mode

Partitioning

For the following commands if you have a M.2 SSD for your main drive you will use /dev/nvme0n1 instead of /dev/sda, if you have multiple M.2 SSDs you should check lsblk to find the one you want to use.

  1. parted /dev/sda -- mklabel gpt
  2. parted /dev/sda -- mkpart root ext4 512MB -8GB
  3. parted /dev/sda -- mkpart swap linux-swap -8GB 100%
  4. parted /dev/sda -- mkpart ESP fat32 1MB 512MB
  5. parted /dev/sda -- set 3 esp on

Now let's format these partitions.

  1. mkfs.ext4 -L nixos /dev/sda1
  2. mkswap -L swap /dev/sda2
  3. mkfs.fat -F 32 -n boot /dev/sda3

And now finish it off by mounting the correct volumes.

  1. mount /dev/disk/by-label/nixos /mnt
  2. mkdir -p /mnt/boot
  3. mount /dev/disk/by-label/boot /mnt/boot
  4. swapon /dev/sda2

Configuration

Now that all our partitions are setup let's do our initial base configuration.

  1. nixos-generate-config --root /mnt
  2. nano /mnt/etc/nixos/configuration.nix

Want to use Vim? Just do the following to install vim on the live installer.

  • nix-env -f '<nixpkgs>' -iA vim

Now that we have our base canvas of our configuration ready to be painted upon let's start with some basics.


{ config, lib, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  boot = {
    # Use the latest linux kernel
    kernelPackages = pkgs.linuxPackages_latest;
    # Grub bootloader stuff, no need to change this.
    loader = {
      efi.canTouchEfiVariables = true;
      grub = {
        enable = true;
        efiSupport = true;
        device = "nodev";
      };
    };
  };

  # Allows us to use closed source packages.
  nixpkgs.config.allowUnfree = true;

  networking = {
    # What should your server be on the network?
    hostName = "homelab";
    # Use network manager, makes life easier
    networkmanager = {
      enable = true;
    };
  };

  # Your timezone here
  time.timeZone = "Australia/Sydney";

  # Change `admin` to whatever username you want.
  users.users.admin = {
    # This makes sure the user isn't root.
    isNormalUser = true;
    extraGroups = [ "wheel" "docker" ]; # Enable ‘sudo’ and the use of docker for the user.
    # Set the home directory, also change this to `/home/your-username`
    home = "/home/admin";
    # Use Zsh
    shell = pkgs.zsh;
  };

  # Enable Zsh, this is just personal preference
  programs = {
    zsh.enable = true;
  };

  environment.systemPackages = with pkgs; [
    vim
  ];

  # Enable the OpenSSH daemon, for SSH...
  services.openssh.enable = true;

  # Backup the system configuration when changed
  system.copySystemConfiguration = true;

  # The current unstable version of NixOS
  system.stateVersion = "24.05";

}
Enter fullscreen mode Exit fullscreen mode

This is the minimum of what you need to get started on NixOS. We won't add anything more to this now as it would just take ages longer to install and we want to get in ASAP!

So once your happy with the configuration just run the following commmand.

  • nixos-install

After a while it will prompt you to set the root password and then voila, you just installed NixOS!

Now just reboot and hop into your new system, don't unplug your keyboard and monitor just yet though, we have a couple more things to do.

Post-Install

Now launched into NixOS login to the root user account since the user you created doesn't have a password yet.
Once logged into root type passwd your-username to set the user password.
Then exit and login to the user account.

For Wifi users you are not done quite yet, follow these steps to login to your network in nmtui.

  1. sudo nmtui
  2. Select Activate a connection
  3. Select your network name and enter the password.
  4. Once connected hit escape a few times and your done!

Congratulations! You have just setup NixOS for your homeserver, in the next post I'll be setting up Nextcloud and organizing our configuration to use modules so stay tuned for the next installment coming next week (hopefully)!

And if you have any suggestions for popular self hosted software you want to see setup on NixOS leave a comment and it might just make it into the series.

Thanks everyone and I'll see you all again soon!

Top comments (5)

Collapse
 
siph profile image
Chris Dawkins

If you wanted to get even more fancy, you could use nixos-generators to build the config into an image and deploy the image directly to the machine. It's very convenient when deploying something simple to a low-power machine (like a webserver to a raspberry pi) because your target doesn't need to do the heavy building.

Collapse
 
jasper-clarke profile image
Jasper

That sounds really interesting, I have heard of something similar before to preload a computer by just booting to a USB and everything automatically installing your NixOS setup. I'll definitely take a look at that thanks!

Collapse
 
nomm profile image
Maddogskullcap

Thank you for the very well explained setup! I really appreciated you showing how to create a bootable usb from the command-line instead of just saying "create a bootable usb", never done that before and now it'll be in my workflow! As well as letting us know you can avoid nano with nix-env(idk why I never though of that before smh). Following, and looking forward to your next nextcloud post.

  • I'd love to see a post about basic server security setup in configuration.nix using ssh-keys, fail2ban, vpn, etc.

Here's some feedback on what a novice(me, i'm novice) might be confused about when running through your described processes:

  • Into partitioning via the command-line. I ran through it all, while getting a strange "information: you may need to update /etc/fstab" message after each command. I looked it up and it seemed like it could just be ignored?

  • I ended up just using the gui installer, because I must have input the password wrong twice when prompted or neglected to declare it in configuration.nix (if I was expected to do that).

    • Using the gui setup doesn't declare it in configuration.nix, but I guess it's a different process altogether and it makes sense for those wanting to share their nix files.

I am curious why you decided not to use the graphical installer? is there a reason you're partitioning manually? Or is it just to avoid having unnecessary services like printing enabled?

  • Since it seems like this post is aimed towards noobs given you described how to get to the boot menu it would nice to include why your installing this way.

Hope this doesn't sound critical, really appreciated the post.

Collapse
 
jasper-clarke profile image
Jasper • Edited

Hey,
Thanks very much and I am so sorry about the delay on the Nextcloud post. I found out that for me personally I can't actually run nextcloud since my network doesn't let me port forward, requiring me to use Cloudflare Tunnels which I don't know how to setup with Nextcloud in Nix.

I have actually setup Vaultwarden + fail2ban on NixOS as well as done security setup so I will make my next post that.

With those fstab stuff as the NixOS manual says you can completely ignore them.
I dont use the GUI installer because to my knowledge it boots Wayland which for me as an Nvidia user, just doesn't work without insane graphical issues. Also I find the graphical calamares installer limiting but thats just me haha.

And yes I wanted to make sure this post would be friendly to even new users of Linux, after all if NixOS is your first Linux distro how good that your starting with the best one XD

I'd love to help out more if I can so if you want flick me a message on Discord to: jasper_at_windswept

Collapse
 
nomm profile image
Maddogskullcap

Sorry about my delay as well, funny(unfortunately) enough your port forwarding issue was the same thing I ran into originally trying my hand at self-hosting using ubuntu...part of the reason I switched to nixos. I wish those tutorials let me know that some ISPs make port forwarding really difficult(xfinity) even if they advertise it. I have a coworker who helped me find a way around it when they were holding my hand through my intial ubuntu server setup, but I forget the details it was awhile ago. I'll definitely bother them again for that solution and DM you to try to implement it into nixos. I also don't want to deal with weird 3rd party proxy stuff. Just saw your new post about to start looking through it ASAP.