lol

vscode-utils/vscodeEnv: add vscodeWithConfiguration, vscodeExts2nix and vscodeEnv

move mktplcExtRefToFetchArgs to file in order to be shared with the new derivations(privately)

+150 -12
+8
pkgs/misc/vscode-extensions/mktplcExtRefToFetchArgs.nix
··· 1 + ext: 2 + { 3 + url = "https://${ext.publisher}.gallery.vsassets.io/_apis/public/gallery/publisher/${ext.publisher}/extension/${ext.name}/${ext.version}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"; 4 + sha256 = ext.sha256; 5 + # The `*.vsix` file is in the end a simple zip file. Change the extension 6 + # so that existing `unzip` hooks takes care of the unpacking. 7 + name = "${ext.publisher}-${ext.name}.zip"; 8 + }
+19 -12
pkgs/misc/vscode-extensions/vscode-utils.nix
··· 1 - { stdenv, lib, fetchurl, unzip }: 1 + { stdenv, lib, writeShellScriptBin, fetchurl, vscode, unzip }: 2 2 3 3 let 4 - mktplcExtRefToFetchArgs = ext: { 5 - url = "https://${ext.publisher}.gallery.vsassets.io/_apis/public/gallery/publisher/${ext.publisher}/extension/${ext.name}/${ext.version}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"; 6 - sha256 = ext.sha256; 7 - # The `*.vsix` file is in the end a simple zip file. Change the extension 8 - # so that existing `unzip` hooks takes care of the unpacking. 9 - name = "${ext.publisher}-${ext.name}.zip"; 10 - }; 11 4 12 5 buildVscodeExtension = a@{ 13 6 name, ··· 34 27 buildInputs = [ unzip ] ++ buildInputs; 35 28 36 29 installPhase = '' 30 + 37 31 runHook preInstall 38 32 39 33 mkdir -p "$out/$installPrefix" ··· 44 38 45 39 }); 46 40 47 - 48 41 fetchVsixFromVscodeMarketplace = mktplcExtRef: 49 - fetchurl((mktplcExtRefToFetchArgs mktplcExtRef)); 42 + fetchurl((import ./mktplcExtRefToFetchArgs.nix mktplcExtRef)); 50 43 51 44 buildVscodeMarketplaceExtension = a@{ 52 45 name ? "", ··· 79 72 extensionsFromVscodeMarketplace = mktplcExtRefList: 80 73 builtins.map extensionFromVscodeMarketplace mktplcExtRefList; 81 74 82 - in 75 + vscodeWithConfiguration = (userParams : import ./vscodeWithConfiguration.nix { 76 + inherit lib vscode extensionsFromVscodeMarketplace writeShellScriptBin; 77 + } // userParams); 78 + 79 + 80 + vscodeExts2nix = (userParams : import ./vscodeExts2nix.nix { 81 + inherit lib vscode; 82 + } // userParams); 83 + 84 + vscodeEnv = (userParams : import ./vscodeEnv.nix { 85 + inherit lib writeShellScriptBin extensionsFromVscodeMarketplace vscode; 86 + } // userParams ); 87 + 88 + in 83 89 84 90 { 85 91 inherit fetchVsixFromVscodeMarketplace buildVscodeExtension 86 92 buildVscodeMarketplaceExtension extensionFromVscodeMarketplace 87 - extensionsFromVscodeMarketplace; 93 + extensionsFromVscodeMarketplace 94 + vscodeWithConfiguration vscodeExts2nix vscodeEnv; 88 95 }
+40
pkgs/misc/vscode-extensions/vscodeEnv.nix
··· 1 + #use vscodeWithConfiguration and vscodeExts2nix to create vscode exetuable that when exits(vscode) will update the mutable extension file, which is imported when getting evaluated by nix. 2 + { pkgs ? import <nixpkgs> {} 3 + , lib ? pkgs.lib 4 + , writeShellScriptBin ? pkgs.writeShellScriptBin 5 + , extensionsFromVscodeMarketplace ? pkgs.vscode-utils.extensionsFromVscodeMarketplace 6 + 7 + ##User input 8 + 9 + , nixExtensions ? [] 10 + # if file exists will use it and import the extensions in it into this dervation else will use empty extensions list 11 + # this file will be created/updated by vscodeExts2nix when vscode exists 12 + , mutableExtensionsFile ? ./extensions.nix 13 + , vscodeExtsFolderName ? ".vscode-exts" 14 + , vscode ? pkgs.vscode 15 + }: 16 + let 17 + mutableExtensionsFilePath = builtins.toPath mutableExtensionsFile; 18 + mutableExtensions = if builtins.pathExists mutableExtensionsFile 19 + then import mutableExtensionsFilePath else []; 20 + vscodeWithConfiguration = import ./vscodeWithConfiguration.nix { 21 + inherit lib writeShellScriptBin vscode extensionsFromVscodeMarketplace 22 + nixExtensions mutableExtensions vscodeExtsFolderName; 23 + }; 24 + 25 + vscodeExts2nix = import ./vscodeExts2nix.nix { 26 + inherit lib writeShellScriptBin; 27 + extensionsToIgnore = nixExtensions; 28 + extensions = mutableExtensions; 29 + vscode = vscodeWithConfiguration; 30 + }; 31 + code = writeShellScriptBin "code" '' 32 + ${vscodeWithConfiguration}/bin/code --wait "$@" 33 + echo 'running vscodeExts2nix to update ${mutableExtensionsFilePath}...' 34 + ${vscodeExts2nix}/bin/vscodeExts2nix > ${mutableExtensionsFilePath} 35 + ''; 36 + in 37 + pkgs.buildEnv { 38 + name = "vscodeEnv"; 39 + paths = [ code vscodeExts2nix ]; 40 + }
+44
pkgs/misc/vscode-extensions/vscodeExts2nix.nix
··· 1 + # based on the passed vscode will stdout a nix expression with the installed vscode extensions 2 + { pkgs ? import <nixpkgs>{} 3 + , lib ? pkgs.lib 4 + , vscode ? pkgs.vscode 5 + , writeShellScriptBin ? pkgs.writeShellScriptBin 6 + 7 + ##User input 8 + 9 + , extensionsToIgnore ? [] 10 + # will use those extensions to get sha256 if still exists when executed. 11 + , extensions ? [] 12 + }: 13 + let 14 + mktplcExtRefToFetchArgs = import ./mktplcExtRefToFetchArgs.nix; 15 + in 16 + writeShellScriptBin "vscodeExts2nix" '' 17 + echo '[' 18 + 19 + for line in $(${vscode}/bin/code --list-extensions --show-versions \ 20 + ${lib.optionalString (extensionsToIgnore != []) '' 21 + | grep -v -i '^\(${lib.concatMapStringsSep "\\|" (e : ''${e.publisher}.${e.name}'') extensionsToIgnore}\)' 22 + ''} 23 + ) ; do 24 + [[ $line =~ ([^.]*)\.([^@]*)@(.*) ]] 25 + name=''${BASH_REMATCH[2]} 26 + publisher=''${BASH_REMATCH[1]} 27 + version=''${BASH_REMATCH[3]} 28 + 29 + extensions="${lib.concatMapStringsSep "." (e : ''${e.publisher}${e.name}@${e.sha256}'') extensions}" 30 + reCurrentExt=$publisher$name"@([^.]*)" 31 + if [[ $extensions =~ $reCurrentExt ]]; then 32 + sha256=''${BASH_REMATCH[1]} 33 + else 34 + sha256=$( 35 + nix-prefetch-url "${(mktplcExtRefToFetchArgs {publisher = ''"$publisher"''; name = ''"$name"''; version = ''"$version"'';}).url}" 2> /dev/null 36 + ) 37 + fi 38 + 39 + echo "{ name = \"''${name}\"; publisher = \"''${publisher}\"; version = \"''${version}\"; sha256 = \"''${sha256}\"; }" 40 + done 41 + 42 + 43 + echo ']' 44 + ''
+39
pkgs/misc/vscode-extensions/vscodeWithConfiguration.nix
··· 1 + # wrapper over vscode to control extensions per project (extensions folder will be created in execution path) 2 + { pkgs ? import <nixpkgs> {} 3 + , lib ? pkgs.lib 4 + , writeShellScriptBin ? pkgs.writeShellScriptBin 5 + , extensionsFromVscodeMarketplace ? pkgs.vscode-utils.extensionsFromVscodeMarketplace 6 + 7 + ##User input 8 + 9 + , vscode ? pkgs.vscode 10 + # extensions to be symlinked into the project's extensions folder 11 + , nixExtensions ? [] 12 + # extensions to be copied into the project's extensions folder 13 + , mutableExtensions ? [] 14 + , vscodeExtsFolderName ? ".vscode-exts" 15 + }: 16 + let 17 + nixExtsDrvs = extensionsFromVscodeMarketplace nixExtensions; 18 + mutExtsDrvs = extensionsFromVscodeMarketplace mutableExtensions; 19 + 20 + #removed not defined extensions 21 + rmExtensions = lib.optionalString (nixExtensions++mutableExtensions != []) '' 22 + find ${vscodeExtsFolderName} -mindepth 1 -maxdepth 1 ${lib.concatMapStringsSep " " (e : ''! -iname ${e.publisher}.${e.name}'') (nixExtensions++mutableExtensions)} -exec sudo rm -rf {} \; 23 + ''; 24 + #copy mutable extension out of the nix store 25 + cpExtensions = '' 26 + ${lib.concatMapStringsSep "\n" (e : ''ln -sfn ${e}/share/vscode/extensions/* ${vscodeExtsFolderName}/'') nixExtsDrvs} 27 + ${lib.concatMapStringsSep "\n" (e : '' 28 + cp -a ${e}/share/vscode/extensions/${e.vscodeExtUniqueId} ${vscodeExtsFolderName}/${e.vscodeExtUniqueId}-${(lib.findSingle (ext: ''${ext.publisher}.${ext.name}'' == e.vscodeExtUniqueId) "" "m" mutableExtensions ).version} 29 + '') mutExtsDrvs} 30 + ''; 31 + in 32 + writeShellScriptBin "code" '' 33 + if ! [[ "$@" =~ "--list-extension" ]]; then 34 + mkdir -p ${vscodeExtsFolderName} 35 + ${rmExtensions} 36 + ${cpExtensions} 37 + fi 38 + ${vscode}/bin/code --extensions-dir ${vscodeExtsFolderName} "$@" 39 + ''