Visual Studio Code

From NixOS Wiki
Revision as of 21:11, 19 April 2022 by Jessestricker (talk | contribs) (Improve formatting in Wayland section)
Jump to: navigation, search
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

With Home Manager

If you are using Home Manager, you will want to modify your home.nix (or a file inherited by it). Example:

Breeze-text-x-plain.png
~/home.nix
{ config, pkgs, ... }:
{
  programs.home-manager.enable = true;
  ...    # More of your home-manager config

  programs = {
    ...    # Start of your programs config

    vscode = {
      enable = true;
      package = pkgs.vscodium;    # You can skip this if you want to use the unfree version
      extensions = with pkgs.vscode-extensions; [
        # Some example extensions...
        dracula-theme.theme-dracula
        vscodevim.vim
        yzhang.markdown-all-in-one
      ];
    }

    ...    # Rest of your programs config
  }

  ...    # Rest of your home-manager config
}


With nix-env

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

Wayland

If you are running Wayland, you may see a blank screen when starting vscode.

To fix this, 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";

Source

Managing extensions

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

{ pkgs, ... }:

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 {
  config = {
    environment.systemPackages = [
      vscode-with-extensions
    ];
  };
}

Some useful examples here: GitHub search for "extensionFromVscodeMarketplace"

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";
  }
... # the output for the rest of your extensions
];
}%

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 VSCode version, then you can install the nodejs-16_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 16 required from 4/2022).

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

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 ]);


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 you system configuration (even if you don't use Gnome as desktop environment):

Breeze-text-x-plain.png
/etc/nixos/configuration.nix
  # needed for store VSCode auth token 
  services.gnome.gnome-keyring.enable = true;


Don't forget perform nixos-rebuild switch and reboot system.