Creating a NixOS live CD/en: Difference between revisions
Updating to match new version of source page |
Updating to match new version of source page |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== 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: | ||
* Ensures authenticity. | * Ensures authenticity. | ||
* No need for internet access. | * No need for internet access. | ||
* 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. | ||
== 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. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ config, pkgs, ... }: | { config, pkgs, ... }: | ||
Line 19: | Line 26: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Build the image via: | Build the image via: | ||
<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> | ||
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: | ||
{{file|flake.nix|nix|<nowiki> | {{file|flake.nix|nix|<nowiki> | ||
{ | { | ||
Line 29: | Line 40: | ||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; | inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; | ||
outputs = { self, nixpkgs }: { | outputs = { self, nixpkgs }: { | ||
packages.x86_64-linux.default = self.nixosConfigurations.exampleIso.config.system.build.isoImage; | |||
nixosConfigurations = { | nixosConfigurations = { | ||
exampleIso = nixpkgs.lib.nixosSystem { | exampleIso = nixpkgs.lib.nixosSystem { | ||
Line 43: | Line 55: | ||
} | } | ||
</nowiki>}} | </nowiki>}} | ||
The following commands will generate the iso-image: | The following commands will generate the iso-image: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
# nix build path:$PWD | |||
# nix build | |||
</syntaxhighlight> | </syntaxhighlight> | ||
The resulting image can be found in <code>result</code>: | The resulting image can be found in <code>result</code>: | ||
<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> | ||
=== Testing the image === | === Testing the image === | ||
To inspect the contents of the ISO image: | To inspect the contents of the ISO image: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ mkdir mnt | $ mkdir mnt | ||
Line 64: | Line 81: | ||
$ umount mnt | $ umount mnt | ||
</syntaxhighlight> | </syntaxhighlight> | ||
To boot the ISO image in an emulator: | To boot the ISO image in an emulator: | ||
<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> | ||
===SSH=== | ===SSH=== | ||
In your <tt>iso.nix</tt>: | In your <tt>iso.nix</tt>: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
Line 82: | Line 104: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===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. | ||
<syntaxhighlight lang="nix"> | |||
<syntaxhighlight lang="nix" line="1"> | |||
{ | { | ||
... | ... | ||
networking = { | networking = { | ||
usePredictableInterfaceNames = false; | usePredictableInterfaceNames = false; | ||
interfaces.eth0. | interfaces.eth0.ipv4.addresses = [{ | ||
address = "64.137.201.46"; | address = "64.137.201.46"; | ||
prefixLength = 24; | prefixLength = 24; | ||
Line 100: | Line 124: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== 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>: | ||
{| class="wikitable" style="margin:auto" | {| class="wikitable" style="margin:auto" | ||
|+ Compression results | |+ Compression results | ||
Line 117: | Line 144: | ||
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43% | | <code>xz -Xdict-size 100%</code> (default) || 450s || 43% | ||
|} | |} | ||
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>: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
Line 126: | Line 155: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==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]. | ||
[[Category:NixOS]] | [[Category:NixOS]] | ||
[[Category:Deployment]] | [[Category:Deployment]] | ||
[[Category:Cookbook]] | [[Category:Cookbook]] |