2020-10-12 00:22:58 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.vscode;
|
|
|
|
|
|
|
|
vscodePname = cfg.package.pname;
|
2023-08-08 10:19:01 +00:00
|
|
|
vscodeVersion = cfg.package.version;
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2021-07-02 22:36:30 +00:00
|
|
|
jsonFormat = pkgs.formats.json { };
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
configDir = {
|
|
|
|
"vscode" = "Code";
|
|
|
|
"vscode-insiders" = "Code - Insiders";
|
|
|
|
"vscodium" = "VSCodium";
|
2024-06-04 18:23:39 +00:00
|
|
|
"openvscode-server" = "OpenVSCode Server";
|
2020-10-12 00:22:58 +00:00
|
|
|
}.${vscodePname};
|
|
|
|
|
|
|
|
extensionDir = {
|
|
|
|
"vscode" = "vscode";
|
|
|
|
"vscode-insiders" = "vscode-insiders";
|
|
|
|
"vscodium" = "vscode-oss";
|
2024-06-04 18:23:39 +00:00
|
|
|
"openvscode-server" = "openvscode-server";
|
2020-10-12 00:22:58 +00:00
|
|
|
}.${vscodePname};
|
|
|
|
|
|
|
|
userDir = if pkgs.stdenv.hostPlatform.isDarwin then
|
|
|
|
"Library/Application Support/${configDir}/User"
|
|
|
|
else
|
|
|
|
"${config.xdg.configHome}/${configDir}/User";
|
|
|
|
|
|
|
|
configFilePath = "${userDir}/settings.json";
|
2023-01-10 09:35:00 +00:00
|
|
|
tasksFilePath = "${userDir}/tasks.json";
|
2020-10-12 00:22:58 +00:00
|
|
|
keybindingsFilePath = "${userDir}/keybindings.json";
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
snippetDir = "${userDir}/snippets";
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
# TODO: On Darwin where are the extensions?
|
|
|
|
extensionPath = ".${extensionDir}/extensions";
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
extensionJson = pkgs.vscode-utils.toExtensionJson cfg.extensions;
|
|
|
|
extensionJsonFile = pkgs.writeTextFile {
|
|
|
|
name = "extensions-json";
|
|
|
|
destination = "/share/vscode/extensions/extensions.json";
|
|
|
|
text = extensionJson;
|
|
|
|
};
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
mergedUserSettings = cfg.userSettings
|
|
|
|
// optionalAttrs (!cfg.enableUpdateCheck) { "update.mode" = "none"; }
|
|
|
|
// optionalAttrs (!cfg.enableExtensionUpdateCheck) {
|
|
|
|
"extensions.autoCheckUpdates" = false;
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
in {
|
2023-01-10 09:35:00 +00:00
|
|
|
imports = [
|
|
|
|
(mkChangedOptionModule [ "programs" "vscode" "immutableExtensionsDir" ] [
|
|
|
|
"programs"
|
|
|
|
"vscode"
|
|
|
|
"mutableExtensionsDir"
|
|
|
|
] (config: !config.programs.vscode.immutableExtensionsDir))
|
|
|
|
];
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
options = {
|
|
|
|
programs.vscode = {
|
|
|
|
enable = mkEnableOption "Visual Studio Code";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.vscode;
|
2023-08-08 10:19:01 +00:00
|
|
|
defaultText = literalExpression "pkgs.vscode";
|
2021-11-04 16:42:44 +00:00
|
|
|
example = literalExpression "pkgs.vscodium";
|
2020-10-12 00:22:58 +00:00
|
|
|
description = ''
|
|
|
|
Version of Visual Studio Code to install.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
userSettings = mkOption {
|
2021-07-02 22:36:30 +00:00
|
|
|
type = jsonFormat.type;
|
2020-10-12 00:22:58 +00:00
|
|
|
default = { };
|
2021-11-04 16:42:44 +00:00
|
|
|
example = literalExpression ''
|
2020-10-12 00:22:58 +00:00
|
|
|
{
|
2023-01-10 09:35:00 +00:00
|
|
|
"files.autoSave" = "off";
|
2020-10-12 00:22:58 +00:00
|
|
|
"[nix]"."editor.tabSize" = 2;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Configuration written to Visual Studio Code's
|
2023-08-08 10:19:01 +00:00
|
|
|
{file}`settings.json`.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
userTasks = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
version = "2.0.0";
|
|
|
|
tasks = [
|
|
|
|
{
|
|
|
|
type = "shell";
|
|
|
|
label = "Hello task";
|
|
|
|
command = "hello";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Configuration written to Visual Studio Code's
|
2023-08-08 10:19:01 +00:00
|
|
|
{file}`tasks.json`.
|
2023-01-10 09:35:00 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-10-12 00:22:58 +00: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 {
|
2021-07-02 22:36:30 +00:00
|
|
|
type = types.nullOr (types.str);
|
|
|
|
default = null;
|
2020-10-12 00:22:58 +00:00
|
|
|
example = "textInputFocus";
|
|
|
|
description = "Optional context filter.";
|
|
|
|
};
|
2021-07-02 22:36:30 +00:00
|
|
|
|
|
|
|
# https://code.visualstudio.com/docs/getstarted/keybindings#_command-arguments
|
|
|
|
args = mkOption {
|
2021-11-04 16:42:44 +00:00
|
|
|
type = types.nullOr (jsonFormat.type);
|
2021-07-02 22:36:30 +00:00
|
|
|
default = null;
|
|
|
|
example = { direction = "up"; };
|
|
|
|
description = "Optional arguments for a command.";
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
default = [ ];
|
2021-11-04 16:42:44 +00:00
|
|
|
example = literalExpression ''
|
2020-10-12 00:22:58 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
key = "ctrl+c";
|
|
|
|
command = "editor.action.clipboardCopyAction";
|
|
|
|
when = "textInputFocus";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Keybindings written to Visual Studio Code's
|
2023-08-08 10:19:01 +00:00
|
|
|
{file}`keybindings.json`.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extensions = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ ];
|
2023-01-10 09:35:00 +00:00
|
|
|
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]";
|
2020-10-12 00:22:58 +00:00
|
|
|
description = ''
|
|
|
|
The extensions Visual Studio Code should be started with.
|
2023-01-10 09:35:00 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutableExtensionsDir = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = ''
|
|
|
|
Whether extensions can be installed or updated manually
|
|
|
|
or by Visual Studio Code.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-08-08 10:19:01 +00:00
|
|
|
|
|
|
|
languageSnippets = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
haskell = {
|
|
|
|
fixme = {
|
|
|
|
prefix = [ "fixme" ];
|
|
|
|
body = [ "$LINE_COMMENT FIXME: $0" ];
|
|
|
|
description = "Insert a FIXME remark";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
description = "Defines user snippets for different languages.";
|
|
|
|
};
|
|
|
|
|
|
|
|
globalSnippets = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
fixme = {
|
|
|
|
prefix = [ "fixme" ];
|
|
|
|
body = [ "$LINE_COMMENT FIXME: $0" ];
|
|
|
|
description = "Insert a FIXME remark";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
description = "Defines global user snippets.";
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
home.packages = [ cfg.package ];
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
home.file = mkMerge [
|
|
|
|
(mkIf (mergedUserSettings != { }) {
|
|
|
|
"${configFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-user-settings" mergedUserSettings;
|
|
|
|
})
|
|
|
|
(mkIf (cfg.userTasks != { }) {
|
|
|
|
"${tasksFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-user-tasks" cfg.userTasks;
|
|
|
|
})
|
|
|
|
(mkIf (cfg.keybindings != [ ])
|
|
|
|
(let dropNullFields = filterAttrs (_: v: v != null);
|
|
|
|
in {
|
|
|
|
"${keybindingsFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-keybindings"
|
|
|
|
(map dropNullFields cfg.keybindings);
|
|
|
|
}))
|
|
|
|
(mkIf (cfg.extensions != [ ]) (let
|
|
|
|
subDir = "share/vscode/extensions";
|
|
|
|
|
|
|
|
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
|
|
|
|
toPaths = ext:
|
|
|
|
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
|
2023-08-08 10:19:01 +00:00
|
|
|
mkMerge (concatMap toPaths cfg.extensions
|
|
|
|
++ lib.optional (lib.versionAtLeast vscodeVersion "1.74.0") {
|
|
|
|
# Whenever our immutable extensions.json changes, force VSCode to regenerate
|
|
|
|
# extensions.json with both mutable and immutable extensions.
|
|
|
|
"${extensionPath}/.extensions-immutable.json" = {
|
|
|
|
text = extensionJson;
|
|
|
|
onChange = ''
|
2024-06-04 18:23:39 +00:00
|
|
|
run rm $VERBOSE_ARG -f ${extensionPath}/{extensions.json,.init-default-profile-extensions}
|
|
|
|
verboseEcho "Regenerating VSCode extensions.json"
|
|
|
|
run ${getExe cfg.package} --list-extensions > /dev/null
|
2023-08-08 10:19:01 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
2023-01-10 09:35:00 +00:00
|
|
|
else {
|
|
|
|
"${extensionPath}".source = let
|
|
|
|
combinedExtensionsDrv = pkgs.buildEnv {
|
|
|
|
name = "vscode-extensions";
|
2023-08-08 10:19:01 +00:00
|
|
|
paths = cfg.extensions
|
|
|
|
++ lib.optional (lib.versionAtLeast vscodeVersion "1.74.0")
|
|
|
|
extensionJsonFile;
|
2023-01-10 09:35:00 +00:00
|
|
|
};
|
|
|
|
in "${combinedExtensionsDrv}/${subDir}";
|
|
|
|
}))
|
2023-08-08 10:19:01 +00:00
|
|
|
|
|
|
|
(mkIf (cfg.globalSnippets != { })
|
|
|
|
(let globalSnippets = "${snippetDir}/global.code-snippets";
|
|
|
|
in {
|
|
|
|
"${globalSnippets}".source =
|
|
|
|
jsonFormat.generate "user-snippet-global.code-snippets"
|
|
|
|
cfg.globalSnippets;
|
|
|
|
}))
|
|
|
|
|
|
|
|
(lib.mapAttrs' (language: snippet:
|
|
|
|
lib.nameValuePair "${snippetDir}/${language}.json" {
|
|
|
|
source = jsonFormat.generate "user-snippet-${language}.json" snippet;
|
|
|
|
}) cfg.languageSnippets)
|
2023-01-10 09:35:00 +00:00
|
|
|
];
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
}
|