vscodium: fix bundle resources being modified on darwin (#373630)
[NixPkgs.git] / pkgs / applications / editors / vscode / extensions / vscode-utils.nix
blob74d674dced92bbe241ae9ed21832ea2bef17b251
2   stdenv,
3   lib,
4   buildEnv,
5   writeShellScriptBin,
6   fetchurl,
7   vscode,
8   unzip,
9   jq,
11 let
12   buildVscodeExtension =
13     a@{
14       pname ? null, # Only optional for backward compatibility.
15       src,
16       # Same as "Unique Identifier" on the extension's web page.
17       # For the moment, only serve as unique extension dir.
18       vscodeExtPublisher,
19       vscodeExtName,
20       vscodeExtUniqueId,
21       configurePhase ? ''
22         runHook preConfigure
23         runHook postConfigure
24       '',
25       buildPhase ? ''
26         runHook preBuild
27         runHook postBuild
28       '',
29       dontPatchELF ? true,
30       dontStrip ? true,
31       nativeBuildInputs ? [ ],
32       passthru ? { },
33       ...
34     }:
35     stdenv.mkDerivation (
36       (removeAttrs a [
37         "vscodeExtUniqueId"
38         "pname"
39       ])
40       // (lib.optionalAttrs (pname != null) {
41         pname = "vscode-extension-${pname}";
42       })
43       // {
45         passthru = passthru // {
46           inherit vscodeExtPublisher vscodeExtName vscodeExtUniqueId;
47         };
49         inherit
50           configurePhase
51           buildPhase
52           dontPatchELF
53           dontStrip
54           ;
56         # Some .vsix files contain other directories (e.g., `package`) that we don't use.
57         # If other directories are present but `sourceRoot` is unset, the unpacker phase fails.
58         sourceRoot = "extension";
60         installPrefix = "share/vscode/extensions/${vscodeExtUniqueId}";
62         nativeBuildInputs = [ unzip ] ++ nativeBuildInputs;
64         installPhase = ''
66           runHook preInstall
68           mkdir -p "$out/$installPrefix"
69           find . -mindepth 1 -maxdepth 1 | xargs -d'\n' mv -t "$out/$installPrefix/"
71           runHook postInstall
72         '';
73       }
74     );
76   fetchVsixFromVscodeMarketplace =
77     mktplcExtRef: fetchurl (import ./mktplcExtRefToFetchArgs.nix mktplcExtRef);
79   buildVscodeMarketplaceExtension =
80     a@{
81       name ? "",
82       src ? null,
83       vsix ? null,
84       mktplcRef,
85       ...
86     }:
87     assert "" == name;
88     assert null == src;
89     buildVscodeExtension (
90       (removeAttrs a [
91         "mktplcRef"
92         "vsix"
93       ])
94       // {
95         pname = "${mktplcRef.publisher}-${mktplcRef.name}";
96         version = mktplcRef.version;
97         src = if (vsix != null) then vsix else fetchVsixFromVscodeMarketplace mktplcRef;
98         vscodeExtPublisher = mktplcRef.publisher;
99         vscodeExtName = mktplcRef.name;
100         vscodeExtUniqueId = "${mktplcRef.publisher}.${mktplcRef.name}";
101       }
102     );
104   mktplcRefAttrList = [
105     "name"
106     "publisher"
107     "version"
108     "sha256"
109     "hash"
110     "arch"
111   ];
113   mktplcExtRefToExtDrv =
114     ext:
115     buildVscodeMarketplaceExtension (
116       removeAttrs ext mktplcRefAttrList
117       // {
118         mktplcRef = builtins.intersectAttrs (lib.genAttrs mktplcRefAttrList (_: null)) ext;
119       }
120     );
122   extensionFromVscodeMarketplace = mktplcExtRefToExtDrv;
123   extensionsFromVscodeMarketplace =
124     mktplcExtRefList: builtins.map extensionFromVscodeMarketplace mktplcExtRefList;
126   vscodeWithConfiguration = import ./vscodeWithConfiguration.nix {
127     inherit lib extensionsFromVscodeMarketplace writeShellScriptBin;
128     vscodeDefault = vscode;
129   };
131   vscodeExts2nix = import ./vscodeExts2nix.nix {
132     inherit lib writeShellScriptBin;
133     vscodeDefault = vscode;
134   };
136   vscodeEnv = import ./vscodeEnv.nix {
137     inherit
138       lib
139       buildEnv
140       writeShellScriptBin
141       extensionsFromVscodeMarketplace
142       jq
143       ;
144     vscodeDefault = vscode;
145   };
147   toExtensionJsonEntry = ext: rec {
148     identifier = {
149       id = ext.vscodeExtUniqueId;
150       uuid = "";
151     };
153     version = ext.version;
155     relativeLocation = ext.vscodeExtUniqueId;
157     location = {
158       "$mid" = 1;
159       fsPath = ext.outPath + "/share/vscode/extensions/${ext.vscodeExtUniqueId}";
160       path = location.fsPath;
161       scheme = "file";
162     };
164     metadata = {
165       id = "";
166       publisherId = "";
167       publisherDisplayName = ext.vscodeExtPublisher;
168       targetPlatform = "undefined";
169       isApplicationScoped = false;
170       updated = false;
171       isPreReleaseVersion = false;
172       installedTimestamp = 0;
173       preRelease = false;
174     };
175   };
177   toExtensionJson = extensions: builtins.toJSON (map toExtensionJsonEntry extensions);
180   inherit
181     fetchVsixFromVscodeMarketplace
182     buildVscodeExtension
183     buildVscodeMarketplaceExtension
184     extensionFromVscodeMarketplace
185     extensionsFromVscodeMarketplace
186     vscodeWithConfiguration
187     vscodeExts2nix
188     vscodeEnv
189     toExtensionJsonEntry
190     toExtensionJson
191     ;