Jump to content

Creating a NixOS live CD/zh: Difference between revisions

From Official NixOS Wiki
Covalode (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit
Covalode (talk | contribs)
Created page with "压缩使得构建过程变得缓慢。"
 
(18 intermediate revisions by the same user not shown)
Line 13: Line 13:
== 构建 ==
== 构建 ==


创建<code>iso.nix</code>文件,并使用<code>nix-build</code>命令来构建最小化的NixOS安装镜像。如下示例中预装了[[Neovim]]  
创建<code>iso.nix</code>文件,并使用<code>nix-build</code>命令来构建最小化的NixOS安装镜像。如下示例中预装了[[Neovim]]


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 35: Line 35:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
另外,也可以使用[[Flakes]]来生成ISO安装镜像。示例中使用<code>nixos-24.05</code>作为nixpkgs源。
Alternatively, use Nix [[Flakes]] to generate a ISO installation image, using the <code>nixos-24.05</code> branch as nixpkgs source:
</div>


{{file|flake.nix|nix|<nowiki>
{{file|flake.nix|nix|<nowiki>
Line 60: Line 58:
</nowiki>}}
</nowiki>}}


<div lang="en" dir="ltr" class="mw-content-ltr">
用以下命令生成iso镜像:
The following commands will generate the iso-image:
</div>


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 69: Line 65:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
生成的镜像文件可以在<code>result</code>中找到
The resulting image can be found in <code>result</code>:
</div>


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 81: Line 75:
=== 测试镜像 ===
=== 测试镜像 ===


<div lang="en" dir="ltr" class="mw-content-ltr">
查看ISO镜像中的内容:
To inspect the contents of the ISO image:
</div>


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 93: Line 85:
</syntaxhighlight>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
在模拟器中启动镜像:
To boot the ISO image in an emulator:
</div>


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 104: Line 94:
===SSH===
===SSH===


的 <tt>iso.nix</tt> 中:
的 <tt>iso.nix</tt> 中添加


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 121: Line 111:
=== 静态 IP 地址===
=== 静态 IP 地址===


<div lang="en" dir="ltr" class="mw-content-ltr">
你可以直接在镜像中设置好静态IP地址。这对于在VPS上进行安装可能会很有帮助。
Static IP addresses can be set in the image itself. This can be useful for VPS installation.
</div>


<syntaxhighlight lang="nix" line="1">
<syntaxhighlight lang="nix" line="1">
Line 144: Line 132:
=== 更快速的构建 ===
=== 更快速的构建 ===


<div lang="en" dir="ltr" class="mw-content-ltr">
构建过程缓慢的原因是压缩。
The build process is slow because of compression.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
以下是<code>nix-build</code>使用的一些压缩方式的用时测试结果:
Here are some timings for <code>nix-build</code>:
</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 166: Line 149:
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43%
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43%
|}
|}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 172: Line 154:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果你并不在意文件大小,可以在你的<code>iso.nix</code>中添加如下内容以使用更快的压缩方式:
If you don't care about file size, you can use a faster compression
by adding this to your <code>iso.nix</code>:
</div>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 186: Line 165:
== 另见 ==
== 另见 ==


<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://nixos.org/manual/nixos/stable/index.html#sec-building-image NixOS 手册: 构建一个 NixOS (Live) ISO].
* [https://nixos.org/manual/nixos/stable/index.html#sec-building-image NixOS Manual: Building a NixOS (Live) ISO].
</div>


[[Category:NixOS]]
[[Category:NixOS]]
[[Category:Deployment]]
[[Category:Deployment]]
[[Category:Cookbook]]
[[Category:Cookbook]]

Latest revision as of 19:31, 9 July 2026

起因

从已安装的NixOS系统中创建一个自定义的 NixOS Live CD 有许多优势:

  • 确保可信度
  • 无需访问互联网
  • 很容易向镜像中添加自己的包和配置

构建

创建iso.nix文件,并使用nix-build命令来构建最小化的NixOS安装镜像。如下示例中预装了Neovim

{ 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 ];
}

通过以下命令构建镜像:

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

另外,也可以使用Flakes来生成ISO安装镜像。示例中使用nixos-24.05作为nixpkgs源。

❄︎ flake.nix
{
  description = "Minimal NixOS installation media";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.default = self.nixosConfigurations.exampleIso.config.system.build.isoImage;
    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 ];
          })
        ];
      };
    };
  };
}

用以下命令生成iso镜像:

# nix build path:$PWD

生成的镜像文件可以在result中找到

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

测试镜像

查看ISO镜像中的内容:

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

在模拟器中启动镜像:

$ 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 地址

你可以直接在镜像中设置好静态IP地址。这对于在VPS上进行安装可能会很有帮助。

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

更快速的构建

构建过程缓慢的原因是压缩。

以下是nix-build使用的一些压缩方式的用时测试结果:

压缩测试结果
squashfsCompression 用时 大小
lz4 100s 59%
gzip -Xcompression-level 1 105s 52%
gzip 210s 49%
xz -Xdict-size 100% (default) 450s 43%

如果你并不在意文件大小,可以在你的iso.nix中添加如下内容以使用更快的压缩方式:

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

另见