Tuesday, June 3, 2014

Proud To See An Linux Distribution In The Name Of  "NIX"

When categorizing Linux distributions, it is often useful to specify which package management system the distribution uses. There are many different package management systems in use, but the two most popular are probably the .deb and .rpm formats. The first comes from Debian and is used by Ubuntu, while the second was developed by RedHat.
NixOS is a Linux distribution with its own unique package manager. The entire operating system, including the kernel, applications, system packages and configuration files, are built by the Nix package manager. The Nix Package Manager uses a declarative system configuration model. This means system configuration items are described in a configuration file, and the OS does everything necessary to make the configuration happen.
If a system admin wants to install a SSH server, then all they need to do is add a line to the configuration to enable that service. NixOS will then download and install OpenSHH, plus generate the relevant configuration files.
Another interesting feature of NixOS is that it stores all its packages in isolation from each other; as a result there are no /bin, /sbin, /lib or /usr directories. All packages are kept in /nix/store instead. This, together with the declarative configuration model, makes upgrading NixOS systems reliable and allows the OS to implement a transactional approach to system management including the ability to roll back upgrades.
The declarative configuration model also makes it easy to reproduce a system configuration on another machine. The admin just copies the configuration file to the new NixOS machine and with one command, the machine will be transformed to use the same kernel, applications, system services, and so on.
To try out NixOS, download the .ISO file from the project’s download page, burn it onto a CD and boot your PC from it. Once booted, you will be presented with a text login prompt. Enter “root” as the username and press ENTER. The NixOS installer doesn’t do any partitioning or formatting, so you need to create and format the partitions manually.
NixOS-install-login
Note: The following commands will erase EVERYTHING on the disk; please proceed with caution.
Type the following:
fdisk /dev/sda
Where “/dev/sda” is the name of the disk you want to use, in this case the first hard disk in the PC.
In fdisk, type “o” to create an empty partition table. Type “n, p, 1, ENTER, +2G, t, 82″ to create a new primary partition of 2Gb to be used as swap space. Now type “n, p, 2, ENTER, ENTER” to create a new primary partition for the NixOS installation. Exit out of fdisk using “w”. If you want to use the free space on the disk without erasing the existing data, then don’t create an empty partition table and modify your partitioning scheme accordingly.
NixOS-partitions
Activate the swap space using:
mkswap -L swap /dev/sda1
swapon /dev/sda1
Now format the second partition:
mkfs.ext4 -L nixos /dev/sda2
Now mount the new partition:
mount /dev/disk/by-label/nixos /mnt
Descriptive configuration files are at the heart of NixOS and the Nix Package Manager. Even the installation process needs a configuration file to tell NixOS what to install. To generate a default configuration file type:
nixos-generate-config --root /mnt
Now edit the resulting file using nano:
nano /mnt/etc/nixos/configuration.nix
Find the following lines and remove the “#” sign from the beginning:
  • boot.loader.grub.device = “/dev/sda”
  • services.openssh.enable = true
  • services.xserver.enable = true
  • services.xserver.layout = “us”
  • services.xserver.desktopManager.kde4.enable = true;
  • services.xserver.displayManager.kdm.enable = true;
Exit nano using CTRL-X and start the installation:
nixos-install
Once the installation has completed, reboot your system. You will need to remove the CD or pick the “Boot from hard disk” option if you booted from the optical drive.
The system should boot into the display manager; however, you can’t yet log in as there are no users defined, and root logins aren’t allowed on the desktop. Press CTRL+ALT+F1 to switch to a console.
Now log in as root and add a user. Here is how to create the user “gary”:
useradd -m gary
passwd gary
You should also change the root password using “passwd”.
Press CTRL+ALT+F7 to get back to the graphical login prompt and log in with the username and password you created above.
NixOS-desktop
NixOS doesn’t include a graphical package manager like Ubuntu or Fedora and all package commands need to be run from the terminal. To list all of the available packages type:
nix-env -qa \*
You can use “grep” to find certain packages. Here is how to search for the substring “fire”:
nix-env -qa \* | grep -i fire
In the list will be the Firefox web browser. To install it use:
nix-env -i firefox
The way we added the user and the way we installed Firefox is called imperative management. You execute a command and it happens. However the power of NixOS is in its declarative management. With declarative management, the system administrator tells NixOS what needs to be achieved, and NixOS will ensure that it happens without breaking the system.
For example, to add a user using declarative user management, edit “/etc/nixos/configuration.nix” (as root). Add the following lines at the end of the file, but before the last “}”:
 users.extraUsers.alice =
{
createHome = true;
home = "/home/alice";
extraGroups = [ "wheel" ];
useDefaultShell = true;
};
To install a package, add the following line to the configuration.nix file:
 environment.systemPackages = [ pkgs.thunderbird ];
NixOS-configuration-nix
To action the new configuration.nix file, use the nixos-rebuild command:
nixos-rebuild switch
This command does everything necessary to make the configuration happen, including downloading Thunderbird and creating the new user etc.
The power of NixOS is formidable. If you want to go deeper then you should look at the NixOS manual, and if you need help you should try the NixOS community page.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.