这是关于在 Mac 上使用 Nix 配置 VSCode 的系列文章的一部分。主要内容如下:
- 安装 VSCode:Nix 不会安装非自由软件如 VSCode,需在
nix-darwin
的configuration
模块中添加nixpkgs.config.allowUnfree = true
。然后在home-manager
配置中使用homeconfig = { pkgs,... }: { programs.vscode = { enable = true; }; };
安装 VSCode,安装后位于/Users/$USER/Applications/Home Manager Apps/
,之前安装的需删除。 - 编辑器设置和快捷键:可通过
programs.vscode = { enable = true; userSettings = { "editor.formatOnSave" = true; }; keybindings = [ { key = "shift+cmd+j"; command = "workbench.action.focusActiveEditorGroup"; when = "terminalFocus"; } ]; };
添加用户设置和快捷键,Nix 类型与 JSON 的转换是 Nix 标准库的一部分。 - 扩展:VSCode 的扩展生态丰富,
home-manager
安装扩展类似安装包,默认nixpkgs
中没有完整的VSCode marketplace
,需添加nix-vscode-extensions
覆盖层,如nix-vscode-extensions.url = "github:nix-community/nix-vscode-extensions";
。可使用with
语句简化扩展列表的书写,如extensions = with pkgs.vscode-marketplace; [ jnoortheen.nix-ide dracula-theme.theme-dracula ];
。 - 与 Spotlight 兼容:Nix 安装的 VSCode 版本在 Spotlight 中不显示,因为是符号链接,Spotlight 不会索引符号链接。可使用
mac-app-util
来解决,在flake
中添加mac-app-util.url = "github:hraban/mac-app-util";
,并在darwinConfigurations
和home-manager
中加载mac-app-util
。 - 总结:Nix 配置现在能够完全配置和管理 VSCode,包括安装扩展和主题,
home-manager
与其他工具也有深度集成,可查看home-manager
的配置选项了解更多。完整的flake.nix
可在 GitHub 上获取。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。