From 6f7f29f672d266aab05a63cda04b0e056ccb73e9 Mon Sep 17 00:00:00 2001 From: Ross Brunton Date: Wed, 9 Oct 2024 01:34:51 +0100 Subject: [PATCH] vscode: Get paths from product.json The VSCode packages contain a product.json which is used to determine the extension and config dirs. Instead of hardcoding the package name, this change parses this file (which may appear in one of two places) and uses that to generate the paths. This hopefully allows more VSCode derivatives to "just work" without home-manager updates. --- modules/programs/vscode.nix | 24 +++++++++---------- tests/modules/programs/vscode/fake-pkg.nix | 9 +++++++ tests/modules/programs/vscode/keybindings.nix | 2 +- tests/modules/programs/vscode/snippets.nix | 2 +- tests/modules/programs/vscode/tasks.nix | 2 +- .../modules/programs/vscode/update-checks.nix | 2 +- 6 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 tests/modules/programs/vscode/fake-pkg.nix diff --git a/modules/programs/vscode.nix b/modules/programs/vscode.nix index f77937d66..027dfd3ab 100644 --- a/modules/programs/vscode.nix +++ b/modules/programs/vscode.nix @@ -11,19 +11,17 @@ let jsonFormat = pkgs.formats.json { }; - configDir = { - "vscode" = "Code"; - "vscode-insiders" = "Code - Insiders"; - "vscodium" = "VSCodium"; - "openvscode-server" = "OpenVSCode Server"; - }.${vscodePname}; + productInfoPath = + if pathExists "${cfg.package}/lib/vscode/resources/app/product.json" then + # Visual Studio Code, VSCodium + "${cfg.package}/lib/vscode/resources/app/product.json" + else + # OpenVSCode Server + "${cfg.package}/product.json"; + productInfo = lib.importJSON productInfoPath; - extensionDir = { - "vscode" = "vscode"; - "vscode-insiders" = "vscode-insiders"; - "vscodium" = "vscode-oss"; - "openvscode-server" = "openvscode-server"; - }.${vscodePname}; + configDir = productInfo.nameShort; + extensionDir = productInfo.dataFolderName; userDir = if pkgs.stdenv.hostPlatform.isDarwin then "Library/Application Support/${configDir}/User" @@ -37,7 +35,7 @@ let snippetDir = "${userDir}/snippets"; # TODO: On Darwin where are the extensions? - extensionPath = ".${extensionDir}/extensions"; + extensionPath = "${extensionDir}/extensions"; extensionJson = pkgs.vscode-utils.toExtensionJson cfg.extensions; extensionJsonFile = pkgs.writeTextFile { diff --git a/tests/modules/programs/vscode/fake-pkg.nix b/tests/modules/programs/vscode/fake-pkg.nix new file mode 100644 index 000000000..40bee0f3a --- /dev/null +++ b/tests/modules/programs/vscode/fake-pkg.nix @@ -0,0 +1,9 @@ +{ pkgs, ... }: +pkgs.writeTextFile { + name = "fake-vscode"; + destination = "/lib/vscode/resources/app/product.json"; + text = builtins.toJSON { + nameShort = "Code"; + dataFolderName = ".vscode"; + }; +} diff --git a/tests/modules/programs/vscode/keybindings.nix b/tests/modules/programs/vscode/keybindings.nix index 64212e29a..4cbffaadd 100644 --- a/tests/modules/programs/vscode/keybindings.nix +++ b/tests/modules/programs/vscode/keybindings.nix @@ -66,7 +66,7 @@ in { programs.vscode = { enable = true; keybindings = bindings; - package = pkgs.writeScriptBin "vscode" "" // { pname = "vscode"; }; + package = import ./fake-pkg.nix pkgs; }; nmt.script = '' diff --git a/tests/modules/programs/vscode/snippets.nix b/tests/modules/programs/vscode/snippets.nix index a7ea44f76..a9adea329 100644 --- a/tests/modules/programs/vscode/snippets.nix +++ b/tests/modules/programs/vscode/snippets.nix @@ -42,7 +42,7 @@ let in { programs.vscode = { enable = true; - package = pkgs.writeScriptBin "vscode" "" // { pname = "vscode"; }; + package = import ./fake-pkg.nix pkgs; globalSnippets = { fixme = { prefix = [ "fixme" ]; diff --git a/tests/modules/programs/vscode/tasks.nix b/tests/modules/programs/vscode/tasks.nix index ea85b3d6c..25e0e65ef 100644 --- a/tests/modules/programs/vscode/tasks.nix +++ b/tests/modules/programs/vscode/tasks.nix @@ -32,7 +32,7 @@ let in { programs.vscode = { enable = true; - package = pkgs.writeScriptBin "vscode" "" // { pname = "vscode"; }; + package = import ./fake-pkg.nix pkgs; userTasks = tasks; }; diff --git a/tests/modules/programs/vscode/update-checks.nix b/tests/modules/programs/vscode/update-checks.nix index 1547bd36a..c6351a987 100644 --- a/tests/modules/programs/vscode/update-checks.nix +++ b/tests/modules/programs/vscode/update-checks.nix @@ -17,7 +17,7 @@ let in { programs.vscode = { enable = true; - package = pkgs.writeScriptBin "vscode" "" // { pname = "vscode"; }; + package = import ./fake-pkg.nix pkgs; enableUpdateCheck = false; enableExtensionUpdateCheck = false; };