Creating a NixOS live CD/ru: Difference between revisions

From NixOS Wiki
Unabomberlive (talk | contribs)
Created page with "Чтобы загрузить образ ISO в эмуляторе:"
Unabomberlive (talk | contribs)
Created page with "{| class="wikitable" style="margin:auto" |+ Результаты сжатия |- ! squashfsCompression !! Время !! Размер |- | <code>lz4</code> || 100s || 59% |- | <code>gzip -Xcompression-level 1</code> || 105s || 52% |- | <code>gzip</code> || 210s || 49% |- | <code>xz -Xdict-size 100%</code> (По умолчанию) || 450s || 43% |}"
Line 114: Line 114:
Here are some timings for <code>nix-build</code>:
Here are some timings for <code>nix-build</code>:
</div>
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
{| class="wikitable" style="margin:auto"
{| class="wikitable" style="margin:auto"
|+ Compression results
|+ Результаты сжатия
|-
|-
! squashfsCompression !! Time !! Size
! squashfsCompression !! Время !! Размер
|-
|-
| <code>lz4</code> || 100s || 59%
| <code>lz4</code> || 100s || 59%
Line 126: Line 125:
| <code>gzip</code> || 210s || 49%
| <code>gzip</code> || 210s || 49%
|-
|-
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43%
| <code>xz -Xdict-size 100%</code> (По умолчанию) || 450s || 43%
|}
|}
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]
See also: [https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce mksquashfs benchmarks]

Revision as of 19:40, 22 July 2024

Мотивация

Создание модифицированного NixOS LiveCD из существующей рабочей установки NixOS имеет ряд преимуществ:

  • Обеспечение подлинности.
  • Нет необходимости в доступе в Интернет.
  • Легко добавлять в образ собственные пакеты и изменения конфигурации.

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

Тестирование образа

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

Чтобы загрузить образ ISO в эмуляторе:

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

SSH

В вашем 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"
  ];
  ...
}

Статический IP-адрес

{
  ...
  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:

Результаты сжатия
squashfsCompression Время Размер
lz4 100s 59%
gzip -Xcompression-level 1 105s 52%
gzip 210s 49%
xz -Xdict-size 100% (По умолчанию) 450s 43%

Если вам не важен размер файла, вы можете использовать более быстрое сжатие, добавив этот параметр к вашему iso.nix:

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