1 #! /usr/bin/env nix-shell
2 #! nix-shell -i bash -p cacert curl jq unzip
3 # shellcheck shell=bash
6 # can be added to your configuration with the following command and snippet:
7 # $ ./pkgs/applications/editors/vscode/extensions/update_installed_exts.sh > extensions.nix
9 # packages = with pkgs;
10 # (vscode-with-extensions.override {
11 # vscodeExtensions = map
12 # (extension: vscode-utils.buildVscodeMarketplaceExtension {
14 # inherit (extension) name publisher version sha256;
17 # (import ./extensions.nix).extensions;
21 # Helper to just fail with a message and non-zero exit code.
27 # Helper to clean up after ourselves if we're killed by SIGINT.
29 TDIR
="${TMPDIR:-/tmp}"
30 echo "Script killed, cleaning up tmpdirs: $TDIR/vscode_exts_*" >&2
31 rm -Rf "$TDIR/vscode_exts_*"
34 function get_vsixpkg
() {
37 # Create a tempdir for the extension download.
38 EXTTMP
=$
(mktemp
-d -t vscode_exts_XXXXXXXX
)
40 URL
="https://$1.gallery.vsassets.io/_apis/public/gallery/publisher/$1/extension/$2/latest/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
42 # Quietly but delicately curl down the file, blowing up at the first sign of trouble.
43 curl
--silent --show-error --retry 3 --fail -X GET
-o "$EXTTMP/$N.zip" "$URL"
44 # Unpack the file we need to stdout then pull out the version
45 VER
=$
(jq
-r '.version' <(unzip -qc "$EXTTMP/$N.zip" "extension/package.json"))
47 SHA
=$
(nix-hash
--flat --base32 --type sha256
"$EXTTMP/$N.zip")
51 # I don't like 'rm -Rf' lurking in my scripts but this seems appropriate.
63 # See if we can find our `code` binary somewhere.
67 CODE
=$
(command -v code ||
command -v codium
)
70 if [ -z "$CODE" ]; then
71 # Not much point continuing.
72 fail
"VSCode executable not found"
75 # Try to be a good citizen and clean up after ourselves if we're killed.
78 # Begin the printing of the nix expression that will house the list of extensions.
79 printf '{ extensions = [\n'
81 # Note that we are only looking to update extensions that are already installed.
82 for i
in $
($CODE --list-extensions)
84 OWNER
=$
(echo "$i" | cut
-d.
-f1)
85 EXT
=$
(echo "$i" | cut
-d.
-f2)
87 get_vsixpkg
"$OWNER" "$EXT"
89 # Close off the nix expression.