2022-12-04 13:45:43 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2022-11-11 16:32:26 +00:00
|
|
|
cfg = config.programs.vscode-mod;
|
|
|
|
|
|
|
|
vscodePname = cfg.package.pname;
|
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
jsonFormat = pkgs.formats.json {};
|
|
|
|
|
|
|
|
configDir =
|
|
|
|
{
|
|
|
|
"vscode" = "Code";
|
|
|
|
"vscode-insiders" = "Code - Insiders";
|
|
|
|
"vscodium" = "VSCodium";
|
|
|
|
}
|
|
|
|
.${vscodePname};
|
|
|
|
|
|
|
|
extensionDir =
|
|
|
|
{
|
|
|
|
"vscode" = "vscode";
|
|
|
|
"vscode-insiders" = "vscode-insiders";
|
|
|
|
"vscodium" = "vscode-oss";
|
|
|
|
}
|
|
|
|
.${vscodePname};
|
|
|
|
|
|
|
|
userDir =
|
|
|
|
if pkgs.stdenv.hostPlatform.isDarwin
|
|
|
|
then "Library/Application Support/${configDir}/User"
|
|
|
|
else "${config.xdg.configHome}/${configDir}/User";
|
2022-11-11 16:32:26 +00:00
|
|
|
|
|
|
|
configFilePath = "${userDir}/settings.json";
|
|
|
|
tasksFilePath = "${userDir}/tasks.json";
|
|
|
|
keybindingsFilePath = "${userDir}/keybindings.json";
|
|
|
|
|
|
|
|
# TODO: On Darwin where are the extensions?
|
|
|
|
extensionPath = ".${extensionDir}/extensions";
|
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
mergedUserSettings =
|
|
|
|
cfg.userSettings
|
|
|
|
// optionalAttrs (!cfg.enableUpdateCheck) {"update.mode" = "none";}
|
2022-11-11 16:32:26 +00:00
|
|
|
// optionalAttrs (!cfg.enableExtensionUpdateCheck) {
|
|
|
|
"extensions.autoCheckUpdates" = false;
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
programs.vscode-mod = {
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableUpdateCheck = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable update checks/notifications.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableExtensionUpdateCheck = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable update notifications for extensions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userSettings = mkOption {
|
|
|
|
type = jsonFormat.type;
|
2022-12-04 13:45:43 +00:00
|
|
|
default = {};
|
2022-11-11 16:32:26 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
"files.autoSave" = "off";
|
|
|
|
"[nix]"."editor.tabSize" = 2;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Configuration written to Visual Studio Code's
|
|
|
|
<filename>settings.json</filename>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
userTasks = mkOption {
|
|
|
|
type = jsonFormat.type;
|
2022-12-04 13:45:43 +00:00
|
|
|
default = {};
|
2022-11-11 16:32:26 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
version = "2.0.0";
|
|
|
|
tasks = [
|
|
|
|
{
|
|
|
|
type = "shell";
|
|
|
|
label = "Hello task";
|
|
|
|
command = "hello";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Configuration written to Visual Studio Code's
|
|
|
|
<filename>tasks.json</filename>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2022-12-04 13:45:43 +00:00
|
|
|
example = {direction = "up";};
|
2022-11-11 16:32:26 +00:00
|
|
|
description = "Optional arguments for a command.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2022-12-04 13:45:43 +00:00
|
|
|
default = [];
|
2022-11-11 16:32:26 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key = "ctrl+c";
|
|
|
|
command = "editor.action.clipboardCopyAction";
|
|
|
|
when = "textInputFocus";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Keybindings written to Visual Studio Code's
|
|
|
|
<filename>keybindings.json</filename>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extensions = mkOption {
|
|
|
|
type = types.listOf types.package;
|
2022-12-04 13:45:43 +00:00
|
|
|
default = [];
|
2022-11-11 16:32:26 +00:00
|
|
|
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]";
|
|
|
|
description = ''
|
|
|
|
The extensions Visual Studio Code should be started with.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutableExtensionsDir = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = ''
|
|
|
|
Whether extensions can be installed or updated manually
|
|
|
|
or by Visual Studio Code.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-12-04 13:45:43 +00:00
|
|
|
home.packages = [cfg.package];
|
2022-11-11 16:32:26 +00:00
|
|
|
|
|
|
|
# make config changeable
|
|
|
|
home = {
|
|
|
|
activation = {
|
2022-12-04 13:45:43 +00:00
|
|
|
vscode-mod-copy = hm.dag.entryAfter ["writeBoundary"] ''
|
2022-11-11 16:32:26 +00:00
|
|
|
$DRY_RUN_CMD cat "${configFilePath}.source" > "${configFilePath}"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
home.file = mkMerge [
|
2022-12-04 13:45:43 +00:00
|
|
|
(mkIf (mergedUserSettings != {}) {
|
2022-11-11 16:32:26 +00:00
|
|
|
# Don't install settings to actual config file path
|
|
|
|
# instead install to ${configFilePath}.source
|
|
|
|
"${configFilePath}.source".source =
|
|
|
|
jsonFormat.generate "vscode-user-settings" mergedUserSettings;
|
|
|
|
})
|
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
(mkIf (cfg.userTasks != {}) {
|
2022-11-11 16:32:26 +00:00
|
|
|
"${tasksFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-user-tasks" cfg.userTasks;
|
|
|
|
})
|
2022-12-04 13:45:43 +00:00
|
|
|
(mkIf (cfg.keybindings != [])
|
|
|
|
(let
|
|
|
|
dropNullFields = filterAttrs (_: v: v != null);
|
2022-11-11 16:32:26 +00:00
|
|
|
in {
|
|
|
|
"${keybindingsFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-keybindings"
|
|
|
|
(map dropNullFields cfg.keybindings);
|
|
|
|
}))
|
2022-12-04 13:45:43 +00:00
|
|
|
(mkIf (cfg.extensions != []) (let
|
2022-11-11 16:32:26 +00:00
|
|
|
subDir = "share/vscode/extensions";
|
|
|
|
|
|
|
|
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
|
|
|
|
toPaths = ext:
|
2022-12-04 13:45:43 +00:00
|
|
|
map (k: {"${extensionPath}/${k}".source = "${ext}/${subDir}/${k}";})
|
|
|
|
(
|
|
|
|
if ext ? vscodeExtUniqueId
|
|
|
|
then [ext.vscodeExtUniqueId]
|
|
|
|
else builtins.attrNames (builtins.readDir (ext + "/${subDir}"))
|
|
|
|
);
|
|
|
|
in
|
|
|
|
if cfg.mutableExtensionsDir
|
|
|
|
then mkMerge (concatMap toPaths cfg.extensions)
|
|
|
|
else {
|
|
|
|
"${extensionPath}".source = let
|
|
|
|
combinedExtensionsDrv = pkgs.buildEnv {
|
|
|
|
name = "vscode-extensions";
|
|
|
|
paths = cfg.extensions;
|
|
|
|
};
|
|
|
|
in "${combinedExtensionsDrv}/${subDir}";
|
|
|
|
}))
|
2022-11-11 16:32:26 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|