1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 buildGoModule,
6 makeWrapper,
7 cacert,
8 moreutils,
9 jq,
10 git,
11 rsync,
12 pkg-config,
13 yarn,
14 python3,
15 esbuild,
16 nodejs,
17 node-gyp,
18 libsecret,
19 xorg,
20 ripgrep,
21 cctools,
22 xcbuild,
23 quilt,
24 nixosTests,
25}:
26
27let
28 system = stdenv.hostPlatform.system;
29
30 python = python3;
31 yarn' = yarn.override { inherit nodejs; };
32 defaultYarnOpts = [ ];
33
34 esbuild' = esbuild.override {
35 buildGoModule =
36 args:
37 buildGoModule (
38 args
39 // rec {
40 version = "0.16.17";
41 src = fetchFromGitHub {
42 owner = "evanw";
43 repo = "esbuild";
44 rev = "v${version}";
45 hash = "sha256-8L8h0FaexNsb3Mj6/ohA37nYLFogo5wXkAhGztGUUsQ=";
46 };
47 vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ=";
48 }
49 );
50 };
51
52 # replaces esbuild's download script with a binary from nixpkgs
53 patchEsbuild = path: version: ''
54 mkdir -p ${path}/node_modules/esbuild/bin
55 jq "del(.scripts.postinstall)" ${path}/node_modules/esbuild/package.json | sponge ${path}/node_modules/esbuild/package.json
56 sed -i 's/${version}/${esbuild'.version}/g' ${path}/node_modules/esbuild/lib/main.js
57 ln -s -f ${esbuild'}/bin/esbuild ${path}/node_modules/esbuild/bin/esbuild
58 '';
59
60 # Comment from @code-asher, the code-server maintainer
61 # See https://github.com/NixOS/nixpkgs/pull/240001#discussion_r1244303617
62 #
63 # If the commit is missing it will break display languages (Japanese, Spanish,
64 # etc). For some reason VS Code has a hard dependency on the commit being set
65 # for that functionality.
66 # The commit is also used in cache busting. Without the commit you could run
67 # into issues where the browser is loading old versions of assets from the
68 # cache.
69 # Lastly, it can be helpful for the commit to be accurate in bug reports
70 # especially when they are built outside of our CI as sometimes the version
71 # numbers can be unreliable (since they are arbitrarily provided).
72 #
73 # To compute the commit when upgrading this derivation, do:
74 # `$ git rev-parse <git-rev>` where <git-rev> is the git revision of the `src`
75 # Example: `$ git rev-parse v4.16.1`
76 commit = "1962f48b7f71772dc2c060dbaa5a6b4c0792a549";
77in
78stdenv.mkDerivation (finalAttrs: {
79 pname = "code-server";
80 version = "4.91.1";
81
82 src = fetchFromGitHub {
83 owner = "coder";
84 repo = "code-server";
85 rev = "v${finalAttrs.version}";
86 fetchSubmodules = true;
87 hash = "sha256-w0+lg/DcxKLrAz6DQGQ9+yPn42LrQ95Yn16IKNfqPvE=";
88 };
89
90 yarnCache = stdenv.mkDerivation {
91 name = "${finalAttrs.pname}-${finalAttrs.version}-${system}-yarn-cache";
92 inherit (finalAttrs) src;
93
94 nativeBuildInputs = [
95 yarn'
96 git
97 cacert
98 ];
99
100 buildPhase = ''
101 runHook preBuild
102
103 export HOME=$PWD
104 export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
105
106 yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
107
108 yarn config set yarn-offline-mirror $out
109 find "$PWD" -name "yarn.lock" -printf "%h\n" | \
110 xargs -I {} yarn --cwd {} \
111 --frozen-lockfile --ignore-scripts --ignore-platform \
112 --ignore-engines --no-progress --non-interactive
113
114 find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
115 xargs -I {} yarn --cwd {} \
116 --ignore-scripts --ignore-engines
117
118 runHook postBuild
119 '';
120
121 outputHashMode = "recursive";
122 outputHashAlgo = "sha256";
123 outputHash = "sha256-LCmygPid6VJqR1PCOMk/Hc6bo4nwsLwYr7O1p3FQVvQ=";
124 };
125
126 nativeBuildInputs = [
127 nodejs
128 yarn'
129 python
130 pkg-config
131 makeWrapper
132 git
133 rsync
134 jq
135 moreutils
136 quilt
137 ];
138
139 buildInputs =
140 [
141 xorg.libX11
142 xorg.libxkbfile
143 ]
144 ++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
145 libsecret
146 ]
147 ++ lib.optionals stdenv.hostPlatform.isDarwin [
148 cctools
149 xcbuild
150 ];
151
152 patches = [
153 # Remove all git calls from the VS Code build script except `git rev-parse
154 # HEAD` which is replaced in postPatch with the commit.
155 ./build-vscode-nogit.patch
156 ];
157
158 postPatch = ''
159 export HOME=$PWD
160
161 patchShebangs ./ci
162
163 # inject git commit
164 substituteInPlace ./ci/build/build-vscode.sh \
165 --replace-fail '$(git rev-parse HEAD)' "${commit}"
166 substituteInPlace ./ci/build/build-release.sh \
167 --replace-fail '$(git rev-parse HEAD)' "${commit}"
168 '';
169
170 configurePhase = ''
171 runHook preConfigure
172
173 # run yarn offline by default
174 echo '--install.offline true' >> .yarnrc
175
176 # set default yarn opts
177 ${lib.concatMapStrings (option: ''
178 yarn --offline config set ${option}
179 '') defaultYarnOpts}
180
181 # set offline mirror to yarn cache we created in previous steps
182 yarn --offline config set yarn-offline-mirror "${finalAttrs.yarnCache}"
183
184 # skip unnecessary electron download
185 export ELECTRON_SKIP_BINARY_DOWNLOAD=1
186
187 # set nodedir to prevent node-gyp from downloading headers
188 # taken from https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific
189 mkdir -p $HOME/.node-gyp/${nodejs.version}
190 echo 9 > $HOME/.node-gyp/${nodejs.version}/installVersion
191 ln -sfv ${nodejs}/include $HOME/.node-gyp/${nodejs.version}
192 export npm_config_nodedir=${nodejs}
193
194 # use updated node-gyp. fixes the following error on Darwin:
195 # PermissionError: [Errno 1] Operation not permitted: '/usr/sbin/pkgutil'
196 export npm_config_node_gyp=${node-gyp}/lib/node_modules/node-gyp/bin/node-gyp.js
197
198 runHook postConfigure
199 '';
200
201 buildPhase =
202 ''
203 runHook preBuild
204
205 # Apply patches.
206 quilt push -a
207
208 export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
209 export SKIP_SUBMODULE_DEPS=1
210 export NODE_OPTIONS="--openssl-legacy-provider --max-old-space-size=4096"
211
212 # Remove all built-in extensions, as these are 3rd party extensions that
213 # get downloaded from the VS Code marketplace.
214 jq --slurp '.[0] * .[1]' "./lib/vscode/product.json" <(
215 cat << EOF
216 {
217 "builtInExtensions": []
218 }
219 EOF
220 ) | sponge ./lib/vscode/product.json
221
222 # Disable automatic updates.
223 sed -i '/update.mode/,/\}/{s/default:.*/default: "none",/g}' \
224 lib/vscode/src/vs/platform/update/common/update.config.contribution.ts
225
226 # Patch out remote download of nodejs from build script.
227 patch -p1 -i ${./remove-node-download.patch}
228
229 # Install dependencies.
230 patchShebangs .
231 find . -name "yarn.lock" -printf "%h\n" | \
232 xargs -I {} yarn --cwd {} \
233 --offline --frozen-lockfile --ignore-scripts --ignore-engines
234 patchShebangs .
235
236 # Use esbuild from nixpkgs.
237 ${patchEsbuild "./lib/vscode/build" "0.12.6"}
238 ${patchEsbuild "./lib/vscode/extensions" "0.11.23"}
239
240 # Kerberos errors while building, so remove it for now as it is not
241 # required.
242 yarn remove kerberos --cwd lib/vscode/remote --offline --frozen-lockfile --ignore-scripts --ignore-engines
243
244 # Put ripgrep binary into bin, so post-install does not try to download it.
245 find -name ripgrep -type d \
246 -execdir mkdir -p {}/bin \; \
247 -execdir ln -s ${ripgrep}/bin/rg {}/bin/rg \;
248
249 # Run post-install scripts after patching.
250 find ./lib/vscode \( -path "*/node_modules/*" -or -path "*/extensions/*" \) \
251 -and -type f -name "yarn.lock" -printf "%h\n" | \
252 xargs -I {} sh -c 'jq -e ".scripts.postinstall" {}/package.json >/dev/null && yarn --cwd {} postinstall --frozen-lockfile --offline || true'
253 patchShebangs .
254
255 ''
256 + lib.optionalString stdenv.hostPlatform.isDarwin ''
257 # Use prebuilt binary for @parcel/watcher, which requires macOS SDK 10.13+
258 # (see issue #101229).
259 pushd ./lib/vscode/remote/node_modules/@parcel/watcher
260 mkdir -p ./build/Release
261 mv ./prebuilds/darwin-x64/node.napi.glibc.node ./build/Release/watcher.node
262 jq "del(.scripts) | .gypfile = false" ./package.json | sponge ./package.json
263 popd
264 ''
265 + ''
266
267 # Build binary packages (argon2, node-pty, etc).
268 npm rebuild --offline
269 npm rebuild --offline --prefix lib/vscode/remote
270
271 # Build code-server and VS Code.
272 yarn build
273 VERSION=${finalAttrs.version} yarn build:vscode
274
275 # Inject version into package.json.
276 jq --slurp '.[0] * .[1]' ./package.json <(
277 cat << EOF
278 {
279 "version": "${finalAttrs.version}"
280 }
281 EOF
282 ) | sponge ./package.json
283
284 # Create release, keeping all dependencies.
285 KEEP_MODULES=1 yarn release
286
287 # Prune development dependencies. We only need to do this for the root as
288 # the VS Code build process already does this for VS Code.
289 npm prune --omit=dev --prefix release
290
291 runHook postBuild
292 '';
293
294 installPhase = ''
295 runHook preInstall
296
297 mkdir -p $out/libexec/code-server $out/bin
298
299 # copy release to libexec path
300 cp -R -T release "$out/libexec/code-server"
301
302 # create wrapper
303 makeWrapper "${nodejs}/bin/node" "$out/bin/code-server" \
304 --add-flags "$out/libexec/code-server/out/node/entry.js"
305
306 runHook postInstall
307 '';
308
309 passthru = {
310 prefetchYarnCache = lib.overrideDerivation finalAttrs.yarnCache (d: {
311 outputHash = lib.fakeSha256;
312 });
313 tests = {
314 inherit (nixosTests) code-server;
315 };
316 # vscode-with-extensions compatibility
317 executableName = "code-server";
318 longName = "Visual Studio Code Server";
319 };
320
321 meta = {
322 changelog = "https://github.com/coder/code-server/blob/${finalAttrs.src.rev}/CHANGELOG.md";
323 description = "Run VS Code on a remote server";
324 longDescription = ''
325 code-server is VS Code running on a remote server, accessible through the
326 browser.
327 '';
328 homepage = "https://github.com/coder/code-server";
329 license = lib.licenses.mit;
330 maintainers = with lib.maintainers; [
331 offline
332 henkery
333 code-asher
334 ];
335 platforms = [
336 "x86_64-linux"
337 "aarch64-linux"
338 "x86_64-darwin"
339 ];
340 mainProgram = "code-server";
341 };
342})