1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-23 11:39:46 +01:00

vscode: change option type of profiles

Signed-off-by: Reputable2722 <153411261+Reputable2772@users.noreply.github.com>
This commit is contained in:
Reputable2722 2024-07-20 20:08:01 +05:30
parent 1779d6bd76
commit dfb6f04bb5
No known key found for this signature in database
2 changed files with 204 additions and 189 deletions

View file

@ -59,143 +59,159 @@ let
destination = "/share/vscode/extensions/extensions.json"; destination = "/share/vscode/extensions/extensions.json";
}; };
mergedUserSettings = userSettings: mergedUserSettings =
userSettings: enableUpdateCheck: enableExtensionUpdateCheck:
userSettings userSettings
// optionalAttrs (!cfg.enableUpdateCheck) { "update.mode" = "none"; } // optionalAttrs (enableUpdateCheck == false) { "update.mode" = "none"; }
// optionalAttrs (!cfg.enableExtensionUpdateCheck) { // optionalAttrs (enableExtensionUpdateCheck == false) {
"extensions.autoCheckUpdates" = false; "extensions.autoCheckUpdates" = false;
}; };
profileType = default: profileType = types.submodule {
types.submodule { options = {
options = { userSettings = mkOption {
userSettings = mkOption { type = jsonFormat.type;
type = jsonFormat.type; default = { };
default = { }; example = literalExpression ''
example = literalExpression '' {
{ "files.autoSave" = "off";
"files.autoSave" = "off"; "[nix]"."editor.tabSize" = 2;
"[nix]"."editor.tabSize" = 2; }
} '';
''; description = ''
description = '' Configuration written to Visual Studio Code's
Configuration written to Visual Studio Code's {file}`settings.json`.
{file}`settings.json`. '';
''; };
};
userTasks = mkOption { userTasks = mkOption {
type = jsonFormat.type; type = jsonFormat.type;
default = { }; default = { };
example = literalExpression '' example = literalExpression ''
{ {
version = "2.0.0"; version = "2.0.0";
tasks = [ tasks = [
{
type = "shell";
label = "Hello task";
command = "hello";
}
];
}
'';
description = ''
Configuration written to Visual Studio Code's
{file}`tasks.json`.
'';
};
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"; type = "shell";
command = "editor.action.clipboardCopyAction"; label = "Hello task";
when = "textInputFocus"; command = "hello";
} }
] ];
''; }
description = '' '';
Keybindings written to Visual Studio Code's description = ''
{file}`keybindings.json`. Configuration written to Visual Studio Code's
''; {file}`tasks.json`.
}; '';
};
extensions = mkOption { keybindings = mkOption {
type = types.listOf types.package; type = types.listOf (types.submodule {
default = [ ]; options = {
example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]"; key = mkOption {
description = '' type = types.str;
The extensions Visual Studio Code should be started with. example = "ctrl+c";
''; description = "The key or key-combination to bind.";
}; };
languageSnippets = mkOption { command = mkOption {
type = jsonFormat.type; type = types.str;
default = { }; example = "editor.action.clipboardCopyAction";
example = { description = "The VS Code command to execute.";
haskell = { };
fixme = {
prefix = [ "fixme" ]; when = mkOption {
body = [ "$LINE_COMMENT FIXME: $0" ]; type = types.nullOr (types.str);
description = "Insert a FIXME remark"; 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.";
}; };
}; };
description = "Defines user snippets for different languages."; });
}; default = [ ];
example = literalExpression ''
[
{
key = "ctrl+c";
command = "editor.action.clipboardCopyAction";
when = "textInputFocus";
}
]
'';
description = ''
Keybindings written to Visual Studio Code's
{file}`keybindings.json`.
'';
};
globalSnippets = mkOption { extensions = mkOption {
type = jsonFormat.type; type = types.listOf types.package;
default = { }; default = [ ];
example = { example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]";
description = ''
The extensions Visual Studio Code should be started with.
'';
};
languageSnippets = mkOption {
type = jsonFormat.type;
default = { };
example = {
haskell = {
fixme = { fixme = {
prefix = [ "fixme" ]; prefix = [ "fixme" ];
body = [ "$LINE_COMMENT FIXME: $0" ]; body = [ "$LINE_COMMENT FIXME: $0" ];
description = "Insert a FIXME remark"; description = "Insert a FIXME remark";
}; };
}; };
description = "Defines global user snippets.";
}; };
} // optionalAttrs default { description = "Defines user snippets for different languages.";
name = mkOption { };
type = types.str;
description = "Visual Studio Code's Profile name."; 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.";
};
enableUpdateCheck = mkOption {
type = types.nullOr types.bool;
default = null;
description = ''
Whether to enable update checks/notifications.
Can only be set for the default profile, but
it applies to all profiles.
'';
};
enableExtensionUpdateCheck = mkOption {
type = types.nullOr types.bool;
default = null;
description = ''
Whether to enable update notifications for extensions.
Can only be set for the default profile, but
it applies to all profiles.
'';
}; };
}; };
allProfiles = cfg.profiles ++ [ cfg.defaultProfile ]; };
defaultProfile = (filterAttrs (n: v: n == "default") cfg.profiles).default;
allProfilesExceptDefault = (removeAttrs cfg.profiles [ "default" ]);
in { in {
imports = [ imports = [
(mkChangedOptionModule [ "programs" "vscode" "immutableExtensionsDir" ] [ (mkChangedOptionModule [ "programs" "vscode" "immutableExtensionsDir" ] [
@ -207,9 +223,12 @@ in {
mkRenamedOptionModule [ "programs" "vscode" v ] [ mkRenamedOptionModule [ "programs" "vscode" v ] [
"programs" "programs"
"vscode" "vscode"
"defaultProfile" "profiles"
"default"
v v
]) [ ]) [
"enableUpdateCheck"
"enableExtensionUpdateCheck"
"userSettings" "userSettings"
"userTasks" "userTasks"
"keybindings" "keybindings"
@ -231,25 +250,9 @@ in {
''; '';
}; };
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.
'';
};
mutableExtensionsDir = mkOption { mutableExtensionsDir = mkOption {
type = types.bool; type = types.bool;
default = cfg.profiles == [ ]; default = allProfilesExceptDefault == { };
example = false; example = false;
description = '' description = ''
Whether extensions can be installed or updated manually Whether extensions can be installed or updated manually
@ -259,30 +262,34 @@ in {
}; };
profiles = mkOption { profiles = mkOption {
type = types.listOf (profileType true); type = types.attrsOf profileType;
default = [ ]; default = { };
description = '' description = ''
A list of all VSCode profiles. Mutually exclusive A list of all VSCode profiles. Mutually exclusive
to programs.vscode.mutableExtensionsDir to programs.vscode.mutableExtensionsDir
''; '';
}; };
defaultProfile = mkOption {
type = profileType false;
default = { };
description = ''
The default VSCode profile.
'';
};
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
warnings = [ warnings = [
(mkIf (cfg.profiles != [ ] && cfg.mutableExtensionsDir) (mkIf (allProfilesExceptDefault != { } && cfg.mutableExtensionsDir)
"programs.vscode.mutableExtensionsDir can be used only if profiles is an empty list.") "programs.vscode.mutableExtensionsDir can be used only if no profiles apart from default are set.")
(mkIf ((filterAttrs (n: v:
(v ? enableExtensionUpdateCheck || v ? enableUpdateCheck)
&& (v.enableExtensionUpdateCheck != null || v.enableUpdateCheck
!= null)) allProfilesExceptDefault) != { })
"The option programs.vscode.profiles.*.enableExtensionUpdateCheck and option programs.vscode.profiles.*.enableUpdateCheck is invalid for all profiles except default.")
]; ];
home.packages = [ cfg.package ]; home.packages = [ cfg.package ];
# The file `${userDir}/globalStorage/storage.json` needs to be writable by VSCode,
# since it contains other data, such as theme backgrounds, recently opened folders, etc.
# A caveat of adding profiles this way is, VSCode has to be closed
# when this file is being written, since the file is loaded into RAM
# and overwritten on closing VSCode.
home.activation.vscodeProfiles = hm.dag.entryAfter [ "writeBoundary" ] (let home.activation.vscodeProfiles = hm.dag.entryAfter [ "writeBoundary" ] (let
modifyGlobalStorage = modifyGlobalStorage =
pkgs.writeShellScript "vscode-global-storage-modify" '' pkgs.writeShellScript "vscode-global-storage-modify" ''
@ -292,7 +299,10 @@ in {
if [ -f "$file" ]; then if [ -f "$file" ]; then
existing_profiles=$(jq '.userDataProfiles // [] | map({ (.name): .location }) | add // {}' $file) existing_profiles=$(jq '.userDataProfiles // [] | map({ (.name): .location }) | add // {}' $file)
file_write="" file_write=""
profiles=(${escapeShellArgs (map (v: v.name) cfg.profiles)}) profiles=(${
escapeShellArgs
(flatten (mapAttrsToList (n: v: n) allProfilesExceptDefault))
})
for profile in "''${profiles[@]}"; do for profile in "''${profiles[@]}"; do
if [[ "$(echo $existing_profiles | jq --arg profile $profile 'has ($profile)')" != "true" ]] || [[ "$(echo $existing_profiles | jq --arg profile $profile 'has ($profile)')" == "true" && "$(echo $existing_profiles | jq --arg profile $profile '.[$profile]')" != "\"$profile\"" ]]; then if [[ "$(echo $existing_profiles | jq --arg profile $profile 'has ($profile)')" != "true" ]] || [[ "$(echo $existing_profiles | jq --arg profile $profile 'has ($profile)')" == "true" && "$(echo $existing_profiles | jq --arg profile $profile '.[$profile]')" != "\"$profile\"" ]]; then
@ -308,58 +318,56 @@ in {
fi fi
if [ "$file_write" != "" ]; then if [ "$file_write" != "" ]; then
userDataProfiles=$(jq ".userDataProfiles += $(echo $file_write | jq -R 'split(" ") | map({ name: ., location: . })')" $file) userDataProfiles=$(jq ".userDataProfiles += $(echo $file_write | jq -R 'split("...") | map({ name: ., location: . })')" $file)
echo $userDataProfiles > $file echo $userDataProfiles > $file
fi fi
''; '';
in modifyGlobalStorage.outPath); in modifyGlobalStorage.outPath);
home.file = mkMerge (flatten [ home.file = mkMerge (flatten [
(map (v: (mapAttrsToList (n: v: [
let (mkIf ((mergedUserSettings v.userSettings v.enableUpdateCheck
# The default profile does not have the `name` key v.enableExtensionUpdateCheck) != { }) {
name = if v ? name then v.name else "default"; "${configFilePath n}".source =
in [
(mkIf ((mergedUserSettings v.userSettings) != { }) {
"${configFilePath name}".source =
jsonFormat.generate "vscode-user-settings" jsonFormat.generate "vscode-user-settings"
(mergedUserSettings v.userSettings); (mergedUserSettings v.userSettings v.enableUpdateCheck
v.enableExtensionUpdateCheck);
}) })
(mkIf (v.userTasks != { }) { (mkIf (v.userTasks != { }) {
"${tasksFilePath name}".source = "${tasksFilePath n}".source =
jsonFormat.generate "vscode-user-tasks" v.userTasks; jsonFormat.generate "vscode-user-tasks" v.userTasks;
}) })
(mkIf (v.keybindings != [ ]) { (mkIf (v.keybindings != [ ]) {
"${keybindingsFilePath name}".source = "${keybindingsFilePath n}".source =
jsonFormat.generate "vscode-keybindings" jsonFormat.generate "vscode-keybindings"
(map (filterAttrs (_: v: v != null)) v.keybindings); (map (filterAttrs (_: v: v != null)) v.keybindings);
}) })
(mkIf (v.languageSnippets != { }) (lib.mapAttrs' (language: snippet: (mkIf (v.languageSnippets != { }) (mapAttrs' (language: snippet:
lib.nameValuePair "${snippetDir name}/${language}.json" { nameValuePair "${snippetDir n}/${language}.json" {
source = source =
jsonFormat.generate "user-snippet-${language}.json" snippet; jsonFormat.generate "user-snippet-${language}.json" snippet;
}) v.languageSnippets)) }) v.languageSnippets))
(mkIf (v.globalSnippets != { }) { (mkIf (v.globalSnippets != { }) {
"${snippetDir name}/global.code-snippets".source = "${snippetDir n}/global.code-snippets".source =
jsonFormat.generate "user-snippet-global.code-snippets" jsonFormat.generate "user-snippet-global.code-snippets"
v.globalSnippets; v.globalSnippets;
}) })
]) allProfiles) ]) cfg.profiles)
# We write extensions.json for all profiles, except the default profile, # We write extensions.json for all profiles, except the default profile,
# since that is handled by code below. # since that is handled by code below.
(mkIf (cfg.profiles != [ ]) (listToAttrs (map (v: (mkIf (allProfilesExceptDefault != { }) (mapAttrs' (n: v:
nameValuePair "${userDir}/profiles/${v.name}/extensions.json" { nameValuePair "${userDir}/profiles/${n}/extensions.json" {
source = "${ source = "${
extensionJsonFile v.name (extensionJson v.extensions) extensionJsonFile n (extensionJson v.extensions)
}/share/vscode/extensions/extensions.json"; }/share/vscode/extensions/extensions.json";
}) cfg.profiles))) }) allProfilesExceptDefault))
(mkIf ((filter (v: v.extensions != [ ]) allProfiles) != [ ]) (let (mkIf (cfg.profiles != { }) (let
# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2 # Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2
subDir = "share/vscode/extensions"; subDir = "share/vscode/extensions";
toPaths = ext: toPaths = ext:
@ -368,13 +376,19 @@ in {
[ ext.vscodeExtUniqueId ] [ ext.vscodeExtUniqueId ]
else else
builtins.attrNames (builtins.readDir (ext + "/${subDir}"))); builtins.attrNames (builtins.readDir (ext + "/${subDir}")));
in if (cfg.mutableExtensionsDir && cfg.profiles == [ ]) then in if (cfg.mutableExtensionsDir && allProfilesExceptDefault == { }) then
mkMerge (concatMap toPaths (flatten (map (v: v.extensions) allProfiles)) # Mutable extensions dir can only occur when only default profile is set.
++ lib.optional (lib.versionAtLeast vscodeVersion "1.74.0") { # Force regenerating extensions.json using the below method,
# causes VSCode to create the extensions.json with all the extensions
# in the extension directory, which includes extensions from other profiles.
mkMerge (concatMap toPaths
(flatten (mapAttrsToList (n: v: v.extensions) cfg.profiles))
++ optional
(versionAtLeast vscodeVersion "1.74.0" && defaultProfile != { }) {
# Whenever our immutable extensions.json changes, force VSCode to regenerate # Whenever our immutable extensions.json changes, force VSCode to regenerate
# extensions.json with both mutable and immutable extensions. # extensions.json with both mutable and immutable extensions.
"${extensionPath}/.extensions-immutable.json" = { "${extensionPath}/.extensions-immutable.json" = {
text = extensionJson cfg.defaultProfile.extensions; text = extensionJson defaultProfile.extensions;
onChange = '' onChange = ''
run rm $VERBOSE_ARG -f ${extensionPath}/{extensions.json,.init-default-profile-extensions} run rm $VERBOSE_ARG -f ${extensionPath}/{extensions.json,.init-default-profile-extensions}
verboseEcho "Regenerating VSCode extensions.json" verboseEcho "Regenerating VSCode extensions.json"
@ -386,10 +400,11 @@ in {
"${extensionPath}".source = let "${extensionPath}".source = let
combinedExtensionsDrv = pkgs.buildEnv { combinedExtensionsDrv = pkgs.buildEnv {
name = "vscode-extensions"; name = "vscode-extensions";
paths = flatten (map (v: v.extensions) allProfiles) ++ lib.optional paths = (flatten (mapAttrsToList (n: v: v.extensions) cfg.profiles))
(lib.versionAtLeast vscodeVersion "1.74.0" && cfg.defaultProfile ++ optional
!= { }) (extensionJsonFile "default" (versionAtLeast vscodeVersion "1.74.0" && defaultProfile != { })
(extensionJson cfg.defaultProfile.extensions)); (extensionJsonFile "default"
(extensionJson defaultProfile.extensions));
}; };
in "${combinedExtensionsDrv}/${subDir}"; in "${combinedExtensionsDrv}/${subDir}";
})) }))

View file

@ -52,12 +52,12 @@ in {
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
programs.vscode.defaultProfile.userSettings = mkIf cfg.hie.enable { programs.vscode.profiles.default.userSettings = mkIf cfg.hie.enable {
"languageServerHaskell.enableHIE" = true; "languageServerHaskell.enableHIE" = true;
"languageServerHaskell.hieExecutablePath" = cfg.hie.executablePath; "languageServerHaskell.hieExecutablePath" = cfg.hie.executablePath;
}; };
programs.vscode.defaultProfile.extensions = programs.vscode.profiles.default.extensions =
[ pkgs.vscode-extensions.justusadam.language-haskell ] [ pkgs.vscode-extensions.justusadam.language-haskell ]
++ lib.optional cfg.hie.enable ++ lib.optional cfg.hie.enable
pkgs.vscode-extensions.alanz.vscode-hie-server; pkgs.vscode-extensions.alanz.vscode-hie-server;