Visual Studio Code: Difference between revisions
imported>Edrex Add instructions for installing insiders build |
imported>SuperSamus Reorder and cleanup |
||
Line 1: | Line 1: | ||
{{note|Visual Studio Code is [[FAQ/unfree|unfree]], its license prohibits distribution. See the [[FAQ/unfree]] page to install unfree software.}} | {{note|Visual Studio Code is [[FAQ/unfree|unfree]], its license prohibits distribution. See the [[FAQ/unfree]] page to install unfree software.}} | ||
Visual Studio Code is a cross-platform text editor developed by Microsoft, built on the Electron framework. | |||
For the free distribution of the VS Code codebase (without MS branding/telemetry) see [[VSCodium]]. | |||
== | == Installation == | ||
=== NixOS === | |||
<syntaxHighlight lang=nix> | |||
environment.systemPackages = with pkgs; [ vscode ]; | |||
</syntaxHighlight> | |||
Extensions can be managed using the 'vscode-with-extensions' package: | |||
<syntaxHighlight lang=nix> | |||
environment.systemPackages = with pkgs; [ | |||
vscode-with-extensions.override { | |||
vscodeExtensions = with vscode-extensions; [ | |||
bbenoist.nix | |||
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"; | |||
} | |||
]; | |||
} | |||
]; | |||
</syntaxHighlight> | |||
Some examples here: [https://github.com/search?q=extensionFromVscodeMarketplace&type=code GitHub search for "extensionFromVscodeMarketplace"] | |||
It's also possible to install VS Code via [[Home Manager]]: | |||
... | <syntaxhighlight lang="nix"> | ||
} | programs.vscode = { | ||
</ | enable = true; | ||
extensions = with pkgs.vscode-extensions; [ | |||
dracula-theme.theme-dracula | |||
vscodevim.vim | |||
yzhang.markdown-all-in-one | |||
]; | |||
}; | |||
</syntaxhighlight> | |||
* See for more options: [https://nix-community.github.io/home-manager/options.html#opt-programs.vscode.enable Home Manager Manual: Options - programs.vscode] | * See for more options: [https://nix-community.github.io/home-manager/options.html#opt-programs.vscode.enable Home Manager Manual: Options - programs.vscode] | ||
* Search for extensions with configurations: [https://search.nixos.org/packages? | * Search for extensions with configurations: [https://search.nixos.org/packages?type=packages&query=vscode-extensions NixOS Search: vscode-extensions] | ||
=== | === Non-NixOS === | ||
<syntaxHighlight lang="console"> | |||
$ nix-env -iA nixos.vscode | |||
</syntaxHighlight> | |||
=== Use VS Code extensions without additional configuration === | |||
With the package vscode-fhs, the editor launches inside a [https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard FHS] compliant chroot environment using buildFHSUserEnv. 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|From a philosophical view, use of buildFHSUserEnv allows for ease-of-use at the cost of some impurity and non-reproducibility. If you prioritize purely-declarative configurations, please stay with the above guidance.}} | |||
Example usage: | |||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
environment.systemPackages = with pkgs; [ vscode-fhs ]; | |||
</syntaxHighlight> | </syntaxHighlight> | ||
Home-manager: | |||
<syntaxHighlight lang=nix> | |||
programs.vscode = { | |||
enable = true; | |||
package = pkgs.vscode-fhs; | |||
}; | |||
</syntaxHighlight> | |||
== | Adding extension-specific dependencies, these will be added to the FHS environment: | ||
<syntaxHighlight lang=nix> | |||
# needed for rust lang server extension | |||
programs.vscode.package = pkgs.vscode-fhsWithPackages (ps: with ps; [ rustup zlib ]); | |||
</syntaxHighlight> | |||
=== 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. | ||
</ | |||
The following derivation [https://discourse.nixos.org/t/how-to-install-latest-vscode-insiders/7895/4 thanks to @jnoortheen], which you can add to <code>home.packages</code> (HM), <code>environment.systemPackages</code> (NixOS), etc., builds a package with the latest insiders. | |||
<syntaxHighlight lang= | <syntaxHighlight lang=nix> | ||
(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec { | |||
src = (builtins.fetchTarball { | |||
url = "https://update.code.visualstudio.com/latest/linux-x64/insider"; | |||
sha256 = "1dajhfsdr55mfnj12clf5apy1d4swr71d3rfwlq2hvvmpxvxsa59"; | |||
}); | |||
version = "latest"; | |||
}); | |||
</syntaxHighlight> | </syntaxHighlight> | ||
You will need to update the <code>sha256</code> value for each new Insiders build. The new value will appear in a validation error when you try to build. | |||
== Wayland == | == Wayland == | ||
To use VS Code under Wayland, set the environment variable <code>NIXOS_OZONE_WL=1</code>: | |||
* temporary fix: run via the terminal: | * temporary fix: run via the terminal: | ||
Line 84: | Line 113: | ||
[https://github.com/NixOS/nixpkgs/commit/b2eb5f62a7fd94ab58acafec9f64e54f97c508a6 Source] | [https://github.com/NixOS/nixpkgs/commit/b2eb5f62a7fd94ab58acafec9f64e54f97c508a6 Source] | ||
== Updating extension versions == | |||
Nixpkgs contains a script which will run <code>code --list-extensions</code>, then look for the latest available versions of those extensions, and output a list which you can add to your Nix config in a format similar to the above. To use it, clone the [https://github.com/NixOS/nixpkgs nixpkgs repo from | Nixpkgs contains a script which will run <code>code --list-extensions</code>, then look for the latest available versions of those extensions, and output a list which you can add to your Nix config in a format similar to the above. To use it, clone the [https://github.com/NixOS/nixpkgs nixpkgs repo from GitHub], and run: [https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh nixpkgs/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh] | ||
Example output: | Example output: | ||
Line 139: | Line 135: | ||
sha256 = "01lc9gpqdjy6himn7jsfjrfz8xrk728c20903lxkxy5fliv232gz"; | sha256 = "01lc9gpqdjy6himn7jsfjrfz8xrk728c20903lxkxy5fliv232gz"; | ||
} | } | ||
]; | ]; | ||
} | } | ||
</syntaxHighlight> | </syntaxHighlight> | ||
== Remote SSH == | == Remote SSH == | ||
The remote-ssh extension works by connecting to a remote host and downloading scripts and pre-built binaries to {{ic|$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). | The remote-ssh extension works by connecting to a remote host and downloading scripts and pre-built binaries to {{ic|$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 === | === Any client to NixOS host === | ||
Line 174: | Line 169: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your | 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). | ||
=== Nix-sourced | === 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". | If vscode-remote is installed from nix (vscode-extensions.ms-vscode-remote as above) on the client machine, everything should "just work". | ||
== Using nix-shell == | == Using nix-shell == | ||
Some features of | Some features of VS Code, like the Python package, require linters or other dependencies. The package [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector nix-env-selector] makes this easy and does not require overrides on VS Code itself to add dependencies. | ||
== Troubleshooting == | == Troubleshooting == | ||
Line 219: | Line 188: | ||
</pre> | </pre> | ||
Try to add the following setting in | Try to add the following setting in your system configuration (even if you don't use Gnome as desktop environment): | ||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | {{file|/etc/nixos/configuration.nix|nix|<nowiki> | ||
# needed for store VS Code auth token | |||
services.gnome.gnome-keyring.enable = true; | |||
</nowiki>}} | </nowiki>}} | ||
''Optional | ''Optional'': add <code>gnome.seahorse</code> to <code>environment.systemPackages</code> to install GUI for GNOME Keyring. | ||
Don't forget perform <code>nixos-rebuild switch</code> and reboot system. | Don't forget to perform <code>nixos-rebuild switch</code> and reboot the system. |
Revision as of 13:36, 8 July 2022
Visual Studio Code is a cross-platform text editor developed by Microsoft, built on the Electron framework.
For the free distribution of the VS Code codebase (without MS branding/telemetry) see VSCodium.
Installation
NixOS
environment.systemPackages = with pkgs; [ vscode ];
Extensions can be managed using the 'vscode-with-extensions' package:
environment.systemPackages = with pkgs; [
vscode-with-extensions.override {
vscodeExtensions = with vscode-extensions; [
bbenoist.nix
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";
}
];
}
];
Some examples here: GitHub search for "extensionFromVscodeMarketplace"
It's also possible to install VS Code via Home Manager:
programs.vscode = {
enable = true;
extensions = with pkgs.vscode-extensions; [
dracula-theme.theme-dracula
vscodevim.vim
yzhang.markdown-all-in-one
];
};
- See for more options: Home Manager Manual: Options - programs.vscode
- Search for extensions with configurations: NixOS Search: vscode-extensions
Non-NixOS
$ nix-env -iA nixos.vscode
Use VS Code extensions without additional configuration
With the package vscode-fhs, the editor launches inside a FHS compliant chroot environment using buildFHSUserEnv. 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.
Example usage:
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 extension
programs.vscode.package = pkgs.vscode-fhsWithPackages (ps: with ps; [ rustup zlib ]);
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 thanks to @jnoortheen, which you can add to home.packages
(HM), environment.systemPackages
(NixOS), etc., builds a package with the latest insiders.
(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec {
src = (builtins.fetchTarball {
url = "https://update.code.visualstudio.com/latest/linux-x64/insider";
sha256 = "1dajhfsdr55mfnj12clf5apy1d4swr71d3rfwlq2hvvmpxvxsa59";
});
version = "latest";
});
You will need to update the sha256
value for each new Insiders build. The new value will appear in a validation error when you try to build.
Wayland
To use VS Code under Wayland, set the environment variable NIXOS_OZONE_WL=1
:
- temporary fix: run via the terminal:
$ NIXOS_OZONE_WL=1 code ...
- permanent fix: add to your NixOS configuration:
environment.sessionVariables.NIXOS_OZONE_WL = "1";
Updating extension versions
Nixpkgs contains a script which will run code --list-extensions
, then look for the latest available versions of those extensions, and output a list which you can add to your Nix config in a format similar to the above. To use it, clone the nixpkgs repo from GitHub, and run: nixpkgs/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh
Example output:
❯ ./nixpkgs/pkgs/applications/editors/vscode/extensions/update_installed_exts.sh
... # it does some fetching and then outputs the list...
{ 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 on host machines.
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 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: 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).
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".
Using nix-shell
Some features of VS Code, like the Python package, require linters or other dependencies. The package nix-env-selector makes this easy and does not require overrides on VS Code itself to add dependencies.
Troubleshooting
Error after Sign On
If you get such an error after sign on in application:
Writing login information to the keychain failed with error 'The name org.freedesktop.secret was not provided by any .service files'.
Try to add the following setting in your system configuration (even if you don't use Gnome as desktop environment):
/etc/nixos/configuration.nix
# needed for store VS Code auth token
services.gnome.gnome-keyring.enable = true;
Optional: add gnome.seahorse
to environment.systemPackages
to install GUI for GNOME Keyring.
Don't forget to perform nixos-rebuild switch
and reboot the system.