NixOS安裝指南

Revision as of 13:36, 23 February 2025 by Mayer (talk | contribs) (Created page with "这些方式也同样有记录, 它们可以使用USB驱动器启动多个发行版. 这种方式不被支持, 结果可能因人而异.")

這份指南是對官方手冊的一份參考指南. 它講述了把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.

Alternative installation media instructions

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

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

Booting the installation media

Since the installation media is hybrid, it will boot both in legacy bios mode and UEFI mode.

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.

Legacy bios boot

This is the only boot possible on machines lacking EFI/UEFI.

UEFI boot

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.

Connecting to the internet

The installation will definitely need a working internet connection. It is possible to install without one, but the available set of packages is limited.

Wired

For network interfaces supported by the kernel, DHCP resolution should already have happened once the shell is available.

Tethered (Internet Sharing)

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.

Wireless

Network Manager is installed on the graphical ISO, meaning that it is possible to use nmtui on the command line to connect to a network.

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.

Partitioning

To partition the persistent storage run sudo fdisk /dev/diskX and follow instructions for DOS or (U)EFI. A very simple example setup is given here.

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 disk label)
  • n
  • 1 (partition number [1/128])
  • 2048 first sector
  • +500M last sector (boot sector size)
  • t
  • 1 (EFI System)
  • n
  • 2
  • default (fill up partition)
  • default (fill up partition)
  • w (write)

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.