Skip to content

Instantly share code, notes, and snippets.

@orther
Forked from rehno-lindeque/NixSetup.md
Created November 7, 2021 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orther/7537d22bbc3ef36b281f3ed81537a52b to your computer and use it in GitHub Desktop.
Save orther/7537d22bbc3ef36b281f3ed81537a52b to your computer and use it in GitHub Desktop.
NixOS Setup (Virtualized + Haskell + Gnome3 + XMonad)
# Micro graphical configuration that gets you started with a gnome3 (+ optionally xmonad) desktop
# * Note that gnome3 installs a *lot* of stuff from the internet
# * After modifying copying this configuration, run:
# $ nixos-rebuild switch # sometimes it helps to try again
# # if this step fails for some reason
# $ reboot
{ config, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
./hardware-configuration.nix
];
# Boot settings.
boot = {
initrd = {
# Disable journaling check on boot because virtualbox doesn't need it
checkJournalingFS = false;
};
# Use the GRUB 2 boot loader.
loader.grub = {
enable = true;
version = 2;
# Define on which hard drive you want to install Grub.
device = "/dev/sda";
};
};
# Enable networking.
networking = {
hostName = "mycomputer"; # Define your hostname.
hostId = # (use whatever was generated)
# wireless.enable = true; # not needed with virtualbox
};
# List services that you want to enable.
services = {
# Enable the OpenSSH daemon.
openssh.enable = true;
# Enable the X11 windowing system.
xserver = {
enable = true;
layout = "us";
# Gnome desktop
# * Slightly more familiar than KDE for people who are used to working with Ubuntu
# * Gnome3 works out of the box with xmonad
desktopManager = {
gnome3.enable = true;
default = "gnome3";
};
# Enable XMonad Desktop Environment. (Optional)
# windowManager = {
# xmonad.enable = true;
# xmonad.enableContribAndExtras = true;
# };
};
};
}
# Minimal graphical configuration that gets you started with a gnome3 (+ optionally xmonad) desktop
# * Note that gnome3 installs a *lot* of stuff from the internet
# * After modifying copying this configuration, run:
# $ nixos-rebuild switch # sometimes it helps to try again
# # if this step fails for some reason
# $ reboot
{ config, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
./hardware-configuration.nix
];
# Boot settings.
boot = {
initrd = {
# Disable journaling check on boot because virtualbox doesn't need it
checkJournalingFS = false;
# Make it pretty
kernelModules = [ "fbcon" ];
};
# Use the GRUB 2 boot loader.
loader.grub = {
enable = true;
version = 2;
# Define on which hard drive you want to install Grub.
device = "/dev/sda";
};
};
# Enable networking.
networking = {
hostName = "mycomputer"; # Define your hostname.
hostId = # (use whatever was generated)
# wireless.enable = true; # not needed with virtualbox
};
# List services that you want to enable.
services = {
# Enable the OpenSSH daemon.
openssh.enable = true;
# Enable the X11 windowing system.
xserver = {
enable = true;
layout = "us";
# Gnome desktop
# * Slightly more familiar than KDE for people who are used to working with Ubuntu
# * Gnome3 works out of the box with xmonad
desktopManager = {
gnome3.enable = true;
default = "gnome3";
};
# Enable XMonad Desktop Environment. (Optional)
# windowManager = {
# xmonad.enable = true;
# xmonad.enableContribAndExtras = true;
# };
};
};
# User account.
# * Set a password using
# $ useradd -m me ; passwd me
users.extraUsers.username = {
createHome = true;
home = "/home/me";
description = "Name Surname";
extraGroups = [ ];
isSystemUser = false;
useDefaultShell = true;
};
}

Setup NixOS (Virtualized + Haskell + Gnome3 + XMonad)

Before you get started

This is pretty out of date now... you may want to look elsewhere

Newer guides than mine (mine is a bit dated and has a lot of rough edges):

Have you looked at these?

Summary

These guidelines show how to install NixOS in VirtualBox with a bunch of stuff I find useful. Feel free to comment / fork / whatever.

DISCLAIMER: This is a rough guide that should only be used by people who are willing to check each step of the process themselves - if you mess up you can destroy your current setup, so be careful.

Why run NixOS inside VirtualBox?

  1. It's quick & simple and less likely to break your system.
  2. I tried to install NixOS directly on my MacBook Pro (2015), but couldn't manage to get the wifi firmware to work.
  3. If you're running OSX you can hopefully take advantage of the native power management for longer battery life, native firmware for everything etc. Stuff just works more.
  4. Virtualization seems like it could be pretty fast (I did notice that things felt a little bit laggy for me though, I'm still experimenting)
  5. These steps are setup to make it easy to switch to a native installation once you're ready.
  6. It may be easier to install unfree wifi firmware in virtualbox...

Intent behind this configuration

  1. It's my own config, intended to get me up and running quickly (i.e. it's opinionated and probably a bit hacky). Features:
  • Window manager: xmonad (a highly customizable, tiling window manager)
  • Haskell dev tools
  1. Do the minimal installation possible, it's easy to make additions, change things after you have a working installation.
  2. Uses native partitions - should in theory make it easy to switch to a native installation when you're ready.
  3. I used a variation of the steps provided by Install Linux on a MacBook Air and Installing NixOS in a VirtualBox guest.

Setup

When I set up my MacBook I made sure to turn off encryption, avoid logical volume stuff etc for the primary disk so that I wouldn't have to deal with any annoying issues when resizing my partitions.

I use two proper partitions (one for my / root directory containing operating system files + packages etc, one for my /home directory containing data + projects etc) on my laptop instead of relying on virtual disks. This should in theory make it easy to browse around your computer (nothing is hidden behind a .vdi file) or to install NixOS natively in future if you prefer to move away from VirtualBox.

Free some space (OSX specific instructions)

Open Disk Utility, click on “Macintosh HD”, add new partition, choose size (e.g., half), choose format as free space, apply. I'm not sure if this is going to work well, but it might be possible to mount the partition for home /home that is given to NixOS in OSX and share the space that way (instead of the usual shared folders).

Download NixOS ISO

Download the Minimal 64 bit ISO

Virtual Machine

Install Homebrew + Cask + VirtualBox (OSX specific instructions)

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap caskroom/cask
brew install brew-cask
brew cask install virtualbox

Create the VM

The following steps may require sudo. I haven't figured out how to create the .vmdk file without sudo yet.

nixos_livecd=path/to/downloaded/iso

# create vm
VBoxManage createvm \
  --name nixos \
  --ostype Linux_64 \
  --register

# create a virtual disk for the boot loader
# (since you obviously don't want to replace your native boot loader)
# Recommend between 200MB-300MB according to https://wiki.archlinux.org/index.php/Partitioning#How_big_should_my_partitions_be.3F
VBoxManage createhd \
  --filename "$HOME/VirtualBox VMs/nixos/boot.vdi" \
  --size 300

# create virtual disk/cd controller
VBoxManage storagectl nixos \
  --name IDE \
  --add ide \
  --controller PIIX4 \
  --portcount 42 \
  --hostiocache on \
  --bootable on

# attach boot disk image to VM
VBoxManage storageattach nixos \
  --storagectl IDE \
  --type hdd \
  --port 0 \
  --device 0 \
  --medium "$HOME/VirtualBox VMs/nixos/boot.vdi"

# attach livecd
VBoxManage storageattach nixos \
  --storagectl IDE \
  --type dvddrive \
  --port 1 \
  --device 0 \
  --medium $nixos_livecd

# attach the disk that is going to contain swap, / and /home partitions
VBoxManage createhd \
  --filename "$HOME/VirtualBox VMs/nixos/nixos.vmdk" \
  --format VMDK
  # TODO: I'm not sure about the details here... you probably need more flags

# bump up memory and CPU (you should probably increase this)
VBoxManage modifyvm nixos --memory 1024 --cpus 2

# enable usb port
VBoxManage modifyvm nixos --usb on

Create / format partitions

You may want to do create a logical volume with encryption instead, but I haven't tried this yet.

# Create boot partition
gdisk /dev/sda
n       # new partition
<enter> # default partition #
<enter> # default start location
<enter> # use the entire disk
ef02    # type boot

w       # write partitions
y       # confirm

# Create swap space, partition for /, partition for /home
gdisk /dev/sdb
n       # new partition
<enter> # default partition #
<enter> # default start location
+2G     # maybe 2 GB swap space?
8200    # swap space

n       # new partition
<enter> # default partition #
<enter> # default start location
+100G   # maybe 100 GB for operating system files?
8300    # linux filesystem (this will be the root / path)

n       # new partition
<enter> # default partition #
<enter> # default start location
<enter> # use all remaining space (maybe 300 GB?)
8302    # linux /home path

w       # write partitions
y       # confirm

# Check that everything looks ok
fdisk -l

# e.g.
#
#    Device           Start         End     Sectors   Size Type
# /dev/sda1            2048     1048542     1046495   299M BIOS boot
#
#    Device           Start         End     Sectors   Size Type
# /dev/sdb1              40      xxxxxx      409600   200M EFI System
# /dev/sdb2        xxxxxxxx      xxxxxx     xxxxxxx  32.6G Apple Core storage
# /dev/sdb3        xxxxxxxx      xxxxxx     xxxxxxx 619.9M Apple boot
# /dev/sdb4        xxxxxxxx      xxxxxx     xxxxxxx     2G Linux swap
# /dev/sdb5        xxxxxxxx      xxxxxx     xxxxxxx   100G Linux filesystem
# /dev/sdb6        xxxxxxxx      xxxxxx     xxxxxxx   300G Linux home

# Format the partitions
mkfs.ext2 -L boot /dev/sda1
mkswap -L swap /dev/sdb4
mkfs.ext4 -j -L nixos /dev/sdb5
mkfs.ext4 -j -L home /dev/sdb6

Mount partitions

NixOS will generate its hardware config based on what is mounted.

swapon /dev/sdb4
mount /dev/sdb5 /mnt
mkdir /mnt/home ; mount /dev/sdb6 /mnt/home
mkdir /mnt/boot ; mount /dev/sda1 /mnt/boot

Minimal configuration to install NixOS

I recommend doing the minimum possible to install NixOS. It's easier to modify it with a new config after your installation is finished.

# You can do this repeatedly, it will only overwrite /mnt/etc/nixos/hardware-configuration.nix not /mnt/etc/nixos/configuration.nix
nixos-generate-config --root /mnt

Alter /mnt/etc/nixos/configuration.nix:

boot.loader.grub.device = "/dev/sda"; # Carefull! this should be the virtual boot disk we created before, not your real disk!

Copy a configuration of your choice

Continue editing /etc/nixos/configuration.nix and use nixos-rebuild switch to update the system-wide configuration - try not to do anything that should be done as a user (I.e. avoid acting as root/sudo whenever possible).

Switch to a minimal (gnome/xmonad-based) graphical configuration by copying either micro-configuration.nix or mini-configuration.nix over to /etc/nixos/configuration.nix

Switch to or roll your own full-fledged configuration of your choice (see configuration.nix below)

Configurations I sometimes look at

Common searches

Setup

Editors

Browsers

Haskell

More reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment