nix-community / nix4vscode

Apache License 2.0
56 stars 5 forks source link
nix vscode

Nix4Vscode

A tool generate nix expression from config.toml. Assuming we have a config.toml file like this:

vscode_version = "1.81.1"

[[extensions]]
publisher_name = "eamodio"
extension_name = "gitlens"

[[extensions]]
publisher_name = "vscodevim"
extension_name = "vim"

We can then run cargo run -- config.toml, and a nix expression will be print to stdout just like this:

{ pkgs, lib }:

let
  vscode-utils = pkgs.vscode-utils;
in
{
  eamodio.gitlens = vscode-utils.extensionFromVscodeMarketplace {
    name = "gitlens";
    publisher = "eamodio";
    version = "2023.9.905";
    sha256 = "1mzyc3sinkg4zmbyh2a85iqdqa7wsnh99hqvk6f8m2jcfhpfrwyb";
  };
  vscodevim.vim = vscode-utils.extensionFromVscodeMarketplace {
    name = "vim";
    publisher = "vscodevim";
    version = "1.26.0";
    sha256 = "0hxb58ygjbqk6qmkp1r421zzib2r1vmz7agbi7bcmjxjpr3grw2w";
  };
}

Let's assume you store these contents in a file named pkgs.nix, you can use it by:

{ pkgs, lib }:
let
  plugins = (import ./vscode_plugins.nix) { pkgs = pkgs; lib = lib; };
in
with pkgs;
{
  enableExtensionUpdateCheck = false;
  enableUpdateCheck = false;
  extensions = with vscode-marketplace;[
    plugins.vscodevim.vim
  ]
  ;
}

Installation

The simplest way to run nix4vscode is inside a devshell. Clone the nix4vscode repository, change into the resulting directory and run:

$ git clone https://github.com/nix-community/nix4vscode.git
$ cd nix4vscode
$ nix develop
$ cargo run -- config.toml

Replace config.toml with the name and path to your VSCode plugin configuration file.

Creating the config.toml file

If you don't already have one, you can create the config.toml file by running the following script:

#!/bin/bash

# Output the VSCode version
echo 'vscode_version = "'$(code --version | head -n1)'"'
echo

# Loop through each installed extension
code --list-extensions | while read extension; do
  publisher_name=$(echo "$extension" | cut -d '.' -f 1)
  extension_name=$(echo "$extension" | cut -d '.' -f 2-)
  echo '[[extensions]]'
  echo 'publisher_name = "'$publisher_name'"'
  echo 'extension_name = "'$extension_name'"'
  echo
done

Just create the script, make it executable and then pipe the output to your config.toml file.

Redirect asset_url

For some extensions (such as codelldb), there is only a downloader in vscode markplace, and the real location of the extension is on github. At this time, asset_url is allowed to be redirected:

vscode_version = "1.81.1"

[[extensions]]
publisher_name = "vadimcn"
extension_name = "vscode-lldb"
asset_url = '''
https://github.com/vadimcn/codelldb/releases/download/v{{ extension.version }}/codelldb-{{ system.arch }}-{{ system.ostype }}.vsix
'''

asset_url is a jinja template string.