Creating a NixOS live CD: Difference between revisions

From NixOS Wiki
Unabomberlive (talk | contribs)
mNo edit summary
Tags: Manual revert Mobile edit Mobile web edit
Unabomberlive (talk | contribs)
Marked this version for translation
Tags: Mobile edit Mobile web edit
Line 1: Line 1:
<translate>
<translate>
<!--T:19-->
== Motivation ==
== Motivation ==
Creating a modified NixOS LiveCD out of an existing working NixOS installation has a number of benefits:
Creating a modified NixOS LiveCD out of an existing working NixOS installation has a number of benefits:
Line 7: Line 8:
</translate>
</translate>
<translate>
<translate>
<!--T:20-->
== Building ==
== Building ==
Building minimal NixOS installation CD with the <code>nix-build</code> command by creating this <code>iso.nix</code>-file. In this example with [[Neovim]] preinstalled.  
Building minimal NixOS installation CD with the <code>nix-build</code> command by creating this <code>iso.nix</code>-file. In this example with [[Neovim]] preinstalled.  
Line 24: Line 26:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:21-->
Build the image via:
Build the image via:
</translate>
</translate>
Line 30: Line 33:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:22-->
Alternatively, use Nix [[Flakes]] to generate a ISO installation image, using the <code>nixos-24.05</code> branch as nixpkgs source:
Alternatively, use Nix [[Flakes]] to generate a ISO installation image, using the <code>nixos-24.05</code> branch as nixpkgs source:
</translate>
</translate>
Line 52: Line 56:
</nowiki>}}
</nowiki>}}
<translate>
<translate>
<!--T:23-->
The following commands will generate the iso-image:
The following commands will generate the iso-image:
</translate>
</translate>
Line 60: Line 65:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:24-->
The resulting image can be found in <code>result</code>:
The resulting image can be found in <code>result</code>:
</translate>
</translate>
Line 67: Line 73:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
=== Testing the image === <!--T:7-->
<!--T:7-->
=== Testing the image ===
To inspect the contents of the ISO image:
To inspect the contents of the ISO image:
</translate>
</translate>
Line 78: Line 85:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:25-->
To boot the ISO image in an emulator:
To boot the ISO image in an emulator:
</translate>
</translate>
Line 85: Line 93:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:26-->
===SSH===
===SSH===
In your <tt>iso.nix</tt>:
In your <tt>iso.nix</tt>:
Line 100: Line 109:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
===Static IP Address=== <!--T:11-->
<!--T:11-->
===Static IP Address===
Static IP addresses can be set in the image itself. This can be useful for VPS installation.
Static IP addresses can be set in the image itself. This can be useful for VPS installation.
</translate>
</translate>
Line 119: Line 129:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:27-->
=== Building faster ===
=== Building faster ===
The build process is slow because of compression.
The build process is slow because of compression.


<!--T:28-->
Here are some timings for <code>nix-build</code>:
Here are some timings for <code>nix-build</code>:
</translate>
</translate>
<translate>
<translate>
<!--T:29-->
{| class="wikitable" style="margin:auto"
{| class="wikitable" style="margin:auto"
|+ Compression results
|+ Compression results
Line 140: Line 153:
</translate>
</translate>
<translate>
<translate>
<!--T:30-->
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]


<!--T:31-->
If you don't care about file size, you can use a faster compression
If you don't care about file size, you can use a faster compression
by adding this to your <code>iso.nix</code>:
by adding this to your <code>iso.nix</code>:
Line 151: Line 166:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:32-->
==See also==
==See also==
* [https://nixos.org/manual/nixos/stable/index.html#sec-building-image NixOS Manual: Building a NixOS (Live) ISO].
* [https://nixos.org/manual/nixos/stable/index.html#sec-building-image NixOS Manual: Building a NixOS (Live) ISO].

Revision as of 19:31, 22 July 2024

Motivation

Creating a modified NixOS LiveCD out of an existing working NixOS installation has a number of benefits:

  • Ensures authenticity.
  • No need for internet access.
  • It is easy to add your own packages and configuration changes to the image.

Building

Building minimal NixOS installation CD with the nix-build command by creating this iso.nix-file. In this example with Neovim preinstalled.

{ config, pkgs, ... }:
{
  imports = [
    <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>

    # Provide an initial copy of the NixOS channel so that the user
    # doesn't need to run "nix-channel --update" first.
    <nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
  ];
  environment.systemPackages = [ pkgs.neovim ];
}

Build the image via:

nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix

Alternatively, use Nix Flakes to generate a ISO installation image, using the nixos-24.05 branch as nixpkgs source:

flake.nix
{
  description = "Minimal NixOS installation media";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
  outputs = { self, nixpkgs }: {
    nixosConfigurations = {
      exampleIso = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ({ pkgs, modulesPath, ... }: {
            imports = [ (modulesPath + "/installer/cd-dvd/installation-cd-minimal.nix") ];
            environment.systemPackages = [ pkgs.neovim ];
          })
        ];
      };
    };
  };
}

The following commands will generate the iso-image:

# git init
# git add flake.nix
# nix build .#nixosConfigurations.exampleIso.config.system.build.isoImage

The resulting image can be found in result:

$ ls result/iso/
nixos-24.05.20240721.63d37cc-x86_64-linux.iso

Testing the image

To inspect the contents of the ISO image:

$ mkdir mnt
$ sudo mount -o loop result/iso/nixos-*.iso mnt
$ ls mnt
boot  EFI  isolinux  nix-store.squashfs  version.txt
$ umount mnt

To boot the ISO image in an emulator:

$ nix-shell -p qemu
$ qemu-system-x86_64 -enable-kvm -m 256 -cdrom result/iso/nixos-*.iso

SSH

In your iso.nix:

{
  ...
  # Enable SSH in the boot process.
  systemd.services.sshd.wantedBy = pkgs.lib.mkForce [ "multi-user.target" ];
  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 AaAeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee username@host"
  ];
  ...
}

Static IP Address

Static IP addresses can be set in the image itself. This can be useful for VPS installation.

{
  ...
  networking = {
    usePredictableInterfaceNames = false;
    interfaces.eth0.ip4 = [{
      address = "64.137.201.46";
      prefixLength = 24;
    }];
    defaultGateway = "64.137.201.1";
    nameservers = [ "8.8.8.8" ];
  };
  ...
}

Building faster

The build process is slow because of compression.

Here are some timings for nix-build:

Compression results
squashfsCompression Time Size
lz4 100s 59%
gzip -Xcompression-level 1 105s 52%
gzip 210s 49%
xz -Xdict-size 100% (default) 450s 43%

See also: mksquashfs benchmarks

If you don't care about file size, you can use a faster compression by adding this to your iso.nix:

{
  isoImage.squashfsCompression = "gzip -Xcompression-level 1";
}

See also