NixOS Workshop

2 the workshop machines

  • usually NixOS runs on bare metal hardware (or in a VM)
  • this workshop runs NixOS inside a systemd container, as it is easy to setup. Goal is to get a first impression of NixOS
  • this is how to setup your own NixOS test machine inside a container:
    • you need a modern Linux (amd64 architecture) with systemd and systemd-nspawn (Package systemd-container)
    • approx. 1.6 GB free storage inside /srv
  • create a directory for the NixOS linux container images
# mkdir -p /srv/container/nixos
  • download and untar the image into the container directory
# curl https://blog.defaultroutes.de/nixos/nixos.tgz | tar -C /srv/container/nixos -xzf -

3 start NixOS

  • launch the NixOS container
# systemd-nspawn -bD /srv/container/nixos
  • Login to NixOS with username root and password nixos

4 creating a normal user

  • open the file /etc/nixos/configuration.nix with the nano text editor (we will install other editors soon)
  • find the part that describes the user accounts (example commented out), and enable the config for a normal user:
# Define a user account. Don't forget to set a password with ‘passwd’.
users.users.jane = {
   isNormalUser = true;
   extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
};
  • save the file
  • activate the new configuration
# nixos-rebuild switch
  • check that the new user exists
# id jane
  • set a password for the new user
passwd jane
  • exit the shell with exit and try to login with the new user
    • test that the new user can become super user with the sudo command

5 fixing the hardware configuration

  • the NixOS was originally installed inside an VirtualBox VM
  • the NixOS hardware autodetection found VirtualBox and automatically created a filesystem mount for the VirtualBox Guest Additions
  • now the NixOS runs inside a Systemd container, and the VirtualBox configuration creates an error message when building a new system configuration (and also during startup):
collision between 
  `/nix/store/n9yllfqp84pwqrpqkk6my9j7hwfrz619-mount.vboxsf/bin/mount.vboxsf' 
  and `/nix/store/bjshbv71j3010pnhnhdxpwdyrj282wkw-VirtualBox-GuestAdditions-6.1.6-5.4.62/bin/mount.vboxsf'
  • open the NixOS hardware configuration in /etc/nixos/hardware-configuration.nix and find the line that configures the Virtual-Guest additions. Remove that line (delete or comment)
    • the file hardware-configuration.nix is included from the configuration.nix file and describes hardware configuration that can differ between machines.
  • rebuild the NixOS system configuration
  • check that the error message has gone

6 install some gobal packages

  • let's install some global packages that will be visible for all users of the system
  • open the file /etc/nixos/configuration.nix and fill in this block almost at the end, but before the last curly brace
environment.systemPackages = with pkgs; [
   tmux
   htop
];
  • save the file, exit the editor and rebuild the NixOS system nixos-rebuild switch
  • test that the command htop is now available
  • use the command which htop to see where the command is located
  • use the command realpath on the full path to the htop tool to find its real place in the filesystem
  • use the command nix search <pgm-name> to find the name of some Linux tools (non GUI), for example your favorite text editor
    • add the names of the packages to the configuration.nix file
    • rebuild the configuration
    • verify that the software is installed

7 install home-manager

  • open the file /etc/nixos/configuration.nix with yout favorite text editor
  • add the package name home-manager to the list of installed packages
  • save the file and exit the editor, rebuild the NixOS system
  • logout as root and login as the unprivileged user created above

8 install user packages with home-manager

  • as the unprivileged user, create the directory ~/.config/nixpkgs
  • in that directory, use a text editor to create the file home.nix. This file contains the per-user configuration for home-manager.
  • add this content into the file. This will install the packages gnupg, gtop and unzip for this user
{ config, pkgs, ... }:
{
  home.packages = with pkgs; [
      gnupg
      gtop
      unzip
    ];
}
  • build the home-directory configuration with home-manager switch
  • verify that the command gtop exists
  • use the commands which and realpath on gtop to find out where in the filesystem the tools binary is stored
  • remove one of the tools from the home.nix file
  • rebuild the home-environment with home-manager switch
  • verify that the tool is gone from this users environment

9 using the Nix Shell

  • verify that no Python interpreter is currently installed
[jane@nixos:~]$ python
The program ‘python’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.python3Minimal
  • enter a new Nix enviroment on the fly with nix-shell containing a current Python3 interpreter
$ nix-shell -p python 
  • verify that how this shell has access to the Python interpreter
[nix-shell:~]$ python
Python 2.7.18 (default, Apr 19 2020, 21:45:35) 
[GCC 9.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • other users, other shells of the same user, even the root user do not see this Python interpreter, because it is not linked into their environment