Creating a NixOS live CD/zh: Difference between revisions
Appearance
Created page with "== 构建 ==" |
Created page with "压缩使得构建过程变得缓慢。" |
||
| (26 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
< | <span id="Motivation"></span> | ||
== | == 起因 == | ||
从已安装的NixOS系统中创建一个自定义的 NixOS Live CD 有许多优势: | |||
* 确保可信度 | |||
* | |||
* 无需访问互联网 | |||
* | |||
* 很容易向镜像中添加自己的包和配置 | |||
* | |||
<span id="Building"></span> | <span id="Building"></span> | ||
== 构建 == | == 构建 == | ||
创建<code>iso.nix</code>文件,并使用<code>nix-build</code>命令来构建最小化的NixOS安装镜像。如下示例中预装了[[Neovim]]。 | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 40: | Line 29: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
通过以下命令构建镜像: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 48: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
另外,也可以使用[[Flakes]]来生成ISO安装镜像。示例中使用<code>nixos-24.05</code>作为nixpkgs源。 | |||
{{file|flake.nix|nix|<nowiki> | {{file|flake.nix|nix|<nowiki> | ||
| Line 73: | Line 58: | ||
</nowiki>}} | </nowiki>}} | ||
用以下命令生成iso镜像: | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 82: | Line 65: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
生成的镜像文件可以在<code>result</code>中找到 | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 94: | Line 75: | ||
=== 测试镜像 === | === 测试镜像 === | ||
查看ISO镜像中的内容: | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 106: | Line 85: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
在模拟器中启动镜像: | |||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
| Line 117: | Line 94: | ||
===SSH=== | ===SSH=== | ||
在你的 <tt>iso.nix</tt> 中添加: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 136: | Line 111: | ||
=== 静态 IP 地址=== | === 静态 IP 地址=== | ||
你可以直接在镜像中设置好静态IP地址。这对于在VPS上进行安装可能会很有帮助。 | |||
<syntaxhighlight lang="nix" line="1"> | <syntaxhighlight lang="nix" line="1"> | ||
| Line 156: | Line 129: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
< | <span id="Building_faster"></span> | ||
=== | === 更快速的构建 === | ||
构建过程缓慢的原因是压缩。 | |||
以下是<code>nix-build</code>使用的一些压缩方式的用时测试结果: | |||
{| class="wikitable" style="margin:auto" | {| class="wikitable" style="margin:auto" | ||
|+ | |+ 压缩测试结果 | ||
|- | |- | ||
! squashfsCompression !! | ! squashfsCompression !! 用时 !! 大小 | ||
|- | |- | ||
| <code>lz4</code> || 100s || 59% | | <code>lz4</code> || 100s || 59% | ||
| Line 182: | Line 149: | ||
| <code>xz -Xdict-size 100%</code> (default) || 450s || 43% | | <code>xz -Xdict-size 100%</code> (default) || 450s || 43% | ||
|} | |} | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> | ||
| Line 188: | Line 154: | ||
</div> | </div> | ||
如果你并不在意文件大小,可以在你的<code>iso.nix</code>中添加如下内容以使用更快的压缩方式: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 202: | Line 165: | ||
== 另见 == | == 另见 == | ||
* [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 | |||
[[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% |
See also: mksquashfs benchmarks
如果你并不在意文件大小,可以在你的iso.nix中添加如下内容以使用更快的压缩方式:
{
isoImage.squashfsCompression = "gzip -Xcompression-level 1";
}