NixOS Installation Guide/zh: Difference between revisions

Mayer (talk | contribs)
Created page with "这些方式也同样有记录, 它们可以使用USB驱动器启动多个发行版. 这种方式不被支持, 结果可能因人而异."
Ardenet (talk | contribs)
No edit summary
 
(85 intermediate revisions by 4 users not shown)
Line 1: Line 1:
这份指南是[https://nixos.org/nixos/manual/index.html#ch-installation 官方手册]一份参考指南. 述了[[NixOS]]为完整操作系统的安装过程. 为了一个已存在的操作系统安装[[Nix]], [[Nix Installation Guide|Nix安装指南]].
<languages/>
指南是官方手册 [https://nixos.org/nixos/manual/index.html#ch-installation] 配套指南述了如何将 [[Special:MyLanguage/NixOS|NixOS]] 安装为完整操作系统。有关现有操作系统安装 [[Special:MyLanguage/Nix|Nix]] 的说明,参阅 [[Special:MyLanguage/Nix Installation Guide|Nix 安装指南]]


除了讲述官方手册的步骤, 它还提供了用于常见使场景的已知良好指导. 当官方手册和这份指南之间出现矛盾, 官方手册中描述的情况为准.
除了涵盖官方手册的步骤外,它还提供了针对常见用的已知有效操作。当官方手册与本指南之间存在差异,支持用例以手册中描述的为准
 
把这份指南当作逐步操作指南使用. 出现不同选择时, 只使用选定的部分, 并在这部分的结尾按照它的指示前进.


<span id="Installation_target"></span>
<span id="Installation_target"></span>
Line 25: Line 24:
== 制作安装媒介 ==
== 制作安装媒介 ==


从NixOS 14.11开始, 安装程序ISO(镜像文件)是混合的. 这意味着它通过CD和USB驱动器都可以启动. 它也可以在EFI系统上启动, 如大多数现代主板和苹果系统. 接下来的指导将假设复制镜像文件到USB驱动器的标准方式. 当使用CD或者DVD时, 刻录到光盘的通常方式应当也适用于该iso文件.
从NixOS 14.11开始, 安装程序ISO(镜像文件)是混合的. 这意味着它通过CD和USB驱动器都可以启动. 它也可以在EFI系统上启动, 如大多数现代主板和苹果系统. 接下来的指导将假设使用复制镜像文件到USB驱动器的标准方式. 当使用CD或者DVD时, 刻录到光盘的通常方式应当也适用于该iso文件.


<span id="&quot;Burning&quot;_to_USB_drive"></span>
<span id="&quot;Burning&quot;_to_USB_drive"></span>
Line 38: Line 37:
# 复制到设备: <code>cp nixos-xxx.iso <em>/dev/sdX</em></code>
# 复制到设备: <code>cp nixos-xxx.iso <em>/dev/sdX</em></code>


{{note|不要使用 /dev/sdX1 或硬盘的分区, 使用整块硬盘 /dev/sdX}}
{{note|不要使用 /dev/sdX1 或硬盘的分区, 使用整块硬盘 /dev/sdX}}


也可通过 <code>dd if=nixos.iso of=/dev/sdX bs=4M status=progress conv=fdatasync</code> 写入硬盘镜像
也可通过 <code>dd if=nixos.iso of=/dev/sdX bs=4M status=progress conv=fdatasync</code> 写入硬盘镜像
Line 45: Line 44:
==== 在macOS上 ====
==== 在macOS上 ====


<div lang="en" dir="ltr" class="mw-content-ltr">
# 使用<code>diskutil list</code>找到正确的设备, 比如说<code><i>diskX</i></code>.
# Find the right device with <code>diskutil list</code>, let's say <code><i>diskX</i></code>.
# 使用<code>diskutil unmountDisk <i>diskX</i></code>取消挂载.
# Unmount with <code>diskutil unmountDisk <i>diskX</i></code>.
# 使用<code>sudo dd if=<b>path_to_nixos.iso</b> of=/dev/<i>diskX</i></code>进行刻录
# Burn with: <code>sudo dd if=<b>path_to_nixos.iso</b> of=/dev/<i>diskX</i></code>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{tip|使用<code>rdiskX</code>而不是<code>diskX</code>可以大幅改变速度. 你可以在另一个终端里使用<code>iostat 2</code>检查写入速度.}}
{{tip|Using <code>rdiskX</code> instead of <code>diskX</code> can makes a large speed difference. You can check the write speed with <code>iostat 2</code> in another terminal.}}
</div>


<span id="From_Windows"></span>
<span id="From_Windows"></span>
==== 在Windows上====
==== 在Windows上====


<div lang="en" dir="ltr" class="mw-content-ltr">
# 下载USBwriter.
# Download [http://sourceforge.net/projects/usbwriter/ USBwriter].
# 启动USBwriter.
# Start USBwriter.
# 选择下载的ISO文件作为'Source'
# Choose the downloaded ISO as 'Source'
# 选择USB驱动器作为'Target'
# Choose the USB drive as 'Target'
# 点击'Write'
# Click 'Write'
# 当USBWriter完成写入操作后, 安全拔出USB驱动器
# When USBwriter has finished writing, safely unplug the USB drive.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Alternative_installation_media_instructions"></span>
=== Alternative installation media instructions ===
=== 可选的安装媒介指导 ===
</div>


之前的方式是制作USB安装媒介的受支持方式.
之前的方式是制作USB安装媒介的受支持方式.
Line 75: Line 67:
这些方式也同样有记录, 它们可以使用USB驱动器启动多个发行版. 这种方式不被支持, 结果可能因人而异.
这些方式也同样有记录, 它们可以使用USB驱动器启动多个发行版. 这种方式不被支持, 结果可能因人而异.


<div lang="en" dir="ltr" class="mw-content-ltr">
* [[NixOS_Installation_Guide/Unetbootin|使用 Unetbootin]]
* [[NixOS_Installation_Guide/Unetbootin|Using Unetbootin]]
* [[NixOS_Installation_Guide/Manual USB Creation|手动 USB 创建]]
* [[NixOS_Installation_Guide/Manual USB Creation|Manual USB Creation]]
* [[NixOS_Installation_Guide/multibootusb|multibootusb]]
* [[NixOS_Installation_Guide/multibootusb|multibootusb]]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
== 启动(Boot)安装媒介 ==
== Booting the installation media ==
{{expansion|Troubleshooting steps, and details are lacking.}}
{{expansion|Troubleshooting steps, and details are lacking.}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
由于安装镜像为混合镜像,因此可在传统 BIOS 模式或 [[Special:MyLanguage/UEFI|UEFI]] 模式下启动。
Since the installation media is hybrid, it will boot both in legacy bios mode and UEFI mode.
 
</div>
无论使用哪种方式启动安装媒介, 你可能需要修改主板或者电脑的配置, 以允许从光盘驱动器(对于CD/DVD)或者从一个外部的USB驱动器启动.
 
<span id="Legacy_bios_boot"></span>
=== Legacy bios启动 ===


<div lang="en" dir="ltr" class="mw-content-ltr">
这是在没有EFI/UEFI的机器上唯一的启动方式.
Whatever mode is used to boot the installation media, your motherboard or computer's configuration may need to be changed to allow booting from a Optical Disk Drive (for CD/DVD) or an external USB drive.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="UEFI_boot"></span>
=== Legacy bios boot ===
=== UEFI启动 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
安装媒介的EFI引导加载程序未经过签名, 也没有使用经过签名的shim来启动. 这意味着只有禁用了Secure Boot(安全启动)才能启动.
This is the only boot possible on machines lacking EFI/UEFI.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Connecting_to_the_internet"></span>
=== UEFI boot ===
== 连接到网络 ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
安装过程'''几乎肯定'''需要有效的网络连接. 无网络的安装是可行的, 但是可用的软件包集合是受限的.
The EFI bootloader of the installation media is not signed and is not using a signed shim to boot. This means that Secure Boot will need to be disabled to boot.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Wired"></span>
== Connecting to the internet ==
=== 有线网络 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
对于内核提供的网络接口, DHCP(动态主机配置协议)解析应当在shell可用之前就已经完成.
The installation will '''definitely''' need a working internet connection. It is possible to install without one, but the available set of packages is limited.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Tethered_(Internet_Sharing)"></span>
=== Wired ===
==有线连接(网络共享)==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果你无法通过网线或者wifi连接到网络, 你可以使用智能手机的网络共享能力. 依赖于你的手机的能力, 只需要内核自带的驱动就可以提供有效的网络连接.
For network interfaces supported by the kernel, DHCP resolution should already have happened once the shell is available.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Wireless"></span>
==Tethered (Internet Sharing)==
=== 无线网络 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Special:MyLanguage/NetworkManager|NetworkManager]] 安装在图形化 ISO 镜像上,这意味着可以在命令行上使用 <code>nmtui</code> 命令连接到网络。
If you can not connect to the internet via cable or wifi, you may use smartphone's tethering capability to share internet. Depending on your smartphones capabilities, only stock kernel drivers may be required which can help providing a working network connection.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
使用左上方的"Applications(应用)"标签页或者在底部的启动栏, 打开一个终端应用, 在那里启动 <code>nmtui</code>. 这可以让你'activate(激活)'一个(无线)连接 - 你应该可以在列表里看到你那里的SSID(无线网络名称), 除此之外你还可以添加一个新的连接. 当无线连接处于活动状态, 并且你已经测试过了时, 一开始就启动的安装程序很可能还没有检测到新的连接. 关掉安装程序, 再重新从屏幕底部的启动栏打开它.  这时它应该会检测到新的网络连接而继续.
=== Wireless ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
在最小化 ISO 镜像上,或者您更熟悉 [[Special:MyLanguage/wpa_supplicant|wpa_supplicant]],那么您也可以运行 <code>wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf</code> 命令,然后输入您的密码并执行 <code>systemctl restart wpa_supplicant</code> 命令。
Network Manager is installed on the graphical ISO, meaning that it is possible to use <code>nmtui</code> on the command line to connect to a network.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Partitioning"></span>
Using the "Applications" tab at top
== Partitioning(分区操作) ==
left or the launcher bar at bottom, choose a terminal application and from there launch <code>nmtui</code>. This will allow you to 'activate' a (wireless) connection - your local SSIDs should be visible in the list, else you can add a new connection.  When the wireless connection is active and you have tested it, it is likely the install app which launched on startup has not detected the new connection.  Close down the install app, and reopen it from the launcher bar at the bottom of the screen.  This should then find the new connection and proceed.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
要对持久存储进行分区,请运行 <code>sudo fdisk /dev/diskX</code> 并按照 MBR 或 (U)EFI 的说明进行操作。要确定您使用的启动模式,请运行:
On the minimal ISO, or if you are more familiar with <code>wpa_supplicant</code> then you can also run <code>wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf</code>, then enter your password and <code>systemctl restart wpa_supplicant</code>.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
== Partitioning ==
$ [ -d /sys/firmware/efi/efivars ] && echo "UEFI" || echo "Legacy"
</div>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
这里给出了一个非常简单的示例设置。
To partition the persistent storage run <code>sudo fdisk /dev/diskX</code> and follow instructions for DOS or (U)EFI.
A very simple example setup is given here.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Legacy_Boot_(MBR)"></span>
=== DOS ===
=== 传统启动(Legacy Boot, MBR)===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* o (dos硬盘标签)
* o (dos disk label)
* n new
* n new
* p primary (4 primary in total)
* p primary (4 primary in total)
* 1 (partition number [1/4])
* 1 (分区编号[1/4])
* 2048 first sector (alignment for performance)
* 2048 first sector (alignment for performance)
* +500M last sector (boot sector size)
* +500M last sector (引导扇区的大小)
* rm signature (Y), if ex. => warning of overwriting existing system, could use wipefs
* rm signature (Y), if ex. => warning of overwriting existing system, could use wipefs
* n
* n
* p
* p
* 2
* 2
* default (fill up partition)
* default (填满分区)
* default (fill up partition)
* default (填满分区)
* w (write)
* w (写入)
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
=== UEFI ===
=== UEFI ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* g (gpt硬盘标签)
* g (gpt disk label)
* n
* n
* 1 (partition number [1/128])
* 1 (分区编号[1/128])
* 2048 first sector
* 2048 first sector
* +500M last sector (boot sector size)
* +500M last sector (引导扇区的大小)
* t
* t
* 1 (EFI System)
* 1 (EFI系统)
* n
* n
* 2
* 2
* default (fill up partition)
* default (填满分区)
* default (fill up partition)
* default (填满分区)
* w (write)
* w (写入)
</div>
 
<span id="Format_partitions"></span>
=== 格式化分区 ===
 
以下示例使用 [[Special:MyLanguage/ext4|ext4]] 文件系统格式。如果您希望使用其他文件系统格式,例如 [[Special:MyLanguage/Btrfs|Btrfs]] 或 [[Special:MyLanguage/ZFS|ZFS]]:


<div lang="en" dir="ltr" class="mw-content-ltr">
* [[Special:MyLanguage/Bcachefs#NixOS installation on bcachefs|在 bcachefs 上安装 NixOS]]
== Label partitions ==
* [[Special:MyLanguage/Btrfs#Installation of NixOS on btrfs|在 btrfs 上安装 NixOS]]
</div>
* [[Special:MyLanguage/LVM#Basic Setup|LVM 基础设置]]
* [[Special:MyLanguage/ZFS#Simple NixOS ZFS on root installation|Simple NixOS ZFS on root installation]]


<div lang="en" dir="ltr" class="mw-content-ltr">
这对于有多个分区设置的情况很有用, 使分区更容易处理
This is useful for having multiple setups and makes partitions easier to handle<syntaxhighlight lang="console">
 
$ lsblk
<syntaxhighlight lang="console">
$ sudo mkfs.fat -F 32 /dev/sdX1
$ lsblk # lists current system block devices
$ sudo fatlabel /dev/sdX1 NIXBOOT
# mkfs.fat -F 32 -n boot /dev/sdX1
$ sudo mkfs.ext4 /dev/sdX2 -L NIXROOT
# mkfs.ext4 /dev/sdX2 -L nixos
$ sudo mount /dev/disk/by-label/NIXROOT /mnt
# mount /dev/disk/by-label/nixos /mnt
$ sudo mkdir -p /mnt/boot
# mkdir -p /mnt/boot
$ sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="NixOS_configuration"></span>
== Swap file ==
== NixOS 配置 ==
 
NixOS 通过 [[Special:MyLanguage/Overview of the NixOS Linux distribution#Declarative Configuration|声明式配置]] 文件进行配置。要生成默认配置文件,请运行 [[Special:MyLanguage/nixos-generate-config]]:
 
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 (2GB size)
# nixos-generate-config --root /mnt
$ sudo chmod 600 /mnt/.swapfile
# nano /mnt/etc/nixos/configuration.nix
$ sudo mkswap /mnt/.swapfile
$ sudo swapon /mnt/.swapfile
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有关使用系统配置的信息,请参阅 [[Special:MyLanguage/NixOS system configuration|NixOS 系统配置]]。有关特定于桌面的配置,请参阅 [[Special:MyLanguage/NixOS as a desktop|桌面设备上的 NixOS]]。
== NixOS config ==
 
最重要的变化:
 
* 键盘布局(keyboard layout), 也就是 <code>[[Keyboard Layout Customization|services.xserver.xkb.layout]]</code>
* [[networking|网络]] (wifi), 若出现问题, 请参阅下方的修复方法
* 安装 [[:Category:Text Editor|编辑器]] 来编辑配置
 
具有自我说明性的NixOS选项可以在[https://search.nixos.org/options NixOS选项搜索]上搜索到.
 
<span id="Swap_file"></span>
== 交换文件 ==
 
有关配置交换空间的更多方法,请参阅 [[Swap]]。以下示例演示了如何创建和启用一个 [[Swap#Swap file|交换文件]]:
 
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  swapDevices = [{
    device = "/var/lib/swapfile";
    size = 16*1024; # 16 GB
  }];
</nowiki>
}}
 
<span id="Bootloader"></span>
=== 引导加载程序 ===
 
NixOS 支持多种 [[Bootloader|引导加载程序]],例如 [[GNU GRUB]] 和 [[Systemd/boot]]。
 
Systemd-boot 是推荐的引导加载程序。以下示例演示了如何在配置中启用 systemd-boot:
 
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  boot.loader.systemd-boot.enable = true;
</nowiki>
}}
 
您可能还希望配置 [[Secure Boot|安全启动]]。
 
<span id="Users"></span>
=== 用户 ===
 
有关创建和管理用户的信息,请参阅 [[User management|用户管理]] 和 {{NixOS Manual|name=NixOS Manual: Chapter - Package Management|anchor=#sec-user-management}}。请参阅以下示例:
 
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  users.users.alice = {
    isNormalUser = true;
    initialPassword = "pw123";
  };
</nowiki>
}}
 
<span id="NixOS_installation"></span>
== 安装 NixOS ==
 
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ sudo nixos-generate-config --root /mnt
# cd /mnt
$ cd /mnt/etc/nixos/
# nixos-install
$ sudo vim configuration.nix
</syntaxhighlight>
</syntaxhighlight>Most essential changes:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
安装后:运行<code>passwd</code>更改用户密码。
* keyboard layout, ie <code>[[Keyboard Layout Customization|services.xserver.xkb.layout]]</code>
* <code>users.users.user</code> with adding entry <code>initialPassword = "pw123";</code>
* [[networking]] (wifi), see below for fix if it breaks
* <code>boot.loader.grub.device = "/dev/sda"; #or "nodev" for efi only</code>
* install editor to edit the configuration
* change hardware config to use labels
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
如果网络出现问题, 请尝试下面的一种方法:
The self-documenting NixOS options can be searched with [https://search.nixos.org/options NixOS options search].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
== NixOS installation ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ cd /mnt
# nixos-rebuild switch --option substitute false # no downloads
$ sudo nixos-install
# nixos-rebuild switch --option binary-caches "" # no downloads
</syntaxhighlight>after installation: Run <code>passwd</code> to change user password.
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* 设置 wpa_supplicant 标志来连接到 wifi
if internet broke/breaks, try one of the following:<syntaxhighlight lang="console">
$ nixos-rebuild switch --option substitute false # no downloads
$ nixos-rebuild switch --option binary-caches "" # no downloads
</syntaxhighlight>
* wpa_supplicant flags to connect to wifi
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<hr />
<hr />
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Additional_notes_for_specific_hardware"></span>
== Additional notes for specific hardware ==
== 有关特定硬件的附加说明 ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
这些是有关特定硬件问题的收集到的说明或链接.
These are collected notes or links for specific hardware issues.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* 博客文章:如何在 [http://grahamc.com/blog/nixos-on-dell-9560 Dell 9560] 上安装 NixOS
* Blog post how to install NixOS on a [http://grahamc.com/blog/nixos-on-dell-9560 Dell 9560]
* 品牌服务器可能需要在 initrd 中包含额外的内核模块(在 configuration.nix 中配置 <code>boot.initrd.extraKernelModules</code>)。例如,HP Proliant 需要“hpsa”模块才能访问磁盘驱动器。
* Brand servers may require extra kernel modules be included into initrd (<code>boot.initrd.extraKernelModules</code> in configuration.nix) For example HP Proliant needs "hpsa" module to see the disk drive.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Category:Guide|分类:指南]]
[[Category:Guide]][[Category:Deployment]]
[[Category:Deployment|分类:开发]]
</div>
[[Category:NixOS|分类:NixOS]]