Visual Studio Code

From NixOS Wiki
Revision as of 08:11, 14 May 2021 by imported>Samuela
Note: Visual Studio Code is unfree, its license prohibits distribution. See the FAQ/unfree page to install unfree software.

For the free distribution of the vscode codebase (without MS branding/telemetry) see VSCodium.

Installing Microsoft's Visual Studio Code

Because it is NixOS, you don't have to be root in order to be able to install stuff. As a normal user, do:

$ nix-env -iA nixos.vscode

And to open or launch the IDE, do:

$ code

Managing extensions

Extensions can be managed using the 'vscode-with-extensions' package:

let
  extensions = (with pkgs.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";
  }];
  vscode-with-extensions = pkgs.vscode-with-extensions.override {
      vscodeExtensions = extensions;
    };
in
  environment.systemPackages = [
    vscode-with-extensions
  ];

We can retrieve an updated set for manually installed / specified packages by cloning the 'nixpkgs' repo from github, and running: 'nixpkgs/pkgs/misc/vscode-extensions/update_installed_exts.sh'

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).

Nix-sourced VSCode 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".

Any client to NixOS host

Use nix-vscode-server on host machines to automate the workaround.

If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VSCode version, then you can install the nodejs-14_x package on the NixOS host and replace the VSCode 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 VSCode upstream requirements (nodejs 14 is needed as of 5/14/2021).

Using nix-shell

Some features of VSCode, like the Python package, require linters or other dependencies. The package nix-env-selector makes this easy and does not require overrides on vscode itself to add dependencies.

Use VSCode extensions without additional configuration

Note: Only available in nixpkgs-unstable or 21.05 and after

In #99968, vscode-fhs and vscodium-fhs packages were added in which the editors launch inside of 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.

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:

$ nix-shell -p vscode-fhs --run code

Home-manager:

  programs.vscode.enable = true;
  programs.vscode.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 ]);