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:
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:
- Check
lsblk
for your USB flash drive, for example mine will be/dev/sdb
- 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
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:
- Get Rufus
- 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
systemctl start wpa_supplicant
add_network
set_network 0 ssid "myhomenetwork"
set_network 0 psk "mypassword"
set_network 0 key_mgmt WPA-PSK
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=]
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.
parted /dev/sda -- mklabel gpt
parted /dev/sda -- mkpart root ext4 512MB -8GB
parted /dev/sda -- mkpart swap linux-swap -8GB 100%
parted /dev/sda -- mkpart ESP fat32 1MB 512MB
parted /dev/sda -- set 3 esp on
Now let's format these partitions.
mkfs.ext4 -L nixos /dev/sda1
mkswap -L swap /dev/sda2
mkfs.fat -F 32 -n boot /dev/sda3
And now finish it off by mounting the correct volumes.
mount /dev/disk/by-label/nixos /mnt
mkdir -p /mnt/boot
mount /dev/disk/by-label/boot /mnt/boot
swapon /dev/sda2
Configuration
Now that all our partitions are setup let's do our initial base configuration.
nixos-generate-config --root /mnt
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";
}
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.
sudo nmtui
- Select
Activate a connection
- Select your network name and enter the password.
- 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)
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.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!
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.
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).
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?
Hope this doesn't sound critical, really appreciated the post.
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
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.