2018-11-19 17:50:35 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.vscode;
|
|
|
|
|
2019-07-29 22:39:25 +02:00
|
|
|
vscodePname = cfg.package.pname;
|
2023-02-08 11:00:27 +01:00
|
|
|
vscodeVersion = cfg.package.version;
|
2019-07-29 22:39:25 +02:00
|
|
|
|
2020-11-30 03:54:55 +01:00
|
|
|
jsonFormat = pkgs.formats.json { };
|
|
|
|
|
2019-07-29 22:39:25 +02:00
|
|
|
configDir = {
|
|
|
|
"vscode" = "Code";
|
|
|
|
"vscode-insiders" = "Code - Insiders";
|
2019-11-11 17:50:34 +01:00
|
|
|
"vscodium" = "VSCodium";
|
2024-02-05 07:34:43 +01:00
|
|
|
"openvscode-server" = "OpenVSCode Server";
|
2019-07-29 22:39:25 +02:00
|
|
|
}.${vscodePname};
|
|
|
|
|
2019-12-06 14:00:28 +01:00
|
|
|
extensionDir = {
|
|
|
|
"vscode" = "vscode";
|
|
|
|
"vscode-insiders" = "vscode-insiders";
|
|
|
|
"vscodium" = "vscode-oss";
|
2024-02-05 07:34:43 +01:00
|
|
|
"openvscode-server" = "openvscode-server";
|
2019-12-06 14:00:28 +01:00
|
|
|
}.${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";
|
2020-06-22 20:48:22 +02:00
|
|
|
|
|
|
|
configFilePath = "${userDir}/settings.json";
|
2022-03-18 23:54:24 +01:00
|
|
|
tasksFilePath = "${userDir}/tasks.json";
|
2020-06-22 20:48:22 +02:00
|
|
|
keybindingsFilePath = "${userDir}/keybindings.json";
|
2019-07-07 19:15:47 +02:00
|
|
|
|
2023-03-13 18:45:03 +01:00
|
|
|
snippetDir = "${userDir}/snippets";
|
|
|
|
|
2019-07-29 22:39:25 +02:00
|
|
|
# TODO: On Darwin where are the extensions?
|
2019-12-06 14:00:28 +01:00
|
|
|
extensionPath = ".${extensionDir}/extensions";
|
2018-11-19 17:50:35 +01:00
|
|
|
|
2023-02-08 11:00:27 +01:00
|
|
|
extensionJson = pkgs.vscode-utils.toExtensionJson cfg.extensions;
|
|
|
|
extensionJsonFile = pkgs.writeTextFile {
|
|
|
|
name = "extensions-json";
|
|
|
|
destination = "/share/vscode/extensions/extensions.json";
|
|
|
|
text = extensionJson;
|
|
|
|
};
|
|
|
|
|
2022-05-17 22:39:11 +02:00
|
|
|
mergedUserSettings = cfg.userSettings
|
|
|
|
// optionalAttrs (!cfg.enableUpdateCheck) { "update.mode" = "none"; }
|
|
|
|
// optionalAttrs (!cfg.enableExtensionUpdateCheck) {
|
|
|
|
"extensions.autoCheckUpdates" = false;
|
|
|
|
};
|
2020-10-12 22:51:12 +02:00
|
|
|
in {
|
2022-02-07 18:40:59 +01:00
|
|
|
imports = [
|
|
|
|
(mkChangedOptionModule [ "programs" "vscode" "immutableExtensionsDir" ] [
|
|
|
|
"programs"
|
|
|
|
"vscode"
|
|
|
|
"mutableExtensionsDir"
|
|
|
|
] (config: !config.programs.vscode.immutableExtensionsDir))
|
|
|
|
];
|
|
|
|
|
2018-11-19 17:50:35 +01:00
|
|
|
options = {
|
|
|
|
programs.vscode = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "Visual Studio Code";
|
2018-11-19 17:50:35 +01:00
|
|
|
|
2019-07-29 22:39:25 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.vscode;
|
2023-02-08 11:00:27 +01:00
|
|
|
defaultText = literalExpression "pkgs.vscode";
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression "pkgs.vscodium";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-07-29 22:39:25 +02:00
|
|
|
Version of Visual Studio Code to install.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-05-17 22:39:11 +02:00
|
|
|
enableUpdateCheck = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-05-17 22:39:11 +02:00
|
|
|
Whether to enable update checks/notifications.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableExtensionUpdateCheck = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-05-17 22:39:11 +02:00
|
|
|
Whether to enable update notifications for extensions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-11-19 17:50:35 +01:00
|
|
|
userSettings = mkOption {
|
2020-11-30 03:54:55 +01:00
|
|
|
type = jsonFormat.type;
|
2020-10-12 22:51:12 +02:00
|
|
|
default = { };
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2018-11-19 17:50:35 +01:00
|
|
|
{
|
2022-10-29 03:01:25 +02:00
|
|
|
"files.autoSave" = "off";
|
2018-11-19 17:50:35 +01:00
|
|
|
"[nix]"."editor.tabSize" = 2;
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-07-07 19:15:47 +02:00
|
|
|
Configuration written to Visual Studio Code's
|
2023-07-01 01:30:13 +02:00
|
|
|
{file}`settings.json`.
|
2018-11-19 17:50:35 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-18 23:54:24 +01:00
|
|
|
userTasks = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
2022-10-29 03:01:25 +02:00
|
|
|
version = "2.0.0";
|
|
|
|
tasks = [
|
2022-03-18 23:54:24 +01:00
|
|
|
{
|
2022-10-29 03:01:25 +02:00
|
|
|
type = "shell";
|
|
|
|
label = "Hello task";
|
|
|
|
command = "hello";
|
2022-03-18 23:54:24 +01:00
|
|
|
}
|
2022-10-29 03:01:25 +02:00
|
|
|
];
|
2022-03-18 23:54:24 +01:00
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-03-18 23:54:24 +01:00
|
|
|
Configuration written to Visual Studio Code's
|
2023-07-01 01:30:13 +02:00
|
|
|
{file}`tasks.json`.
|
2022-03-18 23:54:24 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-22 20:48:22 +02:00
|
|
|
keybindings = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
key = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "ctrl+c";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "The key or key-combination to bind.";
|
2020-06-22 20:48:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
command = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "editor.action.clipboardCopyAction";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "The VS Code command to execute.";
|
2020-06-22 20:48:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
when = mkOption {
|
2021-01-30 01:00:58 +01:00
|
|
|
type = types.nullOr (types.str);
|
|
|
|
default = null;
|
2020-06-22 20:48:22 +02:00
|
|
|
example = "textInputFocus";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Optional context filter.";
|
2020-06-22 20:48:22 +02:00
|
|
|
};
|
2021-01-30 01:00:58 +01:00
|
|
|
|
|
|
|
# https://code.visualstudio.com/docs/getstarted/keybindings#_command-arguments
|
|
|
|
args = mkOption {
|
2021-10-28 06:13:54 +02:00
|
|
|
type = types.nullOr (jsonFormat.type);
|
2021-01-30 01:00:58 +01:00
|
|
|
default = null;
|
|
|
|
example = { direction = "up"; };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Optional arguments for a command.";
|
2021-01-30 01:00:58 +01:00
|
|
|
};
|
2020-06-22 20:48:22 +02:00
|
|
|
};
|
|
|
|
});
|
2020-10-12 22:51:12 +02:00
|
|
|
default = [ ];
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2020-06-22 20:48:22 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
key = "ctrl+c";
|
|
|
|
command = "editor.action.clipboardCopyAction";
|
|
|
|
when = "textInputFocus";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2020-06-22 20:48:22 +02:00
|
|
|
Keybindings written to Visual Studio Code's
|
2023-07-01 01:30:13 +02:00
|
|
|
{file}`keybindings.json`.
|
2020-06-22 20:48:22 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-11-19 17:50:35 +01:00
|
|
|
extensions = mkOption {
|
|
|
|
type = types.listOf types.package;
|
2020-10-12 22:51:12 +02:00
|
|
|
default = [ ];
|
2022-02-25 00:45:41 +01:00
|
|
|
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]";
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-11-19 17:50:35 +01:00
|
|
|
The extensions Visual Studio Code should be started with.
|
2022-02-02 21:57:11 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-02-07 18:40:59 +01:00
|
|
|
mutableExtensionsDir = mkOption {
|
2022-02-02 21:57:11 +01:00
|
|
|
type = types.bool;
|
2022-02-07 18:40:59 +01:00
|
|
|
default = true;
|
|
|
|
example = false;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-02-02 21:57:11 +01:00
|
|
|
Whether extensions can be installed or updated manually
|
|
|
|
or by Visual Studio Code.
|
2018-11-19 17:50:35 +01:00
|
|
|
'';
|
|
|
|
};
|
2023-03-13 18:45:03 +01:00
|
|
|
|
|
|
|
languageSnippets = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
haskell = {
|
|
|
|
fixme = {
|
|
|
|
prefix = [ "fixme" ];
|
|
|
|
body = [ "$LINE_COMMENT FIXME: $0" ];
|
|
|
|
description = "Insert a FIXME remark";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Defines user snippets for different languages.";
|
2023-03-13 18:45:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
globalSnippets = mkOption {
|
|
|
|
type = jsonFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
fixme = {
|
|
|
|
prefix = [ "fixme" ];
|
|
|
|
body = [ "$LINE_COMMENT FIXME: $0" ];
|
|
|
|
description = "Insert a FIXME remark";
|
|
|
|
};
|
|
|
|
};
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Defines global user snippets.";
|
2023-03-13 18:45:03 +01:00
|
|
|
};
|
2018-11-19 17:50:35 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2019-07-29 22:39:25 +02:00
|
|
|
home.packages = [ cfg.package ];
|
2018-11-19 17:50:35 +01:00
|
|
|
|
2022-02-02 21:57:11 +01:00
|
|
|
home.file = mkMerge [
|
2022-05-17 22:39:11 +02:00
|
|
|
(mkIf (mergedUserSettings != { }) {
|
2022-02-02 21:57:11 +01:00
|
|
|
"${configFilePath}".source =
|
2022-05-17 22:39:11 +02:00
|
|
|
jsonFormat.generate "vscode-user-settings" mergedUserSettings;
|
2022-02-02 21:57:11 +01:00
|
|
|
})
|
2022-03-18 23:54:24 +01:00
|
|
|
(mkIf (cfg.userTasks != { }) {
|
|
|
|
"${tasksFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-user-tasks" cfg.userTasks;
|
|
|
|
})
|
2022-02-07 18:40:59 +01:00
|
|
|
(mkIf (cfg.keybindings != [ ])
|
2022-02-02 21:57:11 +01:00
|
|
|
(let dropNullFields = filterAttrs (_: v: v != null);
|
|
|
|
in {
|
|
|
|
"${keybindingsFilePath}".source =
|
|
|
|
jsonFormat.generate "vscode-keybindings"
|
|
|
|
(map dropNullFields cfg.keybindings);
|
|
|
|
}))
|
|
|
|
(mkIf (cfg.extensions != [ ]) (let
|
2022-07-15 16:29:10 +02:00
|
|
|
subDir = "share/vscode/extensions";
|
2022-02-02 21:57:11 +01:00
|
|
|
|
|
|
|
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
|
2022-07-15 16:29:10 +02:00
|
|
|
toPaths = ext:
|
|
|
|
map (k: { "${extensionPath}/${k}".source = "${ext}/${subDir}/${k}"; })
|
|
|
|
(if ext ? vscodeExtUniqueId then
|
|
|
|
[ ext.vscodeExtUniqueId ]
|
|
|
|
else
|
|
|
|
builtins.attrNames (builtins.readDir (ext + "/${subDir}")));
|
2022-02-07 18:40:59 +01:00
|
|
|
in if cfg.mutableExtensionsDir then
|
2023-02-08 11:00:27 +01:00
|
|
|
mkMerge (concatMap toPaths cfg.extensions
|
2023-02-08 12:18:28 +01:00
|
|
|
++ lib.optional (lib.versionAtLeast vscodeVersion "1.74.0") {
|
2023-02-08 11:00:27 +01:00
|
|
|
# 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-01-13 23:15:00 +01:00
|
|
|
run rm $VERBOSE_ARG -f ${extensionPath}/{extensions.json,.init-default-profile-extensions}
|
2024-01-23 22:59:26 +01:00
|
|
|
verboseEcho "Regenerating VSCode extensions.json"
|
2024-01-13 23:15:00 +01:00
|
|
|
run ${getExe cfg.package} --list-extensions > /dev/null
|
2023-02-08 11:00:27 +01:00
|
|
|
'';
|
|
|
|
};
|
2023-02-08 12:18:28 +01:00
|
|
|
})
|
2022-02-07 18:40:59 +01:00
|
|
|
else {
|
2022-07-15 16:29:10 +02:00
|
|
|
"${extensionPath}".source = let
|
|
|
|
combinedExtensionsDrv = pkgs.buildEnv {
|
|
|
|
name = "vscode-extensions";
|
2023-02-08 11:00:27 +01:00
|
|
|
paths = cfg.extensions
|
|
|
|
++ lib.optional (lib.versionAtLeast vscodeVersion "1.74.0")
|
2023-02-08 12:18:28 +01:00
|
|
|
extensionJsonFile;
|
2022-07-15 16:29:10 +02:00
|
|
|
};
|
|
|
|
in "${combinedExtensionsDrv}/${subDir}";
|
2022-02-07 18:40:59 +01:00
|
|
|
}))
|
2023-03-13 18:45:03 +01: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)
|
2022-02-02 21:57:11 +01:00
|
|
|
];
|
2018-11-19 17:50:35 +01:00
|
|
|
};
|
|
|
|
}
|