
参考视频:
NixOS最小化安装 | 从零开始搭建你的NixOS系统(2)_哔哩哔哩_bilibili
分享一个如何快速从零开始使用 NixOS 的方法。特别适合中文用户,涵盖了分区、安装、配置优化等关键步骤。结合了官方手册的推荐方法,并针对中文环境(如输入法、国内源)进行了优化。
下载 NixOS 25.05 最小化 ISO(64-bit Intel/AMD)。烧录到 USB 驱动器,使用工具如 Rufus 或 dd。引导时选择 UEFI 模式,并禁用 Secure Boot(如果启用)。
我将使用最小化 ISO安装,你也可以使用图形化安装程序。
接下来的命令将决定 hardware.nix 的内容。
sudo -i #切换到超级用户
lsblk
cfdisk /dev/sda #设置分区方案
# GPT 分区标签
# 1G,类型:EFI
# 4G,类型:swap
# 剩余空间,类型:Linux Filesystem用 lsblk 检查一下分区,确认无误。我们可以看到刚刚创建的三个分区。现在来为这些分区创建文件系统。
NixOS 手册建议为分区设置标签,按照推荐的方式来做:
mkfs.ext4 -L nixos /dev/sda3
mkswap -L swap /dev/sda2
mkfs.fat -F 32 -n boot /dev/sda1文件系统创建好了,现在挂载它们:
mount /dev/sda3 /mnt
mount --mkdir /dev/sda1 /mnt/boot
swapon /dev/sda2示例输出:
[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-generate-config --root /mnt现在可以开始配置 configuration.nix 文件了。
sudo vim /etc/nixos/configuration.nix
# 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-install /mnt/etc/nixos
## 输入密码,务必设置密码,否则你将无法登录,demo换成你的用户名
nixos-enter --root /mnt -c 'passwd demo'
## 启动系统
reboot对默认的配置文件进行修改,主要包括删除注释等操作、输入法和常用工具正确设置,以下是优化后的结果:
{ 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
然后,运行以下命令检查语法和应用配置:
sudo nixos-rebuild dry-run # 检查配置是否有效
sudo nixos-rebuild switch # 应用配置原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。