Not Just Paranoid

Another site by Pete Maynard

NixOS 00: Complete NixOS n00b

This post explain how to get NixOS up and running without getting overloaded with too many new terms.

Highlighted text is additional information that can be ignored if you only want to play with NixOS.

This post assumes you can create a virtual machine.

NixOS is a Linux distribution with a fancy package manager. You manage the whole system by editing a single configuration file. The package manager takes care of installing and configuring each package and service on your system, based on your single file.

This is like user dotfiles (1,2) but for your whole system.

Or

Kickstart but for everything and way more features.

Installation

  1. Download the ISO and the checksum. Then verify that its downloaded correctly.
wget https://channels.nixos.org/nixos-19.09/latest-nixos-graphical-x86_64-linux.iso{,.sha256}
sha256sum -c latest-nixos-graphical-x86_64-linux.iso.sha256
  1. Boot up your VM with the ISO.
  2. You’ll be dropped into a shell.

Configure the Disks

  1. These commands create an 8GB swap partition with the remaining space used for the root partition.
sudo parted /dev/sda -- mklabel msdos
sudo parted /dev/sda -- mkpart primary 1MiB -8GiB
sudo parted /dev/sda -- mkpart primary linux-swap -8GiB 100%

Above commands are for BIOS not UEFI. Make sure your VM matches.

  1. Format Filesystem.
mkfs.ext4 -L nixos /dev/sda1
mkswap -L swap /dev/sda2

Base Install

  1. Mount the root partition and enable the swap.
mount /dev/disk/by-label/nixos /mnt
swapon /dev/sda2
  1. Generate the initial configuration file. The heart of your system. This will create two files /mnt/etc/nixos/configuration.nix and /mnt/etc/nixos/hardware-configuration.nix.
nixos-generate-config --root /mnt
  1. To have a system with DHCP, SSH, and an admin user (freethink). Your configuration.nix should look like this:
{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];
 
  # Use the GRUB 2 boot loader. (BIOS) 
  boot.loader.grub.enable = true;
  boot.loader.grub.version = 2;
  boot.loader.grub.device = "/dev/sda"; 

  networking.hostName = "nixos"; 
  networking.interfaces.ens3.useDHCP = true;

  # Set your time zone.
  time.timeZone = "Europe/Dublin";

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
     wget vim htop  
  ];

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;
  services.openssh.permitRootLogin = "yes";

  # Open ports in the firewall.
  networking.firewall.allowedTCPPorts = [ 22 ];

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.freethink = {
     isNormalUser = true;
     extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
   };

  system.stateVersion = "19.09";
}
  1. Install this to the VM. Once completed it will ask for a root password. Then you’ll be done. Enter reboot and remove the ISO.
nixos-install
...
reboot

[Optional] Use Latest Unstable Packages

Use the bleeding edge unstable versions.

nix-channel --add https://nixos.org/channels/nixos-unstable nixos
nixos-rebuild switch --upgrade

Note, you might be tempted to edit configuration.nix’s system.stateVersion = "19.09". Don’t do that, it won’t change your channel. This option is used to prevent major changes in packages from breaking your system.

Done

Once rebooted you will be able to ssh into the machine using the root account and password.

ssh root@ip.address 

Now you have a fresh install of NixOS to play with.

This might become a blog series. Come back soon.

Further Information and References

10 Apr 2020 | Tags ( guide nixos )

Website Last Updated on 13 Oct 2020 (CC BY-SA 4.0)

This site uses JQuery and nanogallery2 hosted by jsdelivr.net
for the Flickr photo feed and GoatCounter for user insights.