Jump to content

NixOS Installation Guide/zh: Difference between revisions

From NixOS Wiki
Mayer (talk | contribs)
Created page with "=== UEFI ==="
Mayer (talk | contribs)
Created page with "* g (gpt硬盘标签) * n * 1 (分区编号[1/128]) * 2048 first sector * +500M last sector (引导扇区的大小) * t * 1 (EFI系统) * n * 2 * default (填满分区) * default (填满分区) * w (写入)"
Line 152: Line 152:
=== UEFI ===
=== UEFI ===


<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>


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

Revision as of 14:17, 23 February 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. Find the right device with diskutil list, let's say diskX.
  2. Unmount with diskutil unmountDisk diskX.
  3. Burn with: sudo dd if=path_to_nixos.iso of=/dev/diskX

在Windows上

  1. Download USBwriter.
  2. Start USBwriter.
  3. Choose the downloaded ISO as 'Source'
  4. Choose the USB drive as 'Target'
  5. Click 'Write'
  6. When USBwriter has finished writing, safely unplug the USB drive.

可选的安装媒介指导

之前的方式是制作USB安装媒介的受支持方式.

这些方式也同样有记录, 它们可以使用USB驱动器启动多个发行版. 这种方式不被支持, 结果可能因人而异.

启动安装媒介

因为安装媒介是混合型的, 它通过legacy bios模式和UEFI模式都可以启动.

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

Legacy bios启动

这是在没有EFI/UEFI的机器上唯一的启动方式.

UEFI启动

安装媒介的EFI引导加载程序未经过签名, 也没有使用经过签名的shim来启动. 这意味着只有禁用了Secure Boot(安全启动)才能启动.

连接到网络

安装过程几乎肯定需要有效的网络连接. 无网络的安装是可行的, 但是可用的软件包集合是受限的.

有线网络

对于内核提供的网络接口, DHCP(动态主机配置协议)解析应当在shell可用之前就已经完成.

系留式(网络共享)

如果你无法通过网线或者wifi连接到网络, 你可以使用智能手机的网络共享能力. 依赖于你的手机的能力, 只需要内核自带的驱动就可以提供有效的网络连接.

无线网络

Network Manager安装在图形界面ISO文件中, 意味着可以在命令行中使用nmtui来连接到网络.

Using the "Applications" tab at top left or the launcher bar at bottom, choose a terminal application and from there launch nmtui. 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.

On the minimal ISO, or if you are more familiar with wpa_supplicant then you can also run wpa_passphrase ESSID | sudo tee /etc/wpa_supplicant.conf, then enter your password and systemctl restart wpa_supplicant.

分区

为了给连续存储进行分区, 运行 sudo fdisk /dev/diskX , 然后跟着针对DOS或(U)EFI的指示来做. 这里有一个非常简单的示例设置.

DOS

  • o (dos disk label)
  • n new
  • p primary (4 primary in total)
  • 1 (partition number [1/4])
  • 2048 first sector (alignment for performance)
  • +500M last sector (boot sector size)
  • rm signature (Y), if ex. => warning of overwriting existing system, could use wipefs
  • n
  • p
  • 2
  • default (fill up partition)
  • default (fill up partition)
  • w (write)

UEFI

  • g (gpt硬盘标签)
  • n
  • 1 (分区编号[1/128])
  • 2048 first sector
  • +500M last sector (引导扇区的大小)
  • t
  • 1 (EFI系统)
  • n
  • 2
  • default (填满分区)
  • default (填满分区)
  • w (写入)

Label partitions

This is useful for having multiple setups and makes partitions easier to handle
$ 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

Swap file

$ 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

NixOS config

$ sudo nixos-generate-config --root /mnt
$ cd /mnt/etc/nixos/
$ sudo vim configuration.nix
Most essential changes:
  • keyboard layout, ie services.xserver.xkb.layout
  • users.users.user with adding entry initialPassword = "pw123";
  • networking (wifi), see below for fix if it breaks
  • boot.loader.grub.device = "/dev/sda"; #or "nodev" for efi only
  • install editor to edit the configuration
  • change hardware config to use labels

The self-documenting NixOS options can be searched with NixOS options search.

NixOS installation

$ cd /mnt
$ sudo nixos-install
after installation: Run passwd to change user password.
if internet broke/breaks, try one of the following:
$ nixos-rebuild switch --option substitute false # no downloads
$ nixos-rebuild switch --option binary-caches "" # no downloads
  • wpa_supplicant flags to connect to wifi

Additional notes for specific hardware

These are collected notes or links for specific hardware issues.

  • Blog post how to install NixOS on a Dell 9560
  • Brand servers may require extra kernel modules be included into initrd (boot.initrd.extraKernelModules in configuration.nix) For example HP Proliant needs "hpsa" module to see the disk drive.