You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL环境下NixOS执行nixos-rebuild switch失败求助

WSL下NixOS执行nixos-rebuild switch报错的解决方案

问题场景

我在WSL中安装NixOS,完成镜像下载和频道更新后,为安装git修改了/etc/nixos/configuration.nix,添加了pkgs.git。执行sudo nixos-rebuild switch时触发大量错误,核心提示为Nix搜索路径条目/nix/var/nix/profiles/per-user/root/channels/nixos不存在,找不到nixpkgs/nixos等文件,需通过$NIX_PATH或-I参数指定路径。

我的配置文件

# 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`).

# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL

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

{
  imports = [
    # include NixOS-WSL modules
    <nixos-wsl/modules>
  ];

  wsl.enable = true;
  wsl.defaultUser = "nixos";
  environment.systemPackages = [
    pkgs.git
  ];


  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It's perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "23.05"; # Did you read the comment?
}

错误信息

warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist,
ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file 'nixpkgs/nixos' was not found in the Nix search
path (add it using $NIX_PATH or -I)

at «string»:1:13:

        1| with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild
         |             ^ (use '--show-trace' to show detailed location information) building Nix... warning: Nix search path entry

'/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist,
ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file 'nixpkgs/nixos' was not found in the Nix search
path (add it using $NIX_PATH or -I)

at «none»:0: (source not available) warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not

exist, ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file 'nixpkgs' was not found in the Nix search path
(add it using $NIX_PATH or -I)

at «none»:0: (source not available) warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not

exist, ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file
'nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix' was not
found in the Nix search path (add it using $NIX_PATH or -I)

at «none»:0: (source not available) /tmp/nixos-rebuild.CkQLrX/nix warning: Nix search path entry

'/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist,
ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file 'nixpkgs' was not found in the Nix search path
(add it using $NIX_PATH or -I)

at «none»:0: (source not available) building the system configuration... warning: Nix search path entry

'/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist,
ignoring warning: Nix search path entry
'/nix/var/nix/profiles/per-user/root/channels' does not exist,
ignoring error: file 'nixpkgs/nixos' was not found in the Nix search
path (add it using $NIX_PATH or -I)

at «none»:0: (source not available)

解决方法

方法1:临时指定NIX_PATH执行命令

WSL版NixOS的系统频道默认在/nix/var/nix/profiles/system/channel/nixpkgs,直接在sudo命令中传递环境变量即可:

sudo NIX_PATH=nixpkgs=/nix/var/nix/profiles/system/channel/nixpkgs nixos-rebuild switch

方法2:为root用户配置频道

切换到root用户,配置与system.stateVersion一致版本的频道(这里是23.05):

sudo su
nix-channel --add https://nixos.org/channels/nixos-23.05 nixos
nix-channel --update
exit
sudo nixos-rebuild switch

方法3:用-I参数指定nixpkgs路径

直接在nixos-rebuild命令中通过-I参数指定nixpkgs位置:

sudo nixos-rebuild switch -I nixpkgs=/nix/var/nix/profiles/system/channel/nixpkgs

原因说明

错误根源是Nix无法定位nixpkgs仓库位置。WSL版NixOS的系统级频道已存在,但root用户的个人频道未配置,导致执行sudo nixos-rebuild时找不到默认搜索路径,手动指定路径或配置用户频道即可解决。

内容的提问来源于stack exchange,提问作者Pierre-olivier Gendraud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 19:28:10