DEV Community

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

Posted on

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 (2)

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-at-windswept 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!