Logo

dev-resources.site

for different kinds of informations.

The Ultimate NixOS Homelab Guide - The Install

Published at
5/3/2024
Categories
nixos
linux
homeserver
Author
jasper-clarke
Categories
3 categories in total
nixos
open
linux
open
homeserver
open
Author
13 person written this
jasper-clarke
open
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!

nixos Article's
30 articles in total
Favicon
How to Set Up Kanata on NixOS: A Step-by-Step Guide
Favicon
NixOS - A Unique Linux Distribution
Favicon
NixOS Full Disk Encryption with USB/SD-Card/Password Unlock
Favicon
Choosing my distro: Ubuntu, NixOS, Elementary OS or Pop!_OS
Favicon
Nix first steps
Favicon
OpenResty on NixOS for an API Gateway
Favicon
Why Consider NixOS for Your Environment?
Favicon
Managing NixOS on DigitalOcean with Colmena
Favicon
I Changed My Mind - NixOS is NOT the Best Linux
Favicon
Backup GitHub Repositories with gidek
Favicon
Packing Custom Fonts for NixOS
Favicon
Managing NixOS Secrets via SOPS, sops-nix and opsops
Favicon
Running NixOS Guests on QEMU
Favicon
Archiving PostgreSQL Backups on NixOS
Favicon
The Ultimate NixOS Homelab Guide - Flakes, Modules and Fail2Ban w/ Cloudflare
Favicon
The radical concept of NixOS and why I love it!
Favicon
The Ultimate NixOS Homelab Guide - The Install
Favicon
The One Man Stack
Favicon
I use NixOS for my home-server, and you should too!
Favicon
This distro finally fixes Linux
Favicon
Running a Terraria Dedicated Server on NixOS
Favicon
NixOS on Raspberry Pi 4 with Encrypted Filesystem
Favicon
Day 7: Uninstalling NixOS from my Macbook Pro
Favicon
Nixpkgs: A community-maintained package lock file for all of Unix
Favicon
Writing a (Nix-Friendly) Hello World Web App in Haskell with Scotty
Favicon
Kick the Mix-ups to the Curb: Nix the Tricks and Juggle Go Projects like a Pro
Favicon
Day 6: Making rEFInd Look Good
Favicon
Day 5: NixOS & Git - version control for config.nix
Favicon
Day 4: (Re)Installing NixOS on my Macbook Pro
Favicon
Day 3: Fighting with network interfaces

Featured ones: