Generate Live USB with NixOS flakes?

I’ve finally switched to flakes, but still can’t figure out how to build live USB with it. I want to create the custom IMG file, so I can dd it to an USB drive.

Here is mine flake.nix:

{
  description = "Unstable + HM";

  inputs = {
    home-manager = {
      url = "github:rycee/home-manager/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    nixpkgs.url = "nixpkgs/nixos-unstable";
  };

  outputs = { self, home-manager, nixpkgs }:
    let
      pkgs = (import nixpkgs) {
        system = "x86_64-linux";
      };

      hosts = map (pkgs.lib.removeSuffix ".nix") (
        pkgs.lib.attrNames (
          pkgs.lib.filterAttrs
            (_: entryType: entryType == "regular")
            (builtins.readDir ./hosts)
        )
      );

      build-target = host: {
        name = host;

        value = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";

          modules = [
            home-manager.nixosModules.home-manager
            (import (./hosts + "/${host}.nix"))
          ];
        };
      };

    in
      {
        nixosConfigurations = builtins.listToAttrs (
          pkgs.lib.flatten (
            map
              (
                host: [
                  (build-target host)
                ]
              )
              hosts
          )
        );
      };
}

live-usb.nix locates in ./hosts:

{ config, pkgs, lib, ... }:
{
  isoImage.volumeID = lib.mkForce "id-live";
  isoImage.isoName = lib.mkForce "id-live.iso";

  imports = [
    <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-base.nix>
  ];

  networking.hostName = "live-usb";
  networking.networkmanager.enable = true; # nmcli for wi-fi
  networking.wireless.enable = lib.mkForce false;

  security.polkit.extraConfig = ''
    polkit.addRule(function(action, subject) {
      if (subject.isInGroup("wheel")) {
        return polkit.Result.YES;
      }
    });
  '';

  services.xserver = {
    displayManager = {
      autoLogin = { enable = true; user = vars.user; };
    };
    videoDrivers = [ "nvidia" "amdgpu" "vesa" "modesetting" ];
  };

}

I can see the configuration:

git+file:///etc/nixos
└───nixosConfigurations
    ├───catch-22: NixOS configuration
    ├───cyberdemon: NixOS configuration
    ├───hk47: NixOS configuration
    ├───laundry: NixOS configuration
    ├───live-usb: NixOS configuration
    ├───prism: NixOS configuration
    └───skynet: NixOS configuration

But have no idea what to use instead of

nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=/etc/nixos/hosts/live-usb.nix

Something like this:

nix build .#nixosConfigurations.live-usb.config.system.build.toplevel

and then use isoImage or vm instead depending on the use-case

3 Likes

Thank you!

nix build .#nixosConfigurations.live-usb.config.system.build.isoImage --impure

did the trick

Trying to build this, I get.

error: undefined variable 'vars'

       at /nix/store/ynbi37jng9jzjwpl4hayl1cpicqa6kpq-source/hosts/live-usb.nix:24:43:

           23|     displayManager = {
           24|       autoLogin = { enable = true; user = vars.user; };
             |                                           ^
           25|     };