首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >从零开始安装 NixOS

从零开始安装 NixOS

原创
作者头像
OpenBit
修改2025-09-16 09:36:55
修改2025-09-16 09:36:55
1.2K0
举报

参考视频:

NixOS最小化安装 | 从零开始搭建你的NixOS系统(2)_哔哩哔哩_bilibili


引言

分享一个如何快速从零开始使用 NixOS 的方法。特别适合中文用户,涵盖了分区、安装、配置优化等关键步骤。结合了官方手册的推荐方法,并针对中文环境(如输入法、国内源)进行了优化。

从最小化 ISO 安装

Download | Nix & NixOS

下载 NixOS 25.05 最小化 ISO(64-bit Intel/AMD)。烧录到 USB 驱动器,使用工具如 Rufus 或 dd。引导时选择 UEFI 模式,并禁用 Secure Boot(如果启用)。

我将使用最小化 ISO安装,你也可以使用图形化安装程序。

加载 Live ISO

接下来的命令将决定 hardware.nix 的内容。

代码语言:bash
复制
sudo -i #切换到超级用户
lsblk
cfdisk /dev/sda #设置分区方案

# GPT 分区标签
# 1G,类型:EFI
# 4G,类型:swap
# 剩余空间,类型:Linux Filesystem

lsblk 检查一下分区,确认无误。我们可以看到刚刚创建的三个分区。现在来为这些分区创建文件系统。

NixOS 手册建议为分区设置标签,按照推荐的方式来做:

代码语言:bash
复制
mkfs.ext4 -L nixos /dev/sda3
mkswap -L swap /dev/sda2
mkfs.fat -F 32 -n boot /dev/sda1

文件系统创建好了,现在挂载它们:

代码语言:bash
复制
mount /dev/sda3 /mnt
mount --mkdir /dev/sda1 /mnt/boot
swapon /dev/sda2

示例输出:

代码语言:bash
复制
[root@nixos:~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0  1.5G  1 loop /nix/.ro-store
sr0     11:0    1  1.6G  0 rom  /iso
sda    253:0    0   50G  0 disk
├─sda1 253:1    0    1G  0 part /mnt/boot
├─sda2 253:2    0    4G  0 part [SWAP]
└─sda3 253:3    0   35G  0 part /mnt

看起来没问题,就可以开始生成配置文件了。

初始 NixOS 配置

官方 NixOS 安装手册

生成配置文件,使用以下命令:

代码语言:bash
复制
nixos-generate-config --root /mnt

配置 configuration.nix

现在可以开始配置 configuration.nix 文件了。

sudo vim /etc/nixos/configuration.nix

代码语言:bash
复制
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).

{ config, lib, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Use the systemd-boot EFI boot loader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  networking.hostName = "nixos"; # Define your hostname.
  # Pick only one of the below networking options.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.
  networking.networkmanager.enable = true;  # Easiest to use and most distros use this by default.

  # Set your time zone.
  time.timeZone = "Asia/Shanghai";

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # 使用国内源
  nix.settings.substituters = [

    "https://mirrors.ustc.edu.cn/nix-channels/store?priority=10"
    "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store?priority=5"
    "https://cache.nixos.org/"
  ];

  # Select internationalisation properties.
  # i18n.defaultLocale = "en_US.UTF-8";
  # console = {
  #   font = "Lat2-Terminus16";
  #   keyMap = "us";
  #   useXkbConfig = true; # use xkb.options in tty.
  # };

  # Enable the X11 windowing system.
  # services.xserver.enable = true;
  services.xserver = {
    enable = true;
    windowManager.qtile.enable = true;
  };


  # Configure keymap in X11
  # services.xserver.xkb.layout = "us";
  # services.xserver.xkb.options = "eurosign:e,caps:escape";

  # Enable CUPS to print documents.
  # services.printing.enable = true;

  # Enable sound.
  # services.pulseaudio.enable = true;
  # OR
  # services.pipewire = {
  #   enable = true;
  #   pulse.enable = true;
  # };

  # Enable touchpad support (enabled default in most desktopManager).
  # services.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
 users.users.demo = {   # demo换成你自己的用户名
   isNormalUser = true;
   extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
   packages = with pkgs; [
     tree
   ];
 };

  programs.firefox.enable = true;

  # List packages installed in system profile.
  # You can use https://search.nixos.org/ to find more packages (and options).
 environment.systemPackages = with pkgs; [
   vim 
   wget
   alacritty
 ];

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
   services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;
  # 删除多余的说明注释

  system.stateVersion = "25.05"; # Did you read the comment?

}

安装NixOS

安装系统使用以下命令:

代码语言:bash
复制
nixos-install  /mnt/etc/nixos

## 输入密码,务必设置密码,否则你将无法登录,demo换成你的用户名

nixos-enter --root /mnt -c 'passwd demo'

## 启动系统
reboot

优化 configuration.nix

对默认的配置文件进行修改,主要包括删除注释等操作、输入法和常用工具正确设置,以下是优化后的结果:

代码语言:bash
复制
{ config, lib, pkgs, ... }:

{
  imports = [ ./hardware-configuration.nix ];

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  networking.hostName = "nixos";  # 替换为你的主机名
  networking.networkmanager.enable = true;

  time.timeZone = "Asia/Shanghai";

  # 使用国内源
  nix.settings.substituters = [
    "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store?priority=5"
    "https://mirrors.ustc.edu.cn/nix-channels/store?priority=10"
    "https://cache.nixos.org/"
  ];

  # 国际化设置
  i18n.defaultLocale = "en_US.UTF-8";
  i18n.extraLocaleSettings = {
    LC_ADDRESS = "zh_CN.UTF-8";
    LC_IDENTIFICATION = "zh_CN.UTF-8";
    LC_MEASUREMENT = "zh_CN.UTF-8";
    LC_MONETARY = "zh_CN.UTF-8";
    LC_NAME = "zh_CN.UTF-8";
    LC_NUMERIC = "zh_CN.UTF-8";
    LC_PAPER = "zh_CN.UTF-8";
    LC_TELEPHONE = "zh_CN.UTF-8";
    LC_TIME = "zh_CN.UTF-8";
  };

  # 输入法配置
  i18n.inputMethod = {
    enable = true;
    type = "fcitx5";
    fcitx5.addons = with pkgs; [
      fcitx5-rime
      fcitx5-chinese-addons
    ];
  };

  services.xserver = {
    enable = true;
    windowManager.qtile.enable = true;
  };

  users.users.demo = {  # 替换为你的用户名
    isNormalUser = true;
    extraGroups = [ "wheel" ];
    packages = with pkgs; [ tree ];
  };

  programs.firefox.enable = true;

  environment.systemPackages = with pkgs; [
    vim
    wget
    alacritty
    git
    fcitx5-configtool  # Fcitx5 配置工具
  ];

  # fcitx5 环境变量
  environment.variables = {
    GTK_IM_MODULE = "fcitx";
    QT_IM_MODULE = "fcitx";
    XMODIFIERS = "@im=fcitx";
  };

  fonts.packages = with pkgs; [
    nerd-fonts.jetbrains-mono
    noto-fonts-cjk  # 支持中文
  ];

  # 打印服务
  services.printing.enable = true;

  # 音频服务(PipeWire 优于 PulseAudio)
  services.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };

  system.stateVersion = "25.05";
}

应用配置

备份原文件:sudo cp /etc/nixos/configuration.nix /etc/nixos/configuration.nix.bak

然后,运行以下命令检查语法和应用配置:

代码语言:bash
复制
sudo nixos-rebuild dry-run  # 检查配置是否有效
sudo nixos-rebuild switch   # 应用配置

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • 从最小化 ISO 安装
    • 加载 Live ISO
    • 初始 NixOS 配置
    • 配置 configuration.nix
    • 安装NixOS
  • 优化 configuration.nix
    • 应用配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档