Jump to content

NixOS Installation Guide/zh: Difference between revisions

From NixOS Wiki
Ardenet (talk | contribs)
Created page with "* 博客文章:如何在 [http://grahamc.com/blog/nixos-on-dell-9560 Dell 9560] 上安装 NixOS * 品牌服务器可能需要在 initrd 中包含额外的内核模块(在 configuration.nix 中配置 <code>boot.initrd.extraKernelModules</code>)。例如,HP Proliant 需要“hpsa”模块才能访问磁盘驱动器。"
FuzzyBot (talk | contribs)
Updating to match new version of source page
Line 1: Line 1:
<languages/>
<div class="mw-translate-fuzzy">
这份指南是对[https://nixos.org/nixos/manual/index.html#ch-installation 官方手册]的一份参考指南. 它讲述了把[[NixOS]]作为完整操作系统的安装过程. 为了在一个已存在的操作系统内安装[[Nix]], 请见[[Nix Installation Guide|Nix安装指南]].
这份指南是对[https://nixos.org/nixos/manual/index.html#ch-installation 官方手册]的一份参考指南. 它讲述了把[[NixOS]]作为完整操作系统的安装过程. 为了在一个已存在的操作系统内安装[[Nix]], 请见[[Nix Installation Guide|Nix安装指南]].
</div>


<div class="mw-translate-fuzzy">
除了讲述官方手册上的步骤, 它还提供了用于常见使用场景的已知良好指导. 当官方手册和这份指南之间出现矛盾时, 以官方手册中描述的情况为准.
除了讲述官方手册上的步骤, 它还提供了用于常见使用场景的已知良好指导. 当官方手册和这份指南之间出现矛盾时, 以官方手册中描述的情况为准.
 
</div>
把这份指南当作逐步操作指南使用. 出现不同选择时, 只使用选定的部分, 并在这部分的结尾按照它的指示前进.


<span id="Installation_target"></span>
<span id="Installation_target"></span>
Line 75: Line 78:
{{expansion|Troubleshooting steps, and details are lacking.}}
{{expansion|Troubleshooting steps, and details are lacking.}}


<div class="mw-translate-fuzzy">
因为安装媒介是混合型的, 它通过legacy bios模式和UEFI模式都可以启动.
因为安装媒介是混合型的, 它通过legacy bios模式和UEFI模式都可以启动.
</div>


无论使用哪种方式启动安装媒介, 你可能需要修改主板或者电脑的配置, 以允许从光盘驱动器(对于CD/DVD)或者从一个外部的USB驱动器启动.
无论使用哪种方式启动安装媒介, 你可能需要修改主板或者电脑的配置, 以允许从光盘驱动器(对于CD/DVD)或者从一个外部的USB驱动器启动.
Line 107: Line 112:
=== 无线网络 ===
=== 无线网络 ===


<div class="mw-translate-fuzzy">
Network Manager安装在图形界面ISO文件中, 这意味着可以在命令行中使用<code>nmtui</code>来连接到网络.
Network Manager安装在图形界面ISO文件中, 这意味着可以在命令行中使用<code>nmtui</code>来连接到网络.
</div>


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


<div class="mw-translate-fuzzy">
在最小化安装的ISO文件上(或者你更熟悉 <code>wpa_supplicant</code> ), 你也可以运行 <code>wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf</code>, 然后输入你的密码, 再运行<code>systemctl restart wpa_supplicant</code>.
在最小化安装的ISO文件上(或者你更熟悉 <code>wpa_supplicant</code> ), 你也可以运行 <code>wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf</code>, 然后输入你的密码, 再运行<code>systemctl restart wpa_supplicant</code>.
</div>


<span id="Partitioning"></span>
<span id="Partitioning"></span>
== Partitioning(分区操作) ==
== Partitioning(分区操作) ==


<div class="mw-translate-fuzzy">
为了给连续存储进行分区, 运行 <code>sudo fdisk /dev/diskX</code> , 然后跟着针对DOS或(U)EFI的指示来做.
为了给连续存储进行分区, 运行 <code>sudo fdisk /dev/diskX</code> , 然后跟着针对DOS或(U)EFI的指示来做.
这里有一个非常简单的示例设置.
这里有一个非常简单的示例设置.
</div>
<syntaxhighlight lang="console">
$ [ -d /sys/firmware/efi/efivars ] && echo "UEFI" || echo "Legacy"
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
A very simple example setup is given here.
</div>


<span id="Legacy_Boot_(MBR)"></span>
<div class="mw-translate-fuzzy">
=== DOS ===
=== DOS ===
</div>


* o (dos硬盘标签)
* o (dos硬盘标签)
Line 150: Line 172:
* w (写入)
* w (写入)


<span id="Label_partitions"></span>
<span id="Format_partitions"></span>
<div class="mw-translate-fuzzy">
== Label partitions(给分区贴标签) ==
== Label partitions(给分区贴标签) ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
The example below uses the [[ext4]] filesystem format. If you wish to use other filesystem formats such as [[Btrfs]] or [[ZFS]]:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* [[Bcachefs#NixOS installation on bcachefs]]
* [[Btrfs#Installation of NixOS on btrfs]]
* [[LVM#Basic Setup]]
* [[ZFS#Simple NixOS ZFS on root installation]]
</div>
<div class="mw-translate-fuzzy">
这对于有多个分区设置的情况很有用, 使分区更容易处理<syntaxhighlight lang="console">
这对于有多个分区设置的情况很有用, 使分区更容易处理<syntaxhighlight lang="console">
$ lsblk
$ lsblk
Line 162: Line 198:
$ sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot
$ sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot
</syntaxhighlight>
</syntaxhighlight>
</div>


== Swap文件 ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 (2GB size)
$ lsblk # lists current system block devices
$ sudo chmod 600 /mnt/.swapfile
# mkfs.fat -F 32 -n boot /dev/sdX1
$ sudo mkswap /mnt/.swapfile
# mkfs.ext4 /dev/sdX2 -L nixos
$ sudo swapon /mnt/.swapfile
# mount /dev/disk/by-label/nixos /mnt
# mkdir -p /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot
</syntaxhighlight>
</syntaxhighlight>


<span id="NixOS_configuration"></span>
<div class="mw-translate-fuzzy">
== NixOS配置 ==
== NixOS配置 ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 177: Line 217:
$ sudo vim configuration.nix
$ sudo vim configuration.nix
</syntaxhighlight>最重要的修改:
</syntaxhighlight>最重要的修改:
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS is configured through a [[Overview of the NixOS Linux distribution#Declarative Configuration|declarative configuration]] file. To generate a default config file, run [[nixos-generate-config]]:
</div>
<syntaxhighlight lang="console">
# nixos-generate-config --root /mnt
# nano /mnt/etc/nixos/configuration.nix
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
For information on working with a system configuration, see [[NixOS system configuration]]. For desktop-specific configurations, see [[NixOS as a desktop]].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Most essential changes:
</div>
<div class="mw-translate-fuzzy">
* keyboard layout(键盘布局), 也就是<code>[[Keyboard Layout Customization|services.xserver.xkb.layout]]</code>  
* keyboard layout(键盘布局), 也就是<code>[[Keyboard Layout Customization|services.xserver.xkb.layout]]</code>  
* <code>users.users.user</code> 和附加条目 <code>initialPassword = "pw123";</code>
* <code>users.users.user</code> 和附加条目 <code>initialPassword = "pw123";</code>
Line 184: Line 243:
* 安装编辑器来编辑配置文件
* 安装编辑器来编辑配置文件
* 修改硬件配置以使用标签
* 修改硬件配置以使用标签
</div>


具有自我说明性的NixOS选项可以在[https://search.nixos.org/options NixOS选项搜索]上搜索到.
具有自我说明性的NixOS选项可以在[https://search.nixos.org/options NixOS选项搜索]上搜索到.


<span id="Swap_file"></span>
<div class="mw-translate-fuzzy">
== Swap文件 ==
<syntaxhighlight lang="console">
$ sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 (2GB size)
$ sudo chmod 600 /mnt/.swapfile
$ sudo mkswap /mnt/.swapfile
$ sudo swapon /mnt/.swapfile
</syntaxhighlight>
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
For additional methods of configuring swap, see [[Swap]]. The following example demonstrates how to create and enable a [[Swap#Swap file|swap file]]:
</div>
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  swapDevices = [{
    device = "/var/lib/swapfile";
    size = 16*1024; # 16 GB
  }];
</nowiki>
}}
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Bootloader ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS supports multiple [[Bootloader|bootloaders]] such as [[GNU GRUB]] and [[Systemd/boot]].
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
Systemd-boot is the recommended bootloader. The following example demonstrates how to enable systemd-boot in your configuration:
</div>
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  boot.loader.systemd-boot.enable = true;
</nowiki>
}}
<div lang="en" dir="ltr" class="mw-content-ltr">
You may also wish to configure [[Secure Boot]].
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Users ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
For information on creating and managing users, see [[User management]] and the {{NixOS Manual|name=NixOS Manual: Chapter - Package Management|anchor=#sec-user-management}}. See an example below:
</div>
{{file|/mnt/etc/nixos/configuration.nix|nix|
<nowiki>
  users.users.alice = {
    isNormalUser = true;
    initialPassword = "pw123";
  };
</nowiki>
}}
<span id="NixOS_installation"></span>
<div class="mw-translate-fuzzy">
== NixOS安装 ==
== NixOS安装 ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 192: Line 317:
$ sudo nixos-install
$ sudo nixos-install
</syntaxhighlight>安装完成后: 运行 <code>passwd</code> 来修改用户的密码.
</syntaxhighlight>安装完成后: 运行 <code>passwd</code> 来修改用户的密码.
</div>
<syntaxhighlight lang="console">
# cd /mnt
# nixos-install
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
after installation: Run <code>passwd</code> to change user password.
</div>


<div class="mw-translate-fuzzy">
如果网络出现问题, 请尝试下面的一种方法:<syntaxhighlight lang="console">
如果网络出现问题, 请尝试下面的一种方法:<syntaxhighlight lang="console">
$ nixos-rebuild switch --option substitute false # 无下载
$ nixos-rebuild switch --option substitute false # 无下载
Line 198: Line 334:
</syntaxhighlight>
</syntaxhighlight>
* 设置wpa_supplicant标志来连接到wifi.
* 设置wpa_supplicant标志来连接到wifi.
</div>
<syntaxhighlight lang="console">
# nixos-rebuild switch --option substitute false # no downloads
# nixos-rebuild switch --option binary-caches "" # no downloads
</syntaxhighlight>
<div class="mw-translate-fuzzy">
<hr />
</div>


<hr />
<hr />
Line 210: Line 356:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
[[Category:Guide]][[Category:Deployment]]
[[Category:Guide]]
[[Category:Deployment]]
[[Category:NixOS]]
</div>
</div>

Revision as of 06:23, 8 October 2025

這份指南是對官方手冊的一份參考指南. 它講述了把NixOS作為完整作業系統的安裝過程. 為了在一個已存在的作業系統內安裝Nix, 請見Nix安裝指南.

除了講述官方手冊上的步驟, 它還提供了用於常見使用場景的已知良好指導. 當官方手冊和這份指南之間出現矛盾時, 以官方手冊中描述的情況為準.

安裝目標

NixOS可以安裝在越來越多種類的硬件上:

  • 常規(Intel或AMD)台式機, 筆記本電腦, 或是物理可訪問的伺服器, 該頁面包含了它們
  • 單板計算機(如樹莓派)和其他ARM開發板, 見 NixOS on ARM
  • 雲伺服器和遠程伺服器, 見 NixOS friendly hosters

安裝方式

NixOS, 如同絕大部分基於Linux的作業系統一樣, 可以通過多種方式進行安裝.

  1. 傳統方式, 通過安裝媒介啟動.(會在下面講述.)
  2. 通過已存在的Linux安裝啟動媒介

製作安裝媒介

從NixOS 14.11開始, 安裝程序ISO(鏡像文件)是混合型的. 這意味着它通過CD和USB驅動器都可以啟動. 它也可以在EFI系統上啟動, 如大多數現代主板和蘋果系統. 接下來的指導將假設使用複製鏡像文件到USB驅動器的標準方式. 當使用CD或者DVD時, 刻錄到光盤的通常方式應當也適用於該iso文件.

"刻錄"到USB驅動器

首先, 下載一個NixOS ISO鏡像 或者 創建一個自定義ISO文件. 接着插入一個空間大到足夠容納鏡像的USB. 然後遵循平台的指令:

在Linux上

  1. 通過 lsblkfdisk -l 找到正確的設備. 在接下來的步驟中, 把/dev/sdX 替換為正確的設備.
  2. 複製到設備: cp nixos-xxx.iso /dev/sdX
Note: 不要使用 /dev/sdX1 或硬盤的分區, 請使用整塊硬盤 /dev/sdX

也可通過 dd if=nixos.iso of=/dev/sdX bs=4M status=progress conv=fdatasync 寫入硬盤鏡像

在macOS上

  1. 使用diskutil list找到正確的設備, 比如說diskX.
  2. 使用diskutil unmountDisk diskX取消掛載.
  3. 使用sudo dd if=path_to_nixos.iso of=/dev/diskX進行刻錄
🟆︎
Tip: 使用rdiskX而不是diskX可以大幅改變速度. 你可以在另一個終端里使用iostat 2檢查寫入速度.

在Windows上

  1. 下載USBwriter.
  2. 啟動USBwriter.
  3. 選擇下載的ISO文件作為'Source'
  4. 選擇USB驅動器作為'Target'
  5. 點擊'Write'
  6. 當USBWriter完成寫入操作後, 安全拔出USB驅動器

可選的安裝媒介指導

之前的方式是製作USB安裝媒介的受支持方式.

這些方式也同樣有記錄, 它們可以使用USB驅動器啟動多個發行版. 這種方式不被支持, 結果可能因人而異.

啟動(Boot)安裝媒介

☶︎
This article or section needs to be expanded. Further information may be found in the related discussion page. Please consult the pedia article metapage for guidelines on contributing.

因為安裝媒介是混合型的, 它通過legacy bios模式和UEFI模式都可以啟動.

無論使用哪種方式啟動安裝媒介, 你可能需要修改主板或者電腦的配置, 以允許從光盤驅動器(對於CD/DVD)或者從一個外部的USB驅動器啟動.

Legacy bios啟動

這是在沒有EFI/UEFI的機器上唯一的啟動方式.

UEFI啟動

安裝媒介的EFI引導加載程序未經過簽名, 也沒有使用經過簽名的shim來啟動. 這意味着只有禁用了Secure Boot(安全啟動)才能啟動.

連接到網絡

安裝過程幾乎肯定需要有效的網絡連接. 無網絡的安裝是可行的, 但是可用的軟件包集合是受限的.

有線網絡

對於內核提供的網絡接口, DHCP(動態主機配置協議)解析應當在shell可用之前就已經完成.

有線連接(網絡共享)

如果你無法通過網線或者wifi連接到網絡, 你可以使用智能手機的網絡共享能力. 依賴於你的手機的能力, 只需要內核自帶的驅動就可以提供有效的網絡連接.

無線網絡

Network Manager安裝在圖形界面ISO文件中, 這意味着可以在命令行中使用nmtui來連接到網絡.

使用左上方的"Applications(應用)"標籤頁或者在底部的啟動欄, 打開一個終端應用, 在那裏啟動 nmtui. 這可以讓你'activate(激活)'一個(無線)連接 - 你應該可以在列表裏看到你那裏的SSID(無線網絡名稱), 除此之外你還可以添加一個新的連接. 當無線連接處於活動狀態, 並且你已經測試過了時, 一開始就啟動的安裝程序很可能還沒有檢測到新的連接. 關掉安裝程序, 再重新從屏幕底部的啟動欄打開它. 這時它應該會檢測到新的網絡連接而繼續.

在最小化安裝的ISO文件上(或者你更熟悉 wpa_supplicant ), 你也可以運行 wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf, 然後輸入你的密碼, 再運行systemctl restart wpa_supplicant.

Partitioning(分區操作)

為了給連續存儲進行分區, 運行 sudo fdisk /dev/diskX , 然後跟着針對DOS或(U)EFI的指示來做. 這裏有一個非常簡單的示例設置.

$ [ -d /sys/firmware/efi/efivars ] && echo "UEFI" || echo "Legacy"

A very simple example setup is given here.

DOS

  • o (dos硬盤標籤)
  • n new
  • p primary (4 primary in total)
  • 1 (分區編號[1/4])
  • 2048 first sector (alignment for performance)
  • +500M last sector (引導扇區的大小)
  • rm signature (Y), if ex. => warning of overwriting existing system, could use wipefs
  • n
  • p
  • 2
  • default (填滿分區)
  • default (填滿分區)
  • w (寫入)

UEFI

  • g (gpt硬盤標籤)
  • n
  • 1 (分區編號[1/128])
  • 2048 first sector
  • +500M last sector (引導扇區的大小)
  • t
  • 1 (EFI系統)
  • n
  • 2
  • default (填滿分區)
  • default (填滿分區)
  • w (寫入)

Label partitions(給分區貼標籤)

The example below uses the ext4 filesystem format. If you wish to use other filesystem formats such as Btrfs or ZFS:

這對於有多個分區設置的情況很有用, 使分區更容易處理
$ lsblk
$ sudo mkfs.fat -F 32 /dev/sdX1
$ sudo fatlabel /dev/sdX1 NIXBOOT
$ sudo mkfs.ext4 /dev/sdX2 -L NIXROOT
$ sudo mount /dev/disk/by-label/NIXROOT /mnt
$ sudo mkdir -p /mnt/boot
$ sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot
$ lsblk # lists current system block devices
# mkfs.fat -F 32 -n boot /dev/sdX1
# mkfs.ext4 /dev/sdX2 -L nixos
# mount /dev/disk/by-label/nixos /mnt
# mkdir -p /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot

NixOS配置

$ sudo nixos-generate-config --root /mnt
$ cd /mnt/etc/nixos/
$ sudo vim configuration.nix
最重要的修改:

NixOS is configured through a declarative configuration file. To generate a default config file, run nixos-generate-config:

# nixos-generate-config --root /mnt
# nano /mnt/etc/nixos/configuration.nix

For information on working with a system configuration, see NixOS system configuration. For desktop-specific configurations, see NixOS as a desktop.

Most essential changes:

  • keyboard layout(鍵盤布局), 也就是services.xserver.xkb.layout
  • users.users.user 和附加條目 initialPassword = "pw123";
  • networking (wifi), 如果出現問題, 請見下方的修複方法
  • boot.loader.grub.device = "/dev/sda"; #或者只用于efi的 "nodev"
  • 安裝編輯器來編輯配置文件
  • 修改硬件配置以使用標籤

具有自我說明性的NixOS選項可以在NixOS選項搜索上搜索到.

Swap文件

$ sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 (2GB size)
$ sudo chmod 600 /mnt/.swapfile
$ sudo mkswap /mnt/.swapfile
$ sudo swapon /mnt/.swapfile

For additional methods of configuring swap, see Swap. The following example demonstrates how to create and enable a swap file:

❄︎ /mnt/etc/nixos/configuration.nix
  swapDevices = [{
    device = "/var/lib/swapfile";
    size = 16*1024; # 16 GB
  }];

Bootloader

NixOS supports multiple bootloaders such as GNU GRUB and Systemd/boot.

Systemd-boot is the recommended bootloader. The following example demonstrates how to enable systemd-boot in your configuration:

❄︎ /mnt/etc/nixos/configuration.nix
  boot.loader.systemd-boot.enable = true;

You may also wish to configure Secure Boot.

Users

For information on creating and managing users, see User management and the NixOS Manual: Chapter - Package Management. See an example below:

❄︎ /mnt/etc/nixos/configuration.nix
  users.users.alice = {
    isNormalUser = true;
    initialPassword = "pw123";
  };

NixOS安裝

$ cd /mnt
$ sudo nixos-install
安裝完成後: 運行 passwd 來修改用戶的密碼.
# cd /mnt
# nixos-install

after installation: Run passwd to change user password.

如果網絡出現問題, 請嘗試下面的一種方法:
$ nixos-rebuild switch --option substitute false # 无下载
$ nixos-rebuild switch --option binary-caches "" # 无下载
  • 設置wpa_supplicant標誌來連接到wifi.
# nixos-rebuild switch --option substitute false # no downloads
# nixos-rebuild switch --option binary-caches "" # no downloads


有關特定硬件的附加說明

這些是有關特定硬件問題的收集到的說明或連結.

  • 博客文章:如何在 Dell 9560 上安裝 NixOS
  • 品牌伺服器可能需要在 initrd 中包含額外的內核模塊(在 configuration.nix 中配置 boot.initrd.extraKernelModules)。例如,HP Proliant 需要「hpsa」模塊才能訪問磁盤驅動器。