Visual Studio Code Server support in NixOS
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 年前 | ||
| 6 个月前 | ||
| 3 年前 | ||
| 6 年前 | ||
| 6 个月前 | ||
| 6 年前 | ||
| 1 个月前 | ||
| 1 个月前 |
NixOS 中的 Visual Studio Code Server 支持
NixOS 中对 VS Code Server 的实验性支持。由于缺少硬编码路径,VS Code 默认提供的 NodeJS 无法在 NixOS 中使用,因此会自动将其替换为指向 NixOS 下可正常工作的兼容版本 NodeJS 的符号链接。
安装
NixOS 模块
你可以通过多种方式将该模块添加到系统中。安装完成后,你需要为每个用户手动启用服务(见下文)。
作为 tarball 安装
{
imports = [
(fetchTarball "https://github.com/nix-community/nixos-vscode-server/tarball/master")
];
services.vscode-server.enable = true;
}
作为 flake 安装
{
inputs.vscode-server.url = "github:nix-community/nixos-vscode-server";
outputs = { self, nixpkgs, vscode-server }: {
nixosConfigurations.yourhostname = nixpkgs.lib.nixosSystem {
modules = [
vscode-server.nixosModules.default
({ config, pkgs, ... }: {
services.vscode-server.enable = true;
})
];
};
};
}
启用服务
然后为相关用户启用它们:
systemctl --user enable auto-fix-vscode-server.service
你将看到以下消息:
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
instance name specified.
不过您可以放心忽略它。启用后,该服务将在重启后自动启动,您也可以自行立即启动它,命令如下:
systemctl --user start auto-fix-vscode-server.service
启用用户服务会创建指向 Nix 存储的符号链接,但链接的存储路径在某些情况下可能会被垃圾回收。解决此特定问题的一种方法是创建以下符号链接:
ln -sfT /run/current-system/etc/systemd/user/auto-fix-vscode-server.service ~/.config/systemd/user/auto-fix-vscode-server.service
Home Manager
将此代码放入您的 home-manager 配置中,例如 ~/.config/nixpkgs/home.nix:
{
imports = [
"${fetchTarball "https://github.com/msteen/nixos-vscode-server/tarball/master"}/modules/vscode-server/home.nix"
];
services.vscode-server.enable = true;
}
使用方法
当使用 Microsoft 发布的 VS Code 且无特殊需求时,只需启用并启动服务即可正常运行。如果您有自定义构建或特殊需求,有一些可用选项或许能帮到您。
enable
是否启用该服务。
{
services.vscode-server.enable = true;
}
enableFHS
可以启用符合 FHS(文件系统层次结构标准)的环境,使扩展提供的二进制文件在 NixOS 中无需修补即可运行。请注意,这也存在一些缺点,例如对 SUID 包装器的支持存在问题,以及终端行为可能与正常的 SSH 连接不同,因此默认情况下未启用此功能。
{
services.vscode-server.enableFHS = true;
}
nodejsPackage
默认情况下,VS Code Server 会安装其所需的 Node.js 版本,此服务会自动对其进行补丁处理。但如果您希望节省磁盘空间,或希望它使用特定版本的 Node.js,可以指定它应使用的 Node.js Nix 包。
当 enableFHS 设置为 true 时,它始终需要一个 Node.js 的 Nix 包,但您无需手动设置,因为它会默认使用 VS Code 所使用的最新版本。
免责声明:我目前并不是此扩展的活跃用户,对 NixOS 的使用也不频繁,因此默认版本可能存在过时情况。至少通过提供此选项,您可以在默认版本更新之前采取变通方法。
{
services.vscode-server.nodejsPackage = pkgs.nodejs-16_x;
}
extraRuntimeDependencies
如果您有一个需要 FHS 兼容环境的扩展,但其二进制文件需要尚未包含的依赖项,您可以在此处添加这些依赖项,使其在 FHS 环境中可用。
此列表还用于在自动修补 ELF 二进制文件时确定 RPATH。
{
services.vscode-server.extraRuntimeDependencies = pkgs: with pkgs; [
curl
];
}
installPath
VS Code server 的安装路径是可配置的。你可以将单个路径指定为字符串,或将多个路径指定为列表。如果你有多个安装(例如,稳定版、OSS 版和内部预览版),可以将所有路径都指定为需要监控并自动修复的对象。
# Single path (string)
{
services.vscode-server.installPath = "$HOME/.vscode-server";
}
# Multiple paths (list)
{
services.vscode-server.installPath = [
"$HOME/.vscode-server"
"$HOME/.vscode-server-oss"
"$HOME/.vscode-server-insiders"
];
}
为保持向后兼容性,字符串值会自动转换为单元素列表。
postPatch
本项目旨在让 VS Code server 与 NixOS 协同工作,除此之外的功能均超出项目范围。但如果您需要执行额外操作,可通过此钩子在补丁完成后运行 shell 脚本。
{
services.vscode-server.postPatch = ''
bin_dir=$1
# $bin_dir (vscode 1.105.1) is something like
# ~/.vscode-server/cli/servers/Stable-7d842fb85a0275a4a8e4d7e040d2625abbf7f084/server
# ...
'';
}
故障排除
严格来说,这并非本项目本身的问题,而是 NixOS 中 systemd 用户服务普遍存在的问题。更新后,可能需要先再次禁用该服务:
systemctl --user disable auto-fix-vscode-server.service
这将移除指向旧版本的符号链接。之后你可以重新启用/启动它。
SSH 连接超时
如果远程 SSH 会话启动失败并出现以下错误:
无法连接到远程扩展主机服务器(错误:SSH 连接超时)
尝试将以下内容添加到你的 VS Code 设置 json 中:
"remote.SSH.useLocalServer": false,
已在 VS Code 1.63.2 版本上测试,从 MacOS 主机连接到 NixOS 远程。
未来工作
扩展补丁
还需要更多工作来研究是否可以在不使用 FHS 兼容环境的情况下,自动为 VS Code 扩展中的二进制文件打补丁。
WSL 支持
已经开展了一些工作,旨在让 WSL 能够开箱即用,但目前尚未完全实现。