
A Step-by-Step Guide to Installing NixOS with GNOME on VirtualBox
NixOS stands out in the crowded world of Linux distributions due to its unique approach to package and configuration management. By using a declarative model, it allows you to define your entire system configuration in a single file, making your setup reproducible, reliable, and easy to roll back. If you’re curious to explore this powerful operating system without affecting your current setup, installing it in a virtual machine is the perfect solution.
This guide will walk you through the complete process of installing NixOS with the popular GNOME desktop environment inside VirtualBox.
Prerequisites
Before we begin, ensure you have the following ready:
- Oracle VirtualBox: Installed and running on your host machine.
- NixOS ISO Image: Download the latest stable graphical ISO with GNOME from the official NixOS website.
- Basic Linux Command-Line Knowledge: Familiarity with commands for navigation and editing text files will be helpful.
Step 1: Create the Virtual Machine in VirtualBox
First, we need to prepare the virtual environment for our NixOS installation.
- Open VirtualBox and click “New” to start the new virtual machine wizard.
- Name and Operating System: Give your VM a descriptive name like “NixOS GNOME”. Set the Type to “Linux” and the Version to “Linux 2.6 / 3.x / 4.x (64-bit)” or a more specific option if available.
- Memory Size: Allocate at least 4 GB (4096 MB) of RAM for a smooth GNOME experience. More is better if your host system allows it.
- Hard Disk: Select “Create a virtual hard disk now”. Choose “VDI (VirtualBox Disk Image)” as the type and “Dynamically allocated” for storage efficiency.
- Disk Size: A minimum of 32 GB is recommended to accommodate the operating system, applications, and your files.
- Finalize VM Settings: Once the VM is created, select it and go to Settings > System > Processor. Allocate at least 2 CPU cores. Then, navigate to Settings > Display and increase the Video Memory to the maximum possible (e.g., 128 MB) and enable 3D Acceleration.
- Load the ISO: Go to Settings > Storage. Click on the empty CD icon under the “Controller: IDE” section. On the right-hand panel, click the disc icon and select “Choose a virtual optical disk file…” to load the NixOS ISO you downloaded.
With the virtual machine configured, you’re ready to start the installation.
Step 2: Booting and Disk Partitioning
Start the VM you just created. It will boot from the NixOS ISO into a live environment. The first major task is to partition the virtual hard disk.
- Open a terminal from the live environment.
- Switch to the root user for administrative privileges:
bash
sudo -i
- Identify your virtual hard disk. It is typically
/dev/sda. You can confirm this with thelsblkcommand. - We will use
fdiskto create three essential partitions: a boot partition, a swap partition, and a root partition.
bash
fdisk /dev/sda
- Inside
fdisk, follow these commands:- Press
gto create a new empty GPT partition table. - Press
nto create the first partition for boot. Make it 512M in size. - Press
tto change the partition type, then enter1to select the EFI System type. - Press
nagain for the swap partition. We’ll allocate 4G (or an amount equal to your VM’s RAM). - Press
t, select partition 2, and enter19for Linux Swap. - Press
none last time to create the root partition. Use the remaining default space. - Finally, press
wto write the changes to the disk and exit.
- Press
Step 3: Formatting and Mounting the Partitions
With the partitions created, we need to format them with the correct filesystems and mount them.
- Format the boot partition:
bash
mkfs.fat -F 32 -n boot /dev/sda1
- Set up and activate the swap partition:
bash
mkswap -L swap /dev/sda2
swapon /dev/sda2
- Format the root partition:
bash
mkfs.ext4 -L nixos /dev/sda3
- Mount the partitions: The NixOS installer expects the root partition to be mounted at
/mnt.
bash
mount /dev/disk/by-label/nixos /mnt
mkdir -p /mnt/boot
mount /dev/disk/by-label/boot /mnt/boot
Step 4: Generating and Editing the NixOS Configuration
This is the core of the NixOS installation. We will generate a base configuration file and then edit it to define our desired system, including the GNOME desktop.
Generate the configuration files:
nixos-generate-config --root /mntThis creates two files in
/mnt/etc/nixos/:configuration.nixandhardware-configuration.nix. We only need to edit the first one.Edit
configuration.nix: Use thenanotext editor, which is available in the live environment.
bash
nano /mnt/etc/nixos/configuration.nix
Inside this file, you need to uncomment or add the following lines to set up our system. The file is well-commented, making it easy to navigate.
Enable the GRUB bootloader on an EFI system:
boot.loader.grub.enable = true; boot.loader.grub.device = "nodev"; # For EFI systems boot.loader.grub.efiSupport = true;Set your hostname:
networking.hostName = "nixos-vm";Enable NetworkManager for easy networking:
networking.networkmanager.enable = true;Set your time zone:
time.timeZone = "America/New_York"; # Replace with your time zoneEnable the GNOME Desktop Environment:
services.xserver.enable = true; services.xserver.desktopManager.gnome.enable = true; services.xserver.displayManager.gdm.enable = true;Enable VirtualBox Guest Additions for better integration (like screen resizing and shared clipboard):
virtualisation.virtualbox.guest.enable = true;Define your user account: Replace
"yourusername"with your desired username.users.users.yourusername = { isNormalUser = true; extraGroups = [ "wheel" "networkmanager" ]; # 'wheel' for sudo access };Install some useful packages (optional):
nix
environment.systemPackages = with pkgs; [
vim
wget
git
firefox
];
Save the file by pressing Ctrl + X, then Y, and Enter.
Step 5: The Final Installation
With our configuration defined, the final step is to tell NixOS to build and install the system.
Run the installation command:
nixos-installThis process will download all the required packages and build your system according to your
configuration.nixfile. It may take some time depending on your internet connection.During the installation, you will be prompted to set a password for the root user. Choose a strong password.
After the installation completes successfully, you will be prompted to set a password for the normal user you created (
yourusername).Reboot the system:
bash
reboot
Important: Before the VM reboots, be sure to go to the VirtualBox menu under Devices > Optical Drives and remove the NixOS ISO to prevent it from booting into the installer again.
Welcome to NixOS
After rebooting, you will be greeted by the GNOME login screen. Log in with the username and password you created. You now have a fully functional, declaratively configured NixOS system running with GNOME!
To update your system or install new software in the future, you simply edit your /etc/nixos/configuration.nix file and run the command sudo nixos-rebuild switch. This powerful workflow is what makes NixOS a truly modern and resilient operating system.
Source: https://kifarunix.com/install-nixos-with-gnome-desktop-on-virtualbox/


