Jump to content

Visual Studio Code/zh: Difference between revisions

From Official NixOS Wiki
Ardenet (talk | contribs)
Created page with "Category:Applications"
Tags: Mobile edit Mobile web edit
FuzzyBot (talk | contribs)
Updating to match new version of source page
Line 1: Line 1:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
{{note|Visual Studio Code is [[FAQ/unfree|unfree]] because its license prohibits distribution. See the [[FAQ/unfree]] page to install unfree software.}}{{Infobox application
{{note|Visual Studio Code is [[Special:MyLanguage/FAQ/unfree|unfree]] because its license prohibits distribution. See the [[Special:MyLanguage/FAQ/unfree|FAQ unfree]] page to install unfree software.}}
</div>
 
{{Infobox application
| name = Visual Studio Code
| name = Visual Studio Code
| type = Text Editor
| type = Text Editor
Line 13: Line 16:
| initialRelease = 29 April 2015
| initialRelease = 29 April 2015
}}
}}
<div lang="en" dir="ltr" class="mw-content-ltr">
'''Visual Studio Code''' is a cross-platform text editor developed by Microsoft, built on the Electron framework.
'''Visual Studio Code''' is a cross-platform text editor developed by Microsoft, built on the Electron framework.
</div>
</div>


<div class="mw-translate-fuzzy">
对于 VS Code 代码库的自由分发(不含微软品牌、遥测,以及由 [https://open-vsx.org/ Open VSX] 替代的 [https://marketplace.visualstudio.com/ 扩展市场]),请参阅 [[VSCodium]]。
对于 VS Code 代码库的自由分发(不含微软品牌、遥测,以及由 [https://open-vsx.org/ Open VSX] 替代的 [https://marketplace.visualstudio.com/ 扩展市场]),请参阅 [[VSCodium]]。
</div>


<span id="Installation"></span>
<span id="Installation"></span>
Line 28: Line 35:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Extensions can be managed using {{Nixpkg|pkgs/applications/editors/vscode/with-extensions.nix|vscode-with-extensions}} :
Extensions can be managed using {{Nixpkg|pkgs/applications/editors/vscode/with-extensions.nix|vscode-with-extensions}}:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
{{Note|A limited number of extensions are available through <code>vscode-with-extensions</code>.
Other extensions need to be manually specified using <code>pkgs.vscode-utils.extensionsFromVscodeMarketplace</code>, or use [[#nix-vscode-extensions]] or [[#nix4vscode]].}}
</div>
<syntaxhighlight lang="nix">environment.systemPackages = with pkgs; [
<syntaxhighlight lang="nix">environment.systemPackages = with pkgs; [
   (vscode-with-extensions.override {
   (vscode-with-extensions.override {
Line 49: Line 60:
   })
   })
];</syntaxhighlight>
];</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 58: Line 68:
{{Note|For fetching the sha256 string you can use [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-store-prefetch-file <code>nix store prefetch-file</code>] like so:
{{Note|For fetching the sha256 string you can use [https://nix.dev/manual/nix/stable/command-ref/new-cli/nix3-store-prefetch-file <code>nix store prefetch-file</code>] like so:
</div>
</div>
<syntaxhighlight lang="console">$ nix store prefetch-file marketplace.visualstudio.com/_apis/public/gallery/publishers/<publisher>/vsextensions/<extension>/<version>/vspackage</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
Example:
</div>
<syntaxhighlight lang="console">$ nix store prefetch-file https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-vscode-remote/vsextensions/remote-ssh-edit/0.47.2/vspackage</syntaxhighlight>}}


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">nix store prefetch-file marketplace.visualstudio.com/_apis/public/gallery/publishers/<publisher>/vsextensions/<extension>/<version>/vspackage</syntaxhighlight>
=== nix-vscode-extensions ===
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Example:
<code>extensionsFromVscodeMarketplace</code> is a manual way to fetch extensions. However, to keep updated from upstream, [https://github.com/nix-community/nix-vscode-extensions nix-community/nix-vscode-extensions] provides the Nix expressions for the majority of available extensions from Open VSX and VSCode Marketplace. A GitHub Action updates the extensions daily.
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
To use nix-vscode-extensions, first add an overlay:
</div>
 
{{file|flake.nix|nix|3=
{
    inputs.nix-vscode-extensions.url = "github:nix-community/nix-vscode-extensions";
}
}}
 
{{file|/etc/nixos/configuration.nix|nix|3=
{ inputs, ... }: {
    nixpkgs.overlays = nix-vscode-extensions.overlays.default;
}
}}
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Now extensions may be specified using <code>pkgs.nix-vscode-extensions.open-vsx</code> or <code>pkgs.nix-vscode-extensions.vscode-marketplace</code>.
</div>
</div>
{{file|/etc/nixos/configuration.nix|nix|3=
environment.systemPackages = with pkgs; [
  (vscode-with-extensions.override {
    vscodeExtensions = (with pkgs.nix-vscode-extensions.open-vsx; [
      janisdd.vscode-edit-csv
    ]) ++ (with pkgs.nix-vscode-extensions.vscode-marketplace; [
      kamen.noctis-high-contrast
    ]);
  })
];
}}


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">nix store prefetch-file https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-vscode-remote/vsextensions/remote-ssh-edit/0.47.2/vspackage</syntaxhighlight>}}
=== nix4vscode ===
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
<code>extensionsFromVscodeMarketplace</code> is a manual way to fetch extensions. However, to keep updated from upstream, [https://github.com/nix-community/nix-vscode-extensions nix-community/nix-vscode-extensions] provides the Nix expressions for the majority of available extensions from Open VSX and VSCode Marketplace. A GitHub Action updates the extensions daily. Similarly, [https://github.com/nix-community/nix4vscode nix-community/nix4vscode] provides a Nix overlay for VSCode Marketplace and Open VSX extensions.
Similarly, [https://github.com/nix-community/nix4vscode nix-community/nix4vscode] provides a Nix overlay for VSCode Marketplace and Open VSX extensions.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Home Manager ===
=== Home Manager ===
</div>
<syntaxhighlight lang="nix">programs.vscode = {
<syntaxhighlight lang="nix">programs.vscode = {
   enable = true;
   enable = true;
Line 84: Line 136:
     yzhang.markdown-all-in-one
     yzhang.markdown-all-in-one
   ];
   ];
};</syntaxhighlight>
};
</div>
</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 94: Line 146:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Nix-env ===
=== Nix-env ===
{{Warning|Use of nix-env is unreproducible and thus discouraged and not recommended}}<syntaxhighlight lang="console">nix-env -iA nixos.vscode</syntaxhighlight>
{{Warning|Use of nix-env is unreproducible and thus discouraged and not recommended}}
</div>
</div>
<syntaxhighlight lang="console">$ nix-env -iA nixos.vscode</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Use VS Code extensions without additional configuration ==
== Use VS Code extensions without additional configuration ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
With {{Nixpkg|pkgs/applications/editors/vscode/generic.nix|vscode.fhs}}, the editor launches inside a [[wikipedia:Filesystem_Hierarchy_Standard|FHS]] compliant chroot environment using {{Manual|nixpkgs|sec-fhs-environments|<code>buildFHSEnv</code>}}. This reintroduces directories such as <code>/bin</code>, <code>/lib</code>, and <code>/usr</code>, which allows for extensions which ship pre-compiled binaries to work with little to no additional nixification.
With {{Nixpkg|pkgs/applications/editors/vscode/generic.nix|vscode.fhs}}, the editor launches inside a [[wikipedia:Filesystem_Hierarchy_Standard|FHS]] compliant chroot environment using {{Manual|nixpkgs|sec-fhs-environments|<code>buildFHSEnv</code>}}. This reintroduces directories such as <code>/bin</code>, <code>/lib</code>, and <code>/usr</code>, which allows for extensions which ship pre-compiled binaries to work with little to no additional nixification.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
{{note|{{Manual|nixpkgs|sec-fhs-environments|<code>buildFHSEnv</code>}} allows for ease-of-use at the cost of impurity, non-reproducibility, and denied root access (you cannot use <code>sudo</code> in <code>.fhs</code> packages, also see [https://discourse.nixos.org/t/sudo-does-not-work-from-within-vscode-fhs/14227])}}
{{note|{{Manual|nixpkgs|sec-fhs-environments|<code>buildFHSEnv</code>}} allows for ease-of-use at the cost of impurity, non-reproducibility, and denied root access (you cannot use <code>sudo</code> in <code>.fhs</code> packages, also see [https://discourse.nixos.org/t/sudo-does-not-work-from-within-vscode-fhs/14227 this discussion])}}
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== NixOS ===
=== NixOS ===
</div>
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
environment.systemPackages = with pkgs; [ vscode.fhs ];
environment.systemPackages = with pkgs; [ vscode.fhs ];
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Home Manager ===
=== Home Manager ===
</div>
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
programs.vscode = {
programs.vscode = {
Line 121: Line 181:
};
};
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Adding extension-specific dependencies, these will be added to the FHS environment:
Adding extension-specific dependencies, these will be added to the FHS environment:
</div>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# needed for rust lang server and rust-analyzer extension
# needed for rust lang server and rust-analyzer extension
Line 136: Line 197:
);
);
</syntaxhighlight>
</syntaxhighlight>
<div lang="en" dir="ltr" class="mw-content-ltr">
== Insiders Build ==
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Insiders Build ==
If you need to test a recent code change, you can run the insiders build. It is designed to run alongside the main build, with a separate <code>code-insiders</code> command and a different config path, so you can leave your main VS Code instance installed/running.
If you need to test a recent code change, you can run the insiders build. It is designed to run alongside the main build, with a separate <code>code-insiders</code> command and a different config path, so you can leave your main VS Code instance installed/running.
</div>
</div>
Line 147: Line 210:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec {
(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec {
Line 155: Line 217:
   });
   });
   version = "latest";
   version = "latest";
</div>


   <div lang="en" dir="ltr" class="mw-content-ltr">
   buildInputs = oldAttrs.buildInputs ++ [ pkgs.krb5 ];
buildInputs = oldAttrs.buildInputs ++ [ pkgs.krb5 ];
});
});
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 169: Line 228:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Updating insiders placeholder <code>sha256</code> ===
=== Updating insiders placeholder <code>sha256</code> ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
You will need to update the placeholder <code>sha256</code> value for each new Insiders build.
You will need to update the placeholder <code>sha256</code> value for each new Insiders build.
</div>
</div>
Line 186: Line 248:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
==== For <code>sha256:</code> ====
==== For <code>sha256:</code> ====
Example:<pre>//-- ...
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Example:
</div>
 
<pre>//-- ...
       error: hash mismatch in file downloaded from 'https://code.visualstudio.com/sha/download?build=insider&os=linux-x64':
       error: hash mismatch in file downloaded from 'https://code.visualstudio.com/sha/download?build=insider&os=linux-x64':
         specified: sha256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         specified: sha256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         got:      sha256:16fzxqs6ql4p2apq9aw7l10h4ag1r7jwlfvknk5rd2zmkscwhn6z
         got:      sha256:16fzxqs6ql4p2apq9aw7l10h4ag1r7jwlfvknk5rd2zmkscwhn6z
//-- ...</pre>
//-- ...</pre>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 203: Line 270:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
===== For <code>sha256-</code>  =====
===== For <code>sha256-</code>  =====
Example:<pre>//-- ...
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Example:
</div>
 
<pre>//-- ...
         error: hash mismatch in fixed-output derivation '/nix/store/path':
         error: hash mismatch in fixed-output derivation '/nix/store/path':
         specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
         specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
             got:    sha256-aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg=
             got:    sha256-aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg=
//-- ...</pre>
//-- ...</pre>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 218: Line 290:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import base64
import base64
Line 224: Line 295:
print(base64.decodebytes(text).hex())
print(base64.decodebytes(text).hex())
</syntaxhighlight>
</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 232: Line 302:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
== Creating development environments using nix-shell ==
== Creating development environments using nix-shell ==
Instead of using configuration.nix to add packages (e.g. Python or NodeJS) for developing code on VSCode, you can instead use nix-shell. This will allow you to seamlessly create development environments with the correct packages for your project, without rebuilding and restarting NixOS. See [[Development_environment_with_nix-shell | this page]] for further instructions in building nix-shell development environments.
</div>
 
<div lang="en" dir="ltr" class="mw-content-ltr">
Instead of using configuration.nix to add packages (e.g. Python or NodeJS) for developing code on VSCode, you can instead use nix-shell. This will allow you to seamlessly create development environments with the correct packages for your project, without rebuilding and restarting NixOS. See [[Special:MyLanguage/Development_environment_with_nix-shell|this page]] for further instructions in building nix-shell development environments.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Automatically switch nix shells when switching projects ===
=== Automatically switch nix shells when switching projects ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
You can do this by using [https://github.com/nix-community/nix-direnv nix-direnv] and [https://marketplace.visualstudio.com/items?itemName=mkhl.direnv the VSCode extension direnv] for integration. View the nix-direnv github page linked for a guide on setting it up.
You can do this by using [https://github.com/nix-community/nix-direnv nix-direnv] and [https://marketplace.visualstudio.com/items?itemName=mkhl.direnv the VSCode extension direnv] for integration. View the nix-direnv github page linked for a guide on setting it up.
</div>
</div>
Line 242: Line 318:
<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Alternative for manually switching shells ===
=== Alternative for manually switching shells ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
The extension [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector nix-env-selector] will make switching between different nix-shell environments within VSCode so you can switch between different coding projects easily and manually. It has a guide for setting up nix-shell environments for VSCode.
The extension [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector nix-env-selector] will make switching between different nix-shell environments within VSCode so you can switch between different coding projects easily and manually. It has a guide for setting up nix-shell environments for VSCode.
</div>
</div>
Line 254: Line 333:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
See [[Wayland#Electron and Chromium]]
See [[Special:MyLanguage/Wayland#Electron and Chromium|this]].
</div>
</div>


Line 266: Line 345:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
To run it in your current directory:<syntaxhighlight lang="console">curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh</syntaxhighlight>
To run it in your current directory:
</div>
</div>
<syntaxhighlight lang="console">$ curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh</syntaxhighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
=== Example output ===
=== Example output ===
<syntaxhighlight lang="console">curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh
</div>
 
<syntaxhighlight lang="console">$ curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh
{ extensions = [
{ extensions = [
   {
   {
Line 287: Line 370:
];
];
}</syntaxhighlight>
}</syntaxhighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 310: Line 392:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Note that nix-vscode-server works as of 8/21/21 but is occasionally broken (See https://github.com/msteen/nixos-vscode-server/pull/3, https://github.com/msteen/nixos-vscode-server/pull/4, https://github.com/msteen/nixos-vscode-server/pull/5). Here's a workaround: Install the <code>nodejs-16_x</code> package on the NixOS host, and then run the following nix-shell script:
Note that nix-vscode-server works as of 8/21/21 but is occasionally broken (See [https://github.com/msteen/nixos-vscode-server/pull/3], [https://github.com/msteen/nixos-vscode-server/pull/4], [https://github.com/msteen/nixos-vscode-server/pull/5]). Here's a workaround: Install the <code>nodejs-16_x</code> package on the NixOS host, and then run the following nix-shell script:
</div>
 
<syntaxHighlight lang=haskell>
<syntaxHighlight lang=haskell>
#! /usr/bin/env nix-shell
#! /usr/bin/env nix-shell
#! nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ pkgs.turtle ])"
#! nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ pkgs.turtle ])"
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import Turtle
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
main = sh $ do
main = sh $ do
   homedir <- home
   homedir <- home
Line 335: Line 415:
       echo ("Already fixed " <> repr subdir)
       echo ("Already fixed " <> repr subdir)
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VS Code version, then you can install the <code>nodejs-16_x</code> package on the NixOS host and replace the VS Code provided version. This workaround is described here: https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523. Note that NodeJS needs to be updated according to VS Code upstream requirements (NodeJS 16 required from 4/2022).
If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VS Code version, then you can install the <code>nodejs-16_x</code> package on the NixOS host and replace the VS Code provided version. This workaround is described [https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523 here]. Note that NodeJS needs to be updated according to VS Code upstream requirements (NodeJS 16 required from 4/2022).
</div>
</div>


Line 349: Line 428:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  programs.nix-ld.enable = true;
programs.nix-ld.enable = true;
</nowiki>}}
</nowiki>}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 374: Line 451:
Similar to SSH hosts, both <code>nix-vscode-server</code> and <code>nix-ld</code> solution allows a VSCode Windows client to connect a [https://github.com/nix-community/NixOS-WSL NixOS-WSL] host. However, by default the VSCode Windows client uses <code>wsl.exe --exec</code> to start the code server, which bypasses NixOS environment variables required by <code>nix-ld</code>, resulting in failures.
Similar to SSH hosts, both <code>nix-vscode-server</code> and <code>nix-ld</code> solution allows a VSCode Windows client to connect a [https://github.com/nix-community/NixOS-WSL NixOS-WSL] host. However, by default the VSCode Windows client uses <code>wsl.exe --exec</code> to start the code server, which bypasses NixOS environment variables required by <code>nix-ld</code>, resulting in failures.
</div>  
</div>  


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 380: Line 456:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{file|wslDaemon.js|js|<nowiki>
{{file|wslDaemon.js|js|<nowiki>
.push("sh","-c"
.push("sh","-c"
</nowiki>}}
</nowiki>}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 391: Line 464:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{file|wslDaemon.js|js|<nowiki>
{{file|wslDaemon.js|js|<nowiki>
.push("sh","-l","-c"
.push("sh","-l","-c"
</nowiki>}}
</nowiki>}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 402: Line 473:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
See https://github.com/nix-community/NixOS-WSL/issues/222 for the discussion about <code>wsl --exec</code> issue on NixOS-WSL.
See [https://github.com/nix-community/NixOS-WSL/issues/222 this] for the discussion about <code>wsl --exec</code> issue on NixOS-WSL.
See https://github.com/microsoft/vscode-remote-release/issues/8305#issuecomment-1661396267 about the workaround.
See [https://github.com/microsoft/vscode-remote-release/issues/8305#issuecomment-1661396267 this] about the workaround.
</div>
</div>


Line 415: Line 486:


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
If you get an error similar to the following, enable a [[Secret Service]] provider.
If you get an error similar to the following, enable a [[Special:MyLanguage/Secret Service|Secret Service]] provider.
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<pre>Writing login information to the keychain failed with error 'The name org.freedesktop.secret was not provided by any .service files'.</pre>
<pre>Writing login information to the keychain failed with error 'The name org.freedesktop.secret was not provided by any .service files'.</pre>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 426: Line 495:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<blockquote>
<blockquote>
Server did not start successfully. Full server log at /home/user/.vscode-server/.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.log<br>
Server did not start successfully. Full server log at /home/user/.vscode-server/.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.log<br>
Line 433: Line 501:
/home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/bin/code-server: line 12: /home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/node: No such file or directory
/home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/bin/code-server: line 12: /home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/node: No such file or directory
</blockquote>
</blockquote>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 439: Line 506:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<pre>
$ ldd ~/.vscode-server/bin/*/node
ldd ~/.vscode-server/bin/*/node
</syntaxhighlight>
</pre>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 449: Line 514:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<pre>
$ ~/.vscode-server/bin/*/node
~/.vscode-server/bin/*/node
</syntaxhighlight>
</pre>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 459: Line 522:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxhighlight lang="console">
<pre>
$ nix-env -iA nixos.nodejs-16_x
nix-env -iA nixos.nodejs-16_x
</syntaxhighlight>
</pre>
 
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
#! /bin/sh
#! /bin/sh
Line 480: Line 541:
fi
fi
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 490: Line 550:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=json>
<syntaxHighlight lang=bash>
"remote.SSH.useLocalServer": false
"remote.SSH.useLocalServer": false
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">
See https://github.com/microsoft/vscode-remote-release/issues/2509 and https://github.com/nix-community/nixos-vscode-server/issues/18
See [https://github.com/microsoft/vscode-remote-release/issues/2509] and [https://github.com/nix-community/nixos-vscode-server/issues/18]
</div>
</div>


[[Category:Applications]]
[[Category:Applications]]
[[Category:IDE]]
[[Category:IDE]]

Revision as of 18:05, 5 October 2026

Note: Visual Studio Code is unfree because its license prohibits distribution. See the FAQ unfree page to install unfree software.

Visual Studio Code

Text Editor Application

100%
Developer(s)Microsoft
Initial release29 April 2015
Operating SystemLinux, macOS, Windows
Language(s)TypeScript, JavaScript, HTML, CSS
External links
Websitehttps://code.visualstudio.com/
GitHubhttps://github.com/microsoft/vscode
Bug trackerhttps://github.com/microsoft/vscode/issues
Documentationhttps://code.visualstudio.com/docs

Visual Studio Code is a cross-platform text editor developed by Microsoft, built on the Electron framework.

对于 VS Code 代码库的自由分发(不含微软品牌、遥测,以及由 Open VSX 替代的 扩展市场),请参阅 VSCodium。

安装

NixOS

environment.systemPackages = with pkgs; [ vscode ];

Extensions can be managed using 📦︎ vscode-with-extensions:

Note: A limited number of extensions are available through vscode-with-extensions. Other extensions need to be manually specified using pkgs.vscode-utils.extensionsFromVscodeMarketplace, or use #nix-vscode-extensions or #nix4vscode.
environment.systemPackages = with pkgs; [
  (vscode-with-extensions.override {
    vscodeExtensions = with vscode-extensions; [
      jnoortheen.nix-ide
      ms-python.python
      ms-azuretools.vscode-docker
      ms-vscode-remote.remote-ssh
    ] ++ pkgs.vscode-utils.extensionsFromVscodeMarketplace [
      {
        name = "remote-ssh-edit";
        publisher = "ms-vscode-remote";
        version = "0.47.2";
        sha256 = "1hp6gjh4xp2m1xlm1jsdzxw9d8frkiidhph6nvl24d0h8z34w49g";
      }
    ];
  })
];
Note: {{{1}}}

nix-vscode-extensions

extensionsFromVscodeMarketplace is a manual way to fetch extensions. However, to keep updated from upstream, nix-community/nix-vscode-extensions provides the Nix expressions for the majority of available extensions from Open VSX and VSCode Marketplace. A GitHub Action updates the extensions daily.

To use nix-vscode-extensions, first add an overlay:

❄︎ flake.nix
{
    inputs.nix-vscode-extensions.url = "github:nix-community/nix-vscode-extensions";
}
❄︎ /etc/nixos/configuration.nix
{ inputs, ... }: {
    nixpkgs.overlays = nix-vscode-extensions.overlays.default;
}

Now extensions may be specified using pkgs.nix-vscode-extensions.open-vsx or pkgs.nix-vscode-extensions.vscode-marketplace.

❄︎ /etc/nixos/configuration.nix
environment.systemPackages = with pkgs; [
  (vscode-with-extensions.override {
    vscodeExtensions = (with pkgs.nix-vscode-extensions.open-vsx; [
      janisdd.vscode-edit-csv
    ]) ++ (with pkgs.nix-vscode-extensions.vscode-marketplace; [
      kamen.noctis-high-contrast
    ]);
  })
];

nix4vscode

Similarly, nix-community/nix4vscode provides a Nix overlay for VSCode Marketplace and Open VSX extensions.

Home Manager

programs.vscode = {
  enable = true;
  profiles.default.extensions = with pkgs.vscode-extensions; [
    dracula-theme.theme-dracula
    vscodevim.vim
    yzhang.markdown-all-in-one
  ];
};

Nix-env

⚠︎
Warning: Use of nix-env is unreproducible and thus discouraged and not recommended
$ nix-env -iA nixos.vscode

Use VS Code extensions without additional configuration

With 📦︎ vscode.fhs, the editor launches inside a FHS compliant chroot environment using 📖︎ buildFHSEnv. This reintroduces directories such as /bin, /lib, and /usr, which allows for extensions which ship pre-compiled binaries to work with little to no additional nixification.

Note: 📖︎ buildFHSEnv allows for ease-of-use at the cost of impurity, non-reproducibility, and denied root access (you cannot use sudo in .fhs packages, also see this discussion)

NixOS

environment.systemPackages = with pkgs; [ vscode.fhs ];

Home Manager

programs.vscode = {
  enable = true;
  package = pkgs.vscode.fhs;
};

Adding extension-specific dependencies, these will be added to the FHS environment:

# needed for rust lang server and rust-analyzer extension
programs.vscode.package = pkgs.vscode.fhsWithPackages (
  ps: with ps; [
    rustup
    zlib
    openssl.dev
    pkg-config
  ]
);

Insiders Build

If you need to test a recent code change, you can run the insiders build. It is designed to run alongside the main build, with a separate code-insiders command and a different config path, so you can leave your main VS Code instance installed/running.

The following derivation builds a package with the latest insiders build (on NixOS in environment.systemPackages, or on Home Manager in home.packages) :

(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec {
  src = (builtins.fetchTarball {
    url = "https://code.visualstudio.com/sha/download?build=insider&os=linux-x64";
    sha256 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  });
  version = "latest";

  buildInputs = oldAttrs.buildInputs ++ [ pkgs.krb5 ];
});

Updating insiders placeholder sha256

You will need to update the placeholder sha256 value for each new Insiders build.

The new value will appear in a validation error when you try to build.

Put an arbitrary placeholder value in the sha256 field, try to build and you'll get an error message regarding the sha256 value.

Resolving the "hash mismatch" error

For sha256:

Example:

//-- ...
       error: hash mismatch in file downloaded from 'https://code.visualstudio.com/sha/download?build=insider&os=linux-x64':
         specified: sha256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         got:       sha256:16fzxqs6ql4p2apq9aw7l10h4ag1r7jwlfvknk5rd2zmkscwhn6z
//-- ...

Copy the "got" value (while stripping out the prepended "sha256:"), and paste it in your placeholder sha256 value

Resulting: "16fzxqs6ql4p2apq9aw7l10h4ag1r7jwlfvknk5rd2zmkscwhn6z"

For sha256-

Example:

//-- ...
        error: hash mismatch in fixed-output derivation '/nix/store/path':
         specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
            got:    sha256-aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg=
//-- ...

Copy the "got" value (while stripping out the prepended "sha256-")

Then, run the following python script:

import base64
text = b'aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg=' # Replace the string in between ' with the according hash
print(base64.decodebytes(text).hex())

Its output should be pasted in your placeholder sha256 value

Creating development environments using nix-shell

Instead of using configuration.nix to add packages (e.g. Python or NodeJS) for developing code on VSCode, you can instead use nix-shell. This will allow you to seamlessly create development environments with the correct packages for your project, without rebuilding and restarting NixOS. See this page for further instructions in building nix-shell development environments.

Automatically switch nix shells when switching projects

You can do this by using nix-direnv and the VSCode extension direnv for integration. View the nix-direnv github page linked for a guide on setting it up.

Alternative for manually switching shells

The extension nix-env-selector will make switching between different nix-shell environments within VSCode so you can switch between different coding projects easily and manually. It has a guide for setting up nix-shell environments for VSCode.

Wayland

To make sure VSCode runs on Wayland native instead of Xwayland, you can make it use Ozone Wayland by setting the environment variable NIXOS_OZONE_WL with NIXOS_OZONE_WL=1

See this.

Updating extension versions

The Nixpkgs vscode extensions directory contains a script which use code --list-extensions and lookup the latest versions of those extensions to outputs a nix expression with a single extensions attribute of the same type as extensionsFromVscodeMarketplace inputs.

To run it in your current directory:

$ curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh

Example output

$ curl -fsSL https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh | sh
{ extensions = [
  {
    name = "project-manager";
    publisher = "alefragnani";
    version = "12.4.0";
    sha256 = "0q6zkz7pqz2prmr01h17h9a5q6cn6bjgcxggy69c84j8h2w905wy";
  }
  {
    name = "githistory";
    publisher = "donjayamanne";
    version = "0.6.18";
    sha256 = "01lc9gpqdjy6himn7jsfjrfz8xrk728c20903lxkxy5fliv232gz";
  }
];
}

Remote SSH

The remote-ssh extension works by connecting to a remote host and downloading scripts and pre-built binaries to $HOME/.vscode-server. When first launching remote-ssh for a NixOS host, the connection will fail due to the provided node.js not having been built for a NixOS system (the dynamic libraries aren't in the same place).

Any client to NixOS host

tl;dr Use nix-vscode-server or nix-ld on host machines.

nix-vscode-server

Note that nix-vscode-server works as of 8/21/21 but is occasionally broken (See [1], [2], [3]). Here's a workaround: Install the nodejs-16_x package on the NixOS host, and then run the following nix-shell script:

#! /usr/bin/env nix-shell
#! nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ pkgs.turtle ])"

{-# LANGUAGE OverloadedStrings #-}
import Turtle

main = sh $ do
  homedir <- home
  subdir <- ls $ homedir </> ".vscode-server/bin/"
  let nodepath = subdir </> "node"
  badnode <- isNotSymbolicLink nodepath
  if badnode
    then do
      mv nodepath (subdir </> "node_backup")
      symlink "/run/current-system/sw/bin/node" nodepath
      echo ("Fixed " <> repr subdir)
    else do
      echo ("Already fixed " <> repr subdir)

If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VS Code version, then you can install the nodejs-16_x package on the NixOS host and replace the VS Code provided version. This workaround is described here. Note that NodeJS needs to be updated according to VS Code upstream requirements (NodeJS 16 required from 4/2022).

nix-ld

Add the following settings to configuration.nix on the NixOS host

❄︎ /etc/nixos/configuration.nix
programs.nix-ld.enable = true;

Then run nixos-rebuild switch to enable nix-ld. Unlike the nix-vscode-server solution, the nix-ld solution also enables VSCode extensions even if they include non-Nix binaries.

Nix-sourced VS Code to NixOS host

If vscode-remote is installed from nix (vscode-extensions.ms-vscode-remote as above) on the client machine, everything should "just work".

Remote WSL

Similar to SSH hosts, both nix-vscode-server and nix-ld solution allows a VSCode Windows client to connect a NixOS-WSL host. However, by default the VSCode Windows client uses wsl.exe --exec to start the code server, which bypasses NixOS environment variables required by nix-ld, resulting in failures.

As a workaround, search for the following text in all files under the directory $HOME\.vscode\extensions\

≡︎ wslDaemon.js
.push("sh","-c"

Replace it with

≡︎ wslDaemon.js
.push("sh","-l","-c"

Then restart VS Code and your VS Code client should be able to connect to NixOS host

See this for the discussion about wsl --exec issue on NixOS-WSL. See this about the workaround.

Troubleshooting

Writing login information to the keychain failed

If you get an error similar to the following, enable a Secret Service provider.

Writing login information to the keychain failed with error 'The name org.freedesktop.secret was not provided by any .service files'.

Server did not start successfully

Server did not start successfully. Full server log at /home/user/.vscode-server/.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.log

server log:
/home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/bin/code-server: line 12: /home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/node: No such file or directory

"No such file or directory" means that libc is not found, see

$ ldd ~/.vscode-server/bin/*/node

try to run the node binary on the server

$ ~/.vscode-server/bin/*/node

if this fails, install node version 16, and try to patch the node binary

$ nix-env -iA nixos.nodejs-16_x


#! /bin/sh
# fix-vscode-server-node.sh
# https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523
cd ~/.vscode-server/bin/*
if ! ./node -e "require('process').exit(0)"
then
  echo patching node binary $(readlink -f node)
  rm node
  ln -s $(which node)
else
  echo node is working $(readlink -f node)
fi

Timeout

If you're using fish or other shell, set this in the user settings JSON on the client machine:

"remote.SSH.useLocalServer": false

See [4] and [5]