在WSL中安装NixOS

在WSL中安装NixOS

Install NixOS-WSL

First, download nixos.wsl from the latest release.1 https://github.com/nix-community/NixOS-WSL/releases/tag/2505.7.0

If you have WSL version 2.4.4 or later installed, you can open (double-click) the .wsl file to install it. It is also possible to perform the installation from a PowerShell:

1
wsl --install --from-file nixos.wsl

nixos.wsl must be the path to the file you just downloaded if you’re running the command in another directory.

You can use the --name and --location flags to change the name the distro is registered under (default: NixOS) and the location of the disk image (default: %localappdata%\wsl\{some random GUID}). For a full list of options, refer to wsl --help

To open a shell in your NixOS environment, run wsl -d NixOS, select NixOS from the profile dropdown in Windows Terminal or run it from your Start Menu. (Adjust the name accordingly if you changed it)

  • 参考资料

https://nix-community.github.io/NixOS-WSL/install.html

配置vscode远程访问

The VSCode Remote server can not be run as-is on NixOS, because it downloads a nodejs binary that requires /lib64/ld-linux-x86-64.so.2 to be present, which isn’t the case on NixOS.

There are two options to get the server to run. Option 1 is more robust but might impact other programs. Option 2 is a little bit more brittle and sometimes breaks on updates but doesn’t influence other programs. Both options require wget to be installed:

1
2
3
environment.systemPackages = [
pkgs.wget
];

Option 1: Set up nix-ld
nix-ld is a program that provides /lib64/ld-linux-x86-64.so.2, allowing foreign binaries to run on NixOS.

To set it up, add the following to your configuration:

programs.nix-ld.enable = true;

  • 参考资料

https://nix-community.github.io/NixOS-WSL/how-to/vscode.html

nix-rebuild命令

nix-rebuil命令

1
2
3
4
5
6
7
nixos-rebuild {switch | boot | test | build | dry-build | dry-activate | edit | build-vm | build-vm-with-bootloader}
[{ --upgrade | --upgrade-all }] [--install-bootloader] [--no-build-nix] [--fast] [--rollback] [--builders builder-spec]
[--flake flake-uri] [--override-input input-name flake-uri]
[{ --profile-name | -p } name]
[--build-host host] [--target-host host] [--use-remote-sudo]
[--show-trace] [-I path] [{--verbose | -v}] [{--impure}] [{--max-jobs | -j} number] [{--keep-failed | -K}] [{--keep-going | -k}]

几个常见的命令

1
2
3
4
5
nixos-rebuild list-generations
sudo nix-collect-garbage -d
sudo nixos-rebuild boot
sudo nixos-rebuild switch -p profile-name

其中最后一条命令详解如下:

1
2
3
4
5
6
7
8
9
10
--profile-name,  -p

Instead of using the Nix profile /nix/var/nix/profiles/system to keep track of the current and previous system configurations, use /nix/var/nix/profiles/system-profiles/name. When you use GRUB 2, for every system profile created with this flag, NixOS will create a submenu named “NixOS - Profile 'name'” in GRUB’s boot menu, containing the current and previous configurations of this profile.

For instance, if you want to test a configuration file named test.nix without affecting the default system profile, you would do:

$ nixos-rebuild switch -p test -I nixos-config=./test.nix

The new configuration will appear in the GRUB 2 submenu “NixOS - Profile 'test'”.

可以在/nix/var/nix/profiles/system-profiles/ 目录下找到标签相关的文件,删除掉标签目录,重新执行一次sudo nixos-rebuild boot 就可以删除标签相关grub启动项目。

阅读更多

安装nixos后要做的事情

更新channel源

1
2
sudo nix-channel --add https://mirrors.tuna.tsinghua.edu.cn/nix-channels/nixos-24.11 nixos
sudo nix-channel --update

注意修改nixos-24.11到匹配的版本

启用flatpak

  1. 在/etc/nixos/configuration.nix中添加如下代码,启用flatpak
1
2
3
4
# flatpak support
xdg.portal.extraPortals = [ pkgs.xdg-desktop-portal-kde ];
xdg.portal.enable = true;
services.flatpak.enable = true;
  1. 启用蓝牙
1
2
hardware.bluetooth.enable = true; # enables support for Bluetooth
hardware.bluetooth.powerOnBoot = true; # powers up the default Bluetooth controller on boot
  1. 启用系统级工具
1
2
3
4
5
6
7
8
9
10
11
12
13
environment.systemPackages = with pkgs; [
vim
wget
pkgs.git
pkgs.nodejs_20
pkgs.texliveSmall
cpkgs.hexo-cli
pkgs.pandoc
pkgs.vscode
pkgs.microsoft-edge
pkgs.texliveSmall

];
  1. 安装字体及输入法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
i18n.inputMethod = {

enable = true;
type = "fcitx5";
fcitx5.addons = with pkgs; [
fcitx5-gtk # alternatively, kdePackages.fcitx5-qt
fcitx5-chinese-addons # table input method support
fcitx5-nord # a color theme
fcitx5-fluent
fcitx5-material-color
rime-data
fcitx5-rime
];

};

fonts = {
packages = with pkgs; [
noto-fonts
noto-fonts-cjk-sans
noto-fonts-cjk-serif
source-code-pro
hack-font
jetbrains-mono
];
};

  1. 配置网络
1
2
3
4
5
6
7
8
9
# Enable networking
networking.networkmanager.enable = true;
networking.networkmanager.dns = "none";
networking.nameservers = [
"223.5.5.5"
"8.8.8.8"
"114.114.114.114"
];

  1. 更新系统执行sudo nixos-rebuild switch更新系统

  2. 修改flatpak源,并安装常用软件

1
2
3
4
sudo flatpak remote-modify flathub --url=https://mirrors.ustc.edu.cn/flathub

flatpak install flathub md.obsidian.Obsidian

阅读更多