1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-12 12:59:49 +01:00
home-manager/modules/programs/vscode.nix

150 lines
4.3 KiB
Nix
Raw Normal View History

2018-11-19 17:50:35 +01:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.vscode;
vscodePname = cfg.package.pname;
jsonFormat = pkgs.formats.json { };
configDir = {
"vscode" = "Code";
"vscode-insiders" = "Code - Insiders";
2019-11-11 17:50:34 +01:00
"vscodium" = "VSCodium";
}.${vscodePname};
extensionDir = {
"vscode" = "vscode";
"vscode-insiders" = "vscode-insiders";
"vscodium" = "vscode-oss";
}.${vscodePname};
2020-10-12 22:51:12 +02:00
userDir = if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/${configDir}/User"
else
"${config.xdg.configHome}/${configDir}/User";
configFilePath = "${userDir}/settings.json";
keybindingsFilePath = "${userDir}/keybindings.json";
# TODO: On Darwin where are the extensions?
extensionPath = ".${extensionDir}/extensions";
2018-11-19 17:50:35 +01:00
2020-10-12 22:51:12 +02:00
in {
2018-11-19 17:50:35 +01:00
options = {
programs.vscode = {
enable = mkEnableOption "Visual Studio Code";
package = mkOption {
type = types.package;
default = pkgs.vscode;
example = literalExpression "pkgs.vscodium";
description = ''
Version of Visual Studio Code to install.
'';
};
2018-11-19 17:50:35 +01:00
userSettings = mkOption {
type = jsonFormat.type;
2020-10-12 22:51:12 +02:00
default = { };
example = literalExpression ''
2018-11-19 17:50:35 +01:00
{
"update.channel" = "none";
"[nix]"."editor.tabSize" = 2;
}
'';
description = ''
Configuration written to Visual Studio Code's
<filename>settings.json</filename>.
2018-11-19 17:50:35 +01:00
'';
};
keybindings = mkOption {
type = types.listOf (types.submodule {
options = {
key = mkOption {
type = types.str;
example = "ctrl+c";
description = "The key or key-combination to bind.";
};
command = mkOption {
type = types.str;
example = "editor.action.clipboardCopyAction";
description = "The VS Code command to execute.";
};
when = mkOption {
type = types.nullOr (types.str);
default = null;
example = "textInputFocus";
description = "Optional context filter.";
};
# https://code.visualstudio.com/docs/getstarted/keybindings#_command-arguments
args = mkOption {
type = types.nullOr (jsonFormat.type);
default = null;
example = { direction = "up"; };
description = "Optional arguments for a command.";
};
};
});
2020-10-12 22:51:12 +02:00
default = [ ];
example = literalExpression ''
[
{
key = "ctrl+c";
command = "editor.action.clipboardCopyAction";
when = "textInputFocus";
}
]
'';
description = ''
Keybindings written to Visual Studio Code's
<filename>keybindings.json</filename>.
'';
};
2018-11-19 17:50:35 +01:00
extensions = mkOption {
type = types.listOf types.package;
2020-10-12 22:51:12 +02:00
default = [ ];
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.Nix ]";
2018-11-19 17:50:35 +01:00
description = ''
The extensions Visual Studio Code should be started with.
These will override but not delete manually installed ones.
'';
};
};
};
config = mkIf cfg.enable {
home.packages = [ cfg.package ];
2018-11-19 17:50:35 +01:00
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
2020-10-12 22:51:12 +02:00
home.file = let
subDir = "share/vscode/extensions";
2021-11-25 02:55:38 +01:00
toPaths = ext:
# Links every dir in ext to the extension path.
map (k: { "${extensionPath}/${k}".source = "${ext}/${subDir}/${k}"; })
(if ext ? vscodeExtUniqueId then
[ ext.vscodeExtUniqueId ]
else
builtins.attrNames (builtins.readDir (ext + "/${subDir}")));
2020-10-12 22:51:12 +02:00
toSymlink = concatMap toPaths cfg.extensions;
dropNullFields = filterAttrs (_: v: v != null);
2020-10-12 22:51:12 +02:00
in foldr (a: b: a // b) {
"${configFilePath}" = mkIf (cfg.userSettings != { }) {
source = jsonFormat.generate "vscode-user-settings" cfg.userSettings;
2020-10-12 22:51:12 +02:00
};
"${keybindingsFilePath}" = mkIf (cfg.keybindings != [ ]) {
source = jsonFormat.generate "vscode-keybindings"
(map dropNullFields cfg.keybindings);
2020-10-12 22:51:12 +02:00
};
} toSymlink;
2018-11-19 17:50:35 +01:00
};
}