Getting Started With NixOS

Getting Started With NixOS Linux

NixOS Series #4: Things To Do After Installing NixOS

What do you do right after installing NixOS? Clueless? We got your back.

After installation, you will notice that NixOS is quite different from general-purpose Linux distributions.

Of course, as one of the advanced Linux distributions, it may not feel right at home to most new users.

If you do not know why you should use NixOS, and trying it out of curiosity, it is vital to know who it is for before proceeding.

While I assume you installed the distro already, if it is your first time, I suggest installing NixOS on a virtual machine.

1. Update packages

Updates would always be there even if you used the latest ISO for the installation. So why not start by updating the packages?

To upgrade packages, first, you will have to check for updates in added channels:

nix-channel --update

And then, use the following command to install those updates (if any):

sudo nixos-rebuild switch --upgrade

That's it! It will take care of the rest.

2. Change hostname in NixOS

If you try the traditional way of changing the hostname (using the hostnamectl command), it will throw the following error:

error changing hostname in nixos

With NixOS, you can change the hostname easily using its main config file, which you can access using the following command:

sudo nano /etc/nixos/configuration.nix

In this config file, look for the following line:

networking.hostName = "nixos";

And change it to:

networking.hostName = "Your_Hostname";

For example, I changed my hostname to itsFOSS:

networking.hostName = "itsFOSS";
change hostname in NixOS

Now, save changes and exit from the nano text editor.

To take effect from the change you made to hostname, execute the following command:

sudo nixos-rebuild switch

And finally, reopen the terminal, and the change in hostname should reflect.

Suggested Read πŸ“–

Vim vs Nano: What Should You Choose?
Vim and Nano are two popular terminal text editors. How are they different? What’s best for you? Let us find out.

3. Setup Flatpak

I know what you might be thinking. The Nix package manager already offers a plethora of packages. So, why do you need Flatpak?

Installing what you need could be a bit time-consuming for first-time users. So, Flatpak should make things convenient for you.

Setting up Flatpak is not the same as you do on Ubuntu.

To setup Flatpak, you will have to make changes to the configuration.nix file, which can be accessed using the following:

sudo nano /etc/nixos/configuration.nix

Go to the end of the line in nano and add the following line before the }:

services.flatpak.enable = true;
setup flatpak on nixos

Save changes by pressing Ctrl + O, hit enter and exit by Ctrl + X.

Next, rebuild and switch to the new config file using the following command:

sudo nixos-rebuild switch

And finally, add the Flathub repository to the system using the following command:

flatpak remote-add --if-not-exists flathub

Want to know what exactly is a Flatpak package? You can refer to our article on it:

What is Flatpak? Everything Important You Need to Know About This Universal Packaging System
What are Flatpak packages? Why is it called a universal packaging system, what problem does it solve and how does it work? Learn about Flatpak.

4. Enable garbage collection

NixOS is known for being immutable, and there is a strong reason why.

Whenever you upgrade a package, the old package won't be removed. Just the symlinks of the old package will be given to the latest version.

And doing that, you will collect unnecessary trash from your system.

But removing every old generation will falsify the purpose of NixOS.

So, in that case, you can configure your system to remove garbage packages weekly.

To do that, first, open the nix configuration file:

sudo nano /etc/nixos/configuration.nix

And add the following line at the end of the config file before }:

# Automatic Garbage Collection
nix.gc = {
                automatic = true;
                dates = "weekly";
                options = "--delete-older-than 7d";
enable automatic garbage collection in NixOS

Save changes and exit from the nano text editor.

To activate the garbage collection, rebuild and switch to the new config file:

sudo nixos-rebuild switch

If you are not sure whether the garbage collector is running fine in the background, you can list active timers using the following command:

systemctl list-timers

And as you can see, the Nix garbage collector is running as expected and shows 5 days left for the next cleanup.

5. Install your favorite software

I mean this is the only reason why we use computers. "To use our favorite software," and if there's none, we make it happen!

The best place to look for packages is the Nix package search which can be accessed using any of your preferred browsers.

  • Search package
  • Select the package
  • Click on nix-env and copy the given command for NixOS
  • Execute that command, and that's it

You can check our NixOS package management guide to get all the details.

Let me give you a quick recap here. For example, here, I want to install Librewolf, so I went with the following:

search packages for nixos

But if you want to install services like SSH or plex, the above method won't work.

For that, you will have to look into NixOS options situated at the top of the page.

So let's say I want to install OpenSSH, so I have to follow the given steps:

  • Go to NixOS options
  • Search the name of the service
  • Get the name of the service and paste it to the configuration.nix by changing its value to true
Search the service for nixos
services.openssh.enable = true
enable openssh on nixos

After adding the line to the config file, rebuild the service:

sudo nixos-rebuild switch

6. Enable auto-update in NixOS (optional)

Some users prefer to have auto-updates enabled, whereas others can update packages at their convenience.

So it is all up to you.

To enable auto-update, first open the configuration.nix file:

sudo nano /etc/nixos/configuration.nix

Next, add the following line at the end of the config file before }:

# Auto system update
system.autoUpgrade = {
      enable = true;
enable auto system update in nixos

Save changes and exit from the nano.

To enable the auto-update, you will have to rebuild and switch to that file using the following:

sudo nixos-rebuild switch

You can also check the NixOS upgrade timer using the following command:

systemctl list-timers
auto upgrade timer in nixos

And as you can see, the nixos-upgrade.service is running in the background as intended!

7. Reduce swapiness

If you are utilizing the swap partition, you may want to reduce the swapiness value.

Swapiness is nothing but the value of how aggressively you want to use the swap partition (or memory), which ranges from 0 to 100.

The lesser the swapiness, the more your system will use the physical memory (RAM), whereas a swap partition is nothing but a bit of part of your storage drive.

Also, storage drives are relatively slower than RAM, so you should reduce the swapiness if possible.

Suggested Read πŸ“–

How Much Swap Should You Use in Linux?
How much should be the swap size? Should the swap be double of the RAM size or should it be half of the RAM size? Do I need swap at all if my system has got several GBs of RAM? Perhaps these are the most common asked questions about choosing

To check the default swapiness of your system, use the following:

cat /proc/sys/vm/swappiness
check swapiness of linux system

And for most Linux distributions, it is set to 60.

I would recommend you lower this value to 10.

To do that, first, open the configuration file using the following command:

sudo nano /etc/nixos/hardware-configuration.nix

And add the following line at the end of the line before }:

boot.kernel.sysctl = { "vm.swappiness" = 10;};
reduce swapiness in nixos

Save changes and exit from the text editor.

Now, rebuild the config and switch to it using the following:

sudo nixos-rebuild switch

And now, you can check the swapiness again and it should reflect the change:

cat /proc/sys/vm/swappiness
reduce swapiness in NixOS

That's it!

Wrapping Up

If you follow these points right after installing NixOS for the first time, you should get a good user experience.

Sure, there can be a few other things depending on your requirements. But, I think the above-mentioned things are the most essential or common things to do.

For the next part of this series, I shall discuss setting up the home manager on NixOS, which should be helpful for a system with multiple users.

πŸ’¬ What do you first do after installing NixOS? Let me know your thoughts.