Creating a NixOS live CD: Difference between revisions

Unabomberlive (talk | contribs)
mNo edit summary
Tags: Mobile edit Mobile web edit
Unabomberlive (talk | contribs)
Make translatable
Tags: Mobile edit Mobile web edit
Line 6: Line 6:
* It is easy to add your own packages and configuration changes to the image.
* It is easy to add your own packages and configuration changes to the image.
</translate>
</translate>
 
<translate>
== 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.  
 
</translate>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, pkgs, ... }:
{ config, pkgs, ... }:
Line 24: Line 23:
}
}
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
Build the image via:
Build the image via:
 
</translate>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix
nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
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>
{{file|flake.nix|nix|<nowiki>
{{file|flake.nix|nix|<nowiki>
{
{
Line 52: Line 51:
}
}
</nowiki>}}
</nowiki>}}
 
<translate>
The following commands will generate the iso-image
The following commands will generate the iso-image:
 
</translate>
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# git init
# git init
Line 60: Line 59:
# nix build .#nixosConfigurations.exampleIso.config.system.build.isoImage
# nix build .#nixosConfigurations.exampleIso.config.system.build.isoImage
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
The resulting image can be found in <code>result</code>:
The resulting image can be found in <code>result</code>:
 
</translate>
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ ls result/iso/
$ ls result/iso/
nixos-24.05.20240721.63d37cc-x86_64-linux.iso
nixos-24.05.20240721.63d37cc-x86_64-linux.iso
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
=== Testing the image ===
=== Testing the image ===


To inspect the contents of the ISO image:
To inspect the contents of the ISO image:
 
</translate>
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ mkdir mnt
$ mkdir mnt
Line 79: Line 78:
$ umount mnt
$ umount mnt
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
To boot the ISO image in an emulator:
To boot the ISO image in an emulator:
 
</translate>
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ nix-shell -p qemu
$ nix-shell -p qemu
$ qemu-system-x86_64 -enable-kvm -m 256 -cdrom result/iso/nixos-*.iso
$ qemu-system-x86_64 -enable-kvm -m 256 -cdrom result/iso/nixos-*.iso
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
===SSH===
===SSH===
In your <tt>iso.nix</tt>:
In your <tt>iso.nix</tt>:
 
</translate>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
Line 101: Line 100:
}
}
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
===Static IP Address===
===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>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
Line 121: Line 120:
}
}
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
=== Building faster ===
=== Building faster ===
The build process is slow because of compression.
The build process is slow because of compression.


Here are some timings for <code>nix-build</code>:
Here are some timings for <code>nix-build</code>:
 
</translate>
{| class="wikitable" style="margin:auto"
{| class="wikitable" style="margin:auto"
|+ Compression results
|+ Compression results
Line 140: Line 139:
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43%
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43%
|}
|}
 
<translate>
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]


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>:
 
</translate>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
Line 151: Line 150:
}
}
</syntaxhighlight>
</syntaxhighlight>
 
<translate>
==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].
 
</translate>
<translate>
[[Category:NixOS]]
[[Category:NixOS]]
[[Category:Deployment]]
[[Category:Deployment]]
[[Category:Cookbook]]
[[Category:Cookbook]]
</translate>