使用 Arch WSL 作为你的开发环境!

· · 科技·工程

pacman 初始化

刚刚安装好 Arch 时,pacman 是不可用的,需要立刻初始化密钥环:

sudo pacman-key --init
sudo pacman-key --populate
sudo pacman -Syy archlinux-keyring

下载不了?

国内的网络环境可能会导致下载过慢,因此我们需要先临时在 /etc/pacman.d/mirrorlist 中,启用阿里云的镜像源。

...
## China
#Server = http://mirrors.163.com/archlinux/$repo/os/$arch
Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch
...

等到初期的准备完成之后,我们会对所有镜像进行测速,并重新设置镜像源。

安装完成之后,我们需要立即对 pacman 换源。具体的,先安装 pacman-contrib,然后使用 rankmirrors 测速:

cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
pacman -S pacman-contrib
curl -s "https://archlinux.org/mirrorlist/?country=CN&protocol=https&use_mirror_status=on" | sed -e 's/^#Server/Server/' -e '/^#/d' | rankmirrors -n 5 - > /etc/pacman.d/mirrorlist

最后,进行系统更新。Arch 采用了最激进的滚动更新策略,应当定期(如每天)执行如下命令:

pacman -Syu

Tips

可以打开 pacman 的多线程下载功能来加速下载。具体的,修改 /etc/pacman.conf:

...
ParallelDownloads = 32 # 以 32 线程下载
...

常用软件安装

首先,安装一些基本的软件包:

sudo pacman -S base-devel sudo git

对于 OIer,请安装 gnu 系列的相关编译和调试软件:

sudo pacman -S gcc gdb

如果想要安装 llvm 系列的编译软件,请执行:

sudo pacman -S llvm clang lldb

创建用户

创建一个非 root 用户是有必要的,使用 root 很容易误操作干掉系统。

首先,使用 passwd 给 root 设置一个密码。

如果需要,你可以为创建一个用户组,便于以后扩展多用户:

groupadd [group-name]

使用 useradd 创建你自己的用户,注意要添加到 wheel 组中,这样才能使用 sudo。

useradd -m [-g [group-name]] -G wheel -p [password] [username]

随后便可以使用 su 来切换到创建好的低权限用户,但在这之前,先编辑 /etc/sudoers 来启用 wheel 用户组:

## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL:ALL)ALL

测试一下:

$ sudo pacman -Syu

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

For security reasons, the password you type will not be visible.

[sudo] password for [username]:

能力越大,责任越大,能不用 sudo 尽量不用 sudo,否则会造成难搞的权限问题。

系统美化

安装 neovim

neovim 是 vim 的上位替代,假借插件的力量可以媲美 vscode:

sudo pacman -S neovim fzf                                    # 安装 neovim 和文件搜索软件 fzf
git clone https://github.com/LazyVim/starter ~/.config/nvim  # 安装 LazyVim 管理插件
nvim                                                         # 启动 neovim

可以安装 Nerd Font 来实现图标显示,强烈建议安装。

安装 zsh

zsh 是一个极其优雅的 shell,我们可以使用 oh-my-zsh 管理插件。

使用如下命令安装 zsh 和 oh-my-zsh:

sudo pacman -S zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装常用插件和主题 powerlevel10k:

git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

并在 .zshrc 中启用插件和主题:

...
ZSH_THEME="powerlevel10k/powerlevel10k"
...
plugins=(
  git z extract
  zsh-syntax-highlighting
  zsh-autosuggestions
)
...

修复 WSLg

截止 2025/3/2,默认情况下,Arch 的 X11 应用是不能无缝地打开窗口的,需要编辑 /etc/tmpfiles.d/wslg.conf:

# Type Path                          Mode UID  GID  Age Argument
L+     /tmp/.X11-unix/X0              -    -    -    -  /mnt/wslg/.X11-unix/X0

内核编译

WSL 默认的内核一般不是最新的,我们可以从 github 上下载最新的内核,并按需配置。

首先,安装如下软件:

sudo pacman -S flex pahole cpio rsync bc python

将内核配置文件改为 config-wsl:

export KCONFIG_CONFIG=Microsoft/config-wsl

在 menuconfig 中设置自己所需的选项:

make menuconfig

编译 bzImage:

make -j$(nproc)

安装内核模块:

sudo make modules_install
make headers_install

至此,我们已经安装好了完整的内核环境。