Getting VS Code with Extensions Installed (Unstable Repo)

Some extensions may or may not work right on NixOS unless they are done via the NixOS way. The following is kind of a hack to keep the main NixOS running regular channel, but Visual Studio Code from the unstable branch:

Create a vscode.nix in your /etc/nixos/ folder with the following:

{ config, pkgs, lib, ... }: {
    options = {
        vscode.extensions = lib.mkOption { default = []; };
        vscode.user = lib.mkOption { };     # <- Must be supplied
        vscode.homeDir = lib.mkOption { };  # <- Must be supplied
        nixpkgs.latestPackages = lib.mkOption { default = []; };
    };

    config = {
        ###
        # DIRTY HACK
        # This will fetch latest packages on each rebuild, whatever channel you are at
        nixpkgs.overlays = [
            (self: super:
                let latestPkgs = import (fetchTarball https://github.com/nixos/nixpkgs-channels/archive/nixpkgs-unstable.tar.gz) {
                        config.allowUnfree = true;
                    };
                in lib.genAttrs config.nixpkgs.latestPackages (pkg: latestPkgs."${pkg}")
      
	    )
        ];
        # END DIRTY HACK
        ###

        environment.systemPackages = [ pkgs.vscode ];

        system.activationScripts.fix-vscode-extensions = {
            text = ''
                EXT_DIR=${config.vscode.homeDir}/.vscode/extensions
                mkdir -p $EXT_DIR
                chown ${config.vscode.user}:users $EXT_DIR
                for x in ${lib.concatMapStringsSep " " toString config.vscode.extensions}; do
                    ln -sf $x/share/vscode/extensions/* $EXT_DIR/
                done
                chown -R ${config.vscode.user}:users $EXT_DIR
            '';
            deps = [];
        };
    };
}

In your imports area of the main /etc/nixos/configuration.nix, add ./vscode.nix.

Add the following to your configuration.nix file (changing <username> to your username), this will install your extensions in the local vscode extensions folder (also note that I have the Nix extension and wakatime extensions installing using built-in extensions from Nix, remove if you do not want them):

  vscode.user = "<username>";
  vscode.homeDir = "/home/<username>"; 
  vscode.extensions = with pkgs.vscode-extensions; [
    bbenoist.Nix
    WakaTime.vscode-wakatime
  ]; 

In a lot of cases, NixOS may give you problems with some lesser known extensions (will get undefined variable errors). A couple extensions have their own .nix files that help to get more problematic extensions installed like Wakatime or the cpptools extension. You can check here to see a list of ones you may want to specify as I did in the above example.

To have other extensions that are not already being installed from my previous example, we need to give it four pieces of information for each extension. The best way to do this is to install your extensions the normal way in vscode and then execute this command from their github. This will give us a list of the extensions and the related information it needs in the format it needs. So now we can adapt the previous code block by giving it more information, in this case we added some eslint and an elixir language server extensions to illustrate adding new extensions:

  vscode.user = "<username>";
  vscode.homeDir = "/home/<username>"; 
  vscode.extensions = with pkgs.vscode-extensions; [
    bbenoist.Nix
    WakaTime.vscode-wakatime
  ] ++ pkgs.vscode-utils.extensionsFromVscodeMarketplace [
  {
    name = "vscode-eslint";
    publisher = "dbaeumer";
    version = "2.1.14";
    sha256 = "113w2iis4zi4z3sqc3vd2apyrh52hbh2gvmxjr5yvjpmrsksclbd";
  }
  {
    name = "elixir-ls";
    publisher = "JakeBecker";
    version = "0.6.2";
    sha256 = "1vmfvjh916cqjva874a751zyjx77zyj0x8ia9hw3f09mvbvdbzqh";
  }
  {
    name = "vs-code-prettier-eslint";
    publisher = "rvest";
    version = "0.4.1";
    sha256 = "0inqkn574zjzg52qcnmpfhsbzbi6vxnr2lrqqff9mh5vvvqsm6v0";
  }
  ]; 

To get updated versions and sha256 keys, you will need to run the same script from github as before. Replace the changed/update extensions. As you would with doing any other changes to the configuration.nix file, you will need to run the nixos-rebuild command to have to install vscode and the extensions you defined.

Useful References

NixOS Discoure - VSCode extensions setup post

NixOS Wiki - Vscode