Creating a nixos live cd for whole system

I have been attempting to follow the NixOS guide for generating a Live CD, but I am uncertain as to how to incorporate it into my Flake configuration. My goal is to make an ISO image that covers the whole system.

{
  description = "My NixOS configuration";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager = {
      url = github:nix-community/home-manager;
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs@{ self, nixpkgs, home-manager, ... }:
    let
      user = "lukas";
      configDir = "nix-config";
    in {
      nixosConfigurations = import ./hosts {
        inherit (nixpkgs) lib;
        inherit inputs nixpkgs home-manager user configDir;
      };
    }; 
}

What do I have to add to the flake above so nix build works?

Below you can see a working example. It’s what I do, with some bits removed:

run with nix build .#iso

# flake.nix
{
  inputs = {
    nixpkgs.url = "flake:nixpkgs/nixos-unstable";

    flake-utils.url = "github:numtide/flake-utils";

    nixos-generators.url = "github:nix-community/nixos-generators";
    nixos-generators.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs@{ self, nixpkgs, flake-utils, ... }: {
    ## nix build .#iso
    ## nixcfg --build-iso && nixcfg --burn-iso 00000111112222333
    packages.x86_64-linux.iso = inputs.nixos-generators.nixosGenerate {
      system = "x86_64-linux";
      format = "install-iso";
      specialArgs = {
        inherit inputs;
      };
      modules = [
        "${inputs.nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix"
        ./images/iso.nix
        {
          system.stateVersion = "23.11";
        }
      ];
    };
  };
}

# images/iso.nix
{ lib
, pkgs
, ...
}:
##   - Build iso:
# nix build .#iso
##   - Find installation device (eg. /dev/sdX):
# lsblk
##   - Write to thumb-drive:
# sudo dd bs=4M if=result/iso/my-nixos-live.iso of=/dev/sdX status=progress oflag=sync
{
  imports = [
    ./base-config.nix
  ];

  isoImage.volumeID = lib.mkForce "my-nixos-live";
  isoImage.isoName = lib.mkForce "my-nixos-live.iso";
  # Use zstd instead of xz for compressing the liveUSB image, it's 6x faster and 15% bigger.
  isoImage.squashfsCompression = "zstd -Xcompression-level 6";
}
# images/base-config.nix
{ lib
, pkgs
, ...
}:
{
  imports = [
    # ../modules/base-system.nix
    # ../modules/services/numlock-on-tty
  ];

  networking = {
    useDHCP = false;
    hostName = "my-nixos-live"; # default: "nixos"
    usePredictableInterfaceNames = false;
    interfaces.eth0.useDHCP = true;
    # interfaces.eth0.ipv4.addresses = [
    #   {
    #     address = "192.168.1.2";
    #     prefixLength = 24;
    #   }
    # ];
    # defaultGateway = "192.168.1.1";
    # nameservers = [ "192.168.1.1" "1.1.1.1" "8.8.8.8" ];
  };

  boot.supportedFilesystems = [ "zfs" "f2fs" ];
  # serial connection for apu
  boot.kernelParams = [ "console=ttyS0,115200n8" ];

  users.mutableUsers = false;
  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 xxxx me@mypc"
  ];
  users.users = {
    "nixos" = {
      isNormalUser = true;
      home = "/home/nixos";
      password = "";
      uid = 1000;
      extraGroups = [ "systemd-journal" "wheel" ];
    };
  };

  # sshd
  services.openssh = {
    enable = true;
    settings.PasswordAuthentication = false;
    settings.PermitRootLogin = lib.mkDefault "prohibit-password";
    hostKeys = [
      { type = "rsa"; bits = 4096; path = "/etc/ssh/ssh_host_rsa_key"; }
      { type = "ed25519"; path = "/etc/ssh/ssh_host_ed25519_key"; }
    ];
  };

  services.avahi = {
    enable = true;
    nssmdns = true;
    publish.addresses = true;
    publish.domain = true;
    publish.enable = true;
    publish.userServices = true;
    publish.workstation = true;
  };

  # Turn on flakes.
  nix.package = pkgs.nixVersions.stable;
  nix.extraOptions = ''
    experimental-features = nix-command flakes
  '';

  # includes this flake in the live iso : "/etc/nixcfg"
  environment.etc.nixcfg.source =
    builtins.filterSource
      (path: type:
        baseNameOf path
        != ".git"
        && type != "symlink"
        && !(pkgs.lib.hasSuffix ".qcow2" path)
        && baseNameOf path != "secrets")
      ../.;

  environment.systemPackages = with pkgs; [
    git
    htop
    tmux
    tree
    nano
    rsync
    ripgrep
    cryptsetup
    nixpkgs-fmt
  ];

  ## FIX for running out of space / tmp, which is used for building
  fileSystems."/nix/.rw-store" = {
    fsType = "tmpfs";
    options = [ "mode=0755" "nosuid" "nodev" "relatime" "size=14G" ];
    neededForBoot = true;
  };





  # Part of base-system.nix:
  time.timeZone = lib.mkDefault "Etc/UTC";

  i18n = {
    defaultLocale = "en_IE.UTF-8";
    extraLocaleSettings = {
      LC_TIME = "en_GB.UTF-8";
    };
    supportedLocales = lib.mkDefault [
      "en_GB.UTF-8/UTF-8"
      "en_IE.UTF-8/UTF-8"
      "en_US.UTF-8/UTF-8"
    ];
  };
  environment.variables = {
    TERM = "xterm-256color";
  };

  # # Use a high-res font.
  # boot.loader.systemd-boot.consoleMode = "0";
  console = {
    # https://github.com/NixOS/nixpkgs/issues/114698
    earlySetup = true; # Sets the font size much earlier in the boot process
    colors = [
      # # frappe colors
      "51576d"
      "e78284"
      "a6d189"
      "e5c890"
      "8caaee"
      "f4b8e4"
      "81c8be"
      "b5bfe2"
      "626880"
      "e78284"
      "a6d189"
      "e5c890"
      "8caaee"
      "f4b8e4"
      "81c8be"
      "a5adce"
    ];
    font = "Lat2-Terminus16";
    useXkbConfig = true; # Use same config for linux console
  };

  services.xserver = {
    enable = lib.mkDefault false; # but still here so we can copy the XKB config to TTYs
    autoRepeatDelay = 300;
    autoRepeatInterval = 35;
  } // lib.optionalAttrs false {
    xkbVariant = "colemak";
    xkbOptions = "caps:super,compose:ralt,shift:both_capslock";
  };
}
2 Likes

Thank you! I’ll look into it!

the import

"${inputs.nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix"

should not be needed as it is already imported by the format from nixos-generators. Or did you run into issues without that line?

1 Like

I removed it and it still builds. It was a line from before I used the generator.

1 Like