27 # Populate passthru.tests
30 # needed to fix "Save as Root"
34 # Attributes inherit from specific versions
48 sourceExecutableName ? executableName,
49 useVSCodeRipgrep ? false,
57 # Vscode and variants allow for users to download and use extensions
58 # which often include the usage of pre-built binaries.
59 # This has been an on-going painpoint for many users, as
60 # a full extension update cycle has to be done through nixpkgs
61 # in order to create or update extensions.
62 # See: #83288 #91179 #73810 #41189
64 # buildFHSEnv allows for users to use the existing vscode
65 # extension tooling without significant pain.
68 additionalPkgs ? pkgs: [ ],
71 # also determines the name of the wrapped command
72 pname = executableName;
75 # additional libraries which are commonly needed for extensions
79 # ld-linux-x86-64-linux.so.2 and others
94 ++ additionalPkgs pkgs;
97 "--bind-try /etc/nixos/ /etc/nixos/"
100 # symlink shared assets, including icons and desktop entries
101 extraInstallCommands = ''
102 ln -s "${finalAttrs.finalPackage}/share" "$out/"
105 runScript = "${finalAttrs.finalPackage}/bin/${executableName}";
107 # vscode likes to kill the parent so that the
108 # gui application isn't attached to the terminal session
109 dieWithParent = false;
112 inherit executableName;
113 inherit (finalAttrs.finalPackage) pname version; # for home-manager module
118 Wrapped variant of ${pname} which launches in a FHS compatible environment.
119 Should allow for easy usage of extensions without nix-specific modifications.
143 fhsWithPackages = f: fhs { additionalPkgs = f; };
145 // lib.optionalAttrs (vscodeServer != null) {
146 inherit rev vscodeServer;
151 name = executableName;
152 desktopName = longName;
153 comment = "Code Editing. Redefined.";
154 genericName = "Text Editor";
155 exec = "${executableName} %F";
156 icon = "vs${executableName}";
157 startupNotify = true;
158 startupWMClass = shortName;
165 keywords = [ "vscode" ];
166 actions.new-empty-window = {
167 name = "New Empty Window";
168 exec = "${executableName} --new-window %F";
169 icon = "vs${executableName}";
173 name = executableName + "-url-handler";
174 desktopName = longName + " - URL Handler";
175 comment = "Code Editing. Redefined.";
176 genericName = "Text Editor";
177 exec = executableName + " --open-url %U";
178 icon = "vs${executableName}";
179 startupNotify = true;
180 startupWMClass = shortName;
187 mimeTypes = [ "x-scheme-handler/vs${executableName}" ];
188 keywords = [ "vscode" ];
199 ++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
210 runtimeDependencies = lib.optionals stdenv.hostPlatform.isLinux [
220 ++ lib.optionals stdenv.hostPlatform.isLinux [
224 # override doesn't preserve splicing https://github.com/NixOS/nixpkgs/issues/132651
225 # Has to use `makeShellWrapper` from `buildPackages` even though `makeShellWrapper` from the inputs is spliced because `propagatedBuildInputs` would pick the wrong one because of a different offset.
226 (buildPackages.wrapGAppsHook3.override { makeWrapper = buildPackages.makeShellWrapper; })
230 dontConfigure = true;
231 noDumpEnvVars = true;
238 if stdenv.hostPlatform.isDarwin then
240 mkdir -p "$out/Applications/${longName}.app" "$out/bin"
241 cp -r ./* "$out/Applications/${longName}.app"
242 ln -s "$out/Applications/${longName}.app/Contents/Resources/app/bin/${sourceExecutableName}" "$out/bin/${executableName}"
246 mkdir -p "$out/lib/vscode" "$out/bin"
247 cp -r ./* "$out/lib/vscode"
249 ln -s "$out/lib/vscode/bin/${sourceExecutableName}" "$out/bin/${executableName}"
251 # These are named vscode.png, vscode-insiders.png, etc to match the name in upstream *.deb packages.
252 mkdir -p "$out/share/pixmaps"
253 cp "$out/lib/vscode/resources/app/resources/linux/code.png" "$out/share/pixmaps/vs${executableName}.png"
255 # Override the previously determined VSCODE_PATH with the one we know to be correct
256 sed -i "/ELECTRON=/iVSCODE_PATH='$out/lib/vscode'" "$out/bin/${executableName}"
257 grep -q "VSCODE_PATH='$out/lib/vscode'" "$out/bin/${executableName}" # check if sed succeeded
259 # Remove native encryption code, as it derives the key from the executable path which does not work for us.
260 # The credentials should be stored in a secure keychain already, so the benefit of this is questionable
261 # in the first place.
262 rm -rf $out/lib/vscode/resources/app/node_modules/vscode-encrypt
272 # we cannot use runtimeDependencies otherwise libdbusmenu do not work on kde
273 lib.optionalString stdenv.hostPlatform.isLinux
274 "--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ libdbusmenu ]}"
276 # Add gio to PATH so that moving files to the trash works when not using a desktop environment
277 --prefix PATH : ${glib.bin}/bin
278 --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}"
279 --add-flags ${lib.escapeShellArg commandLineArgs}
283 # See https://github.com/NixOS/nixpkgs/issues/49643#issuecomment-873853897
284 # linux only because of https://github.com/NixOS/nixpkgs/issues/138729
286 lib.optionalString stdenv.hostPlatform.isLinux ''
287 # this is a fix for "save as root" functionality
288 packed="resources/app/node_modules.asar"
289 unpacked="resources/app/node_modules"
290 asar extract "$packed" "$unpacked"
291 substituteInPlace $unpacked/@vscode/sudo-prompt/index.js \
292 --replace "/usr/bin/pkexec" "/run/wrappers/bin/pkexec" \
293 --replace "/bin/bash" "${bash}/bin/bash"
296 # without this symlink loading JsChardet, the library that is used for auto encoding detection when files.autoGuessEncoding is true,
297 # fails to load with: electron/js2c/renderer_init: Error: Cannot find module 'jschardet'
298 # and the window immediately closes which renders VSCode unusable
299 # see https://github.com/NixOS/nixpkgs/issues/152939 for full log
300 ln -rs "$unpacked" "$packed"
305 if stdenv.hostPlatform.isDarwin then
306 if lib.versionAtLeast version "1.94.0" then
307 "Contents/Resources/app/node_modules/@vscode/ripgrep/bin/rg"
309 "Contents/Resources/app/node_modules.asar.unpacked/@vscode/ripgrep/bin/rg"
311 "resources/app/node_modules/@vscode/ripgrep/bin/rg";
313 if !useVSCodeRipgrep then
316 ln -s ${ripgrep}/bin/rg ${vscodeRipgrep}
320 chmod +x ${vscodeRipgrep}
324 postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
326 --add-needed ${libglvnd}/lib/libGLESv2.so.2 \
327 --add-needed ${libglvnd}/lib/libGL.so.1 \
328 --add-needed ${libglvnd}/lib/libEGL.so.1 \
329 $out/lib/vscode/${executableName}