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