NixOS

NixOS is the distribution!

Nix is the package manager!

Take note of 2024年03月31日 since it may be helpful.

Lately, I have been slowly relying on using nixos modules for installation, but it still not majority of it of course. Reading the nix file is still hard but I noticed that variables are typically laid out this way.

variable = mkEnableOption (lib.mdDoc "Something") // { ... };

I don’t know the purpose of // there but it doesn’t seem like a comment. Sometimes it is just mkOption.

Installing VirtualBox

{ config, lib, pkgs, ... }:

{
  imports =
    [
      # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  virtualisation.virtualbox.host = {
    enable = true;
    enableExtensionPack = true; (1)
  };
}
1 This will force VirtualBox to compile. You can skip this if you don’t need the Extension Pack by Oracle.

If you get an error saying.

Kernel driver not installed (rc=-1908)
The VirtualBox Linux kernel driver is either not loaded or not set up correctly. Please try setting it up again by executing

'/sbin/vboxconfig'

as root.

If your system has EFI Secure Boot enabled you may also need to sign the kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load them. Please see your Linux system's documentation for more information.

where: suplibOsInit what: 3 VERR_VM_DRIVER_NOT_INSTALLED (-1908) - The support driver is not installed. On linux, open returned ENOENT.

This can be solved by rebooting.

Installing VMWare Workstation Pro

{ config, lib, pkgs, ... }:

{
  imports =
    [
      # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  virtualisation.vmware.host = {
    enable = true;
  };
}

This automatically starts vmware-networks.service. You can disable this by adding.

  systemd.services = {
    vmware-networks.wantedBy = lib.mkForce [ ];
  };

/tmp as tmpfs

By default, NixOS does not mount tmpfs on /tmp thus /tmp will persists across reboot. Use boot.tmp.useTmpfs = true; to mount /tmp as tmpfs.

Upgrade Flakes

  1. Run

nix flake update

Then, upgrade

nixos-rebuild switch --flake . --upgrade

VMWare

For solving build issues due to old module version (source)
nixpkgs.overlays = [
  (
    final: prev: {
      linuxPackages_latest = prev.linuxPackages_latest.extend (
        _lpfinal: _lpprev: {
          vmware = prev.linuxPackages_latest.vmware.overrideAttrs (_oldAttrs: {
            version = "workstation-17.5.2-k6.9+-unstable-2024-08-22";
            src = final.fetchFromGitHub {
              owner = "nan0desu";
              repo = "vmware-host-modules";
              rev = "b489870663afa6bb60277a42a6390c032c63d0fa";
              hash = "sha256-9t4a4rnaPA4p/SccmOwsL0GsH2gTWlvFkvkRoZX4DJE=";
            };
          });
        }
      );
    }
  )
];

Cleaning up Nix Store

Delete all profiles in /nix/var/nix/profiles.

sudo nix profile wipe-history --profile /nix/var/nix/profiles/system --older-than 3d

Then clean up nix store.

nix store gc

This cleans more data and saves more storage than nix-garbage-collect. Might be because of removing the profile histories.

NixOs List Generations

nixos-rebuild list-generations

Updating

nixos-rebuild switch --upgrade

Installation

Summary:

  1. connect to wifi using wpa_cli

  2. partition the disk with parted (esp, root, home). make sure to use BTRFS

  3. use Full-Disk Encryption

  4. create subvolumes (nix, persist, log)

  5. mount them

partitioning the drive

  1. Set partition table to GPT

    #parted /dev/<device> -- mklabel gpt
    parted /dev/vda -- mklabel gpt
  2. Create boot partition

    #parted /dev/<device> -- mkpart ESP fat32 1MiB 512MiB
    parted /dev/vda -- mkpart ESP fat32 1MiB 512MiB
  3. Set ESP boot flag

    #parted /dev/<device> -- set <partition number> <partition label> on
    parted /dev/vda -- set 1 ESP on
  4. Create root and home partition

    #parted /dev/<device> -- mkpart root <file system> <start> end>
    parted /dev/vda -- mkpart root btrfs 512MiB 32.5MiB
    parted /dev/vda -- mkpart home btrfs 32.5GiB 100%

simpler version

parted /dev/vda
mklabel gpt
mkpart ESP fat32 1MiB 512MiB
set 1 ESP on
mkpart root btrfs 512MiB 32.5GiB
mkpart home btrfs 32.5GiB 100%

print to verify

formatting with luks encryption

  1. format boot partition (no encryption)

    mkfs.fat -F 32 -n boot /dev/vda1
  2. format root and home partitions with luks

    #cryptsetup --verify-passphrase -v luksFormat <partition>
    cryptsetup --verify-passphrase -v luksFormat /dev/vda2
    cryptsetup --verify-passphrase -v luksFormat /dev/vda3
    This does not include a label to the luks container. Use cryptsetup config <luks container> --label <label> to add a label.
  3. mount encrypted partitions

    #cryptsetup open <partition> <label>
    cryptsetup open /dev/vda2 root_luks
    cryptsetup open /dev/vda2 home_luks
  4. partition home and root

    #mkfs.btrfs -L <label> <luks dev mapper name>
    mkfs.btrfs -L nixos /dev/mapper/root_luks
    mkfs.btrfs -L home /dev/mapper/home_luks

    i use root and home to have separate partition labels. this causes an issue in booting if labels are the same. the labels here might be used by /dev/disk/by-label/.

  5. mounting root and home partitions

    #mount -t <file system> /dev/mapper/<partition> </mnt locations>
    mount -t btrfs /dev/mapper/root_luks /mnt
    mount -t btrfs /dev/mapper/home_luks /mnt/home

    do mkdir /mnt/home if directory does not exist

  6. creating subvolumes

    btrfs subvolume create /mnt/root
    btrfs subvolume create /mnt/nix
    btrfs subvolume create /mnt/persist
    btrfs subvolume create /mnt/log
    btrfs subvolume snapshot -r /mnt/root /mnt/root-blank
  7. unmount

    umount /mnt/home
    umount /mnt
  8. mount root and home, and others

    mount -o subvol=root,compress=zstd,noatime,ssd,space_cache=v2 /dev/mapper/root_luks /mnt
  9. create directories for mount point

    mkdir /mnt/home
    mkdir /mnt/nix
    mkdir /mnt/persist
    mkdir -p /mnt/var/log
  10. mount home and subvolumes

    mount -o compress=zstd,relatime,ssd,space_cache=v2 /dev/mapper/home_luks /mnt/home
    mount -o subvol=nix,compress=zstd,noatime,ssd,space_cache=v2 /dev/mapper/root_luks /mnt/nix
    mount -o subvol=persist,compress=zstd,noatime,ssd,space_cache=v2 /dev/mapper/root_luks /mnt/persist
    mount -o subvol=log,compress=zstd,noatime,ssd,space_cache=v2 /dev/mapper/root_luks /mnt/var/log
  11. mount boot

    mkdir /mnt/boot
    #mount /dev/<partition> /mnt/boot
    mount /dev/vda1 /mnt/boot
  12. generate nixos-config

    nixos-generate-config --root /mnt