mirror of
https://github.com/nix-community/home-manager
synced 2024-11-18 00:59:44 +01:00
167 lines
4.7 KiB
Nix
167 lines
4.7 KiB
Nix
{ 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";
|
|
"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";
|
|
|
|
configFilePath = "${userDir}/settings.json";
|
|
keybindingsFilePath = "${userDir}/keybindings.json";
|
|
|
|
# TODO: On Darwin where are the extensions?
|
|
extensionPath = ".${extensionDir}/extensions";
|
|
|
|
in {
|
|
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.
|
|
'';
|
|
};
|
|
|
|
userSettings = mkOption {
|
|
type = jsonFormat.type;
|
|
default = { };
|
|
example = literalExpression ''
|
|
{
|
|
"update.channel" = "none";
|
|
"[nix]"."editor.tabSize" = 2;
|
|
}
|
|
'';
|
|
description = ''
|
|
Configuration written to Visual Studio Code's
|
|
<filename>settings.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;
|
|
example = { direction = "up"; };
|
|
description = "Optional arguments for a command.";
|
|
};
|
|
};
|
|
});
|
|
default = [ ];
|
|
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;
|
|
default = [ ];
|
|
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.Nix ]";
|
|
description = ''
|
|
The extensions Visual Studio Code should be started with.
|
|
'';
|
|
};
|
|
|
|
immutableExtensionsDir = mkOption {
|
|
type = types.bool;
|
|
default = false;
|
|
example = true;
|
|
description = ''
|
|
Whether extensions can be installed or updated manually
|
|
or by Visual Studio Code.
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
home.packages = [ cfg.package ];
|
|
|
|
home.file = mkMerge [
|
|
(mkIf (cfg.userSettings != { }) {
|
|
"${configFilePath}".source =
|
|
jsonFormat.generate "vscode-user-settings" cfg.userSettings;
|
|
})
|
|
(mkIf (cfg.keybindings != { })
|
|
(let dropNullFields = filterAttrs (_: v: v != null);
|
|
in {
|
|
"${keybindingsFilePath}".source =
|
|
jsonFormat.generate "vscode-keybindings"
|
|
(map dropNullFields cfg.keybindings);
|
|
}))
|
|
(mkIf (cfg.extensions != [ ]) (let
|
|
combinedExtensionsDrv = pkgs.buildEnv {
|
|
name = "vscode-extensions";
|
|
paths = cfg.extensions;
|
|
};
|
|
|
|
extensionsFolder = "${combinedExtensionsDrv}/share/vscode/extensions";
|
|
|
|
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
|
|
addSymlinkToExtension = k: {
|
|
"${extensionPath}/${k}".source = "${extensionsFolder}/${k}";
|
|
};
|
|
extensions = builtins.attrNames (builtins.readDir extensionsFolder);
|
|
in if cfg.immutableExtensionsDir then {
|
|
"${extensionPath}".source = extensionsFolder;
|
|
} else
|
|
mkMerge (map addSymlinkToExtension extensions)))
|
|
];
|
|
};
|
|
}
|