32 system = stdenv.hostPlatform.system;
35 yarn' = yarn.override { inherit nodejs; };
36 defaultYarnOpts = [ ];
38 esbuild' = esbuild.override {
45 src = fetchFromGitHub {
49 hash = "sha256-8L8h0FaexNsb3Mj6/ohA37nYLFogo5wXkAhGztGUUsQ=";
51 vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ=";
56 # replaces esbuild's download script with a binary from nixpkgs
57 patchEsbuild = path: version: ''
58 mkdir -p ${path}/node_modules/esbuild/bin
59 jq "del(.scripts.postinstall)" ${path}/node_modules/esbuild/package.json | sponge ${path}/node_modules/esbuild/package.json
60 sed -i 's/${version}/${esbuild'.version}/g' ${path}/node_modules/esbuild/lib/main.js
61 ln -s -f ${esbuild'}/bin/esbuild ${path}/node_modules/esbuild/bin/esbuild
64 # Comment from @code-asher, the code-server maintainer
65 # See https://github.com/NixOS/nixpkgs/pull/240001#discussion_r1244303617
67 # If the commit is missing it will break display languages (Japanese, Spanish,
68 # etc). For some reason VS Code has a hard dependency on the commit being set
69 # for that functionality.
70 # The commit is also used in cache busting. Without the commit you could run
71 # into issues where the browser is loading old versions of assets from the
73 # Lastly, it can be helpful for the commit to be accurate in bug reports
74 # especially when they are built outside of our CI as sometimes the version
75 # numbers can be unreliable (since they are arbitrarily provided).
77 # To compute the commit when upgrading this derivation, do:
78 # `$ git rev-parse <git-rev>` where <git-rev> is the git revision of the `src`
79 # Example: `$ git rev-parse v4.16.1`
80 commit = "1962f48b7f71772dc2c060dbaa5a6b4c0792a549";
82 stdenv.mkDerivation (finalAttrs: {
83 pname = "code-server";
86 src = fetchFromGitHub {
89 rev = "v${finalAttrs.version}";
90 fetchSubmodules = true;
91 hash = "sha256-w0+lg/DcxKLrAz6DQGQ9+yPn42LrQ95Yn16IKNfqPvE=";
94 yarnCache = stdenv.mkDerivation {
95 name = "${finalAttrs.pname}-${finalAttrs.version}-${system}-yarn-cache";
96 inherit (finalAttrs) src;
108 export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
110 yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
112 yarn config set yarn-offline-mirror $out
113 find "$PWD" -name "yarn.lock" -printf "%h\n" | \
114 xargs -I {} yarn --cwd {} \
115 --frozen-lockfile --ignore-scripts --ignore-platform \
116 --ignore-engines --no-progress --non-interactive
118 find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
119 xargs -I {} yarn --cwd {} \
120 --ignore-scripts --ignore-engines
125 outputHashMode = "recursive";
126 outputHashAlgo = "sha256";
127 outputHash = "sha256-LCmygPid6VJqR1PCOMk/Hc6bo4nwsLwYr7O1p3FQVvQ=";
130 nativeBuildInputs = [
148 ++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
151 ++ lib.optionals stdenv.hostPlatform.isDarwin [
161 # Remove all git calls from the VS Code build script except `git rev-parse
162 # HEAD` which is replaced in postPatch with the commit.
163 ./build-vscode-nogit.patch
172 substituteInPlace ./ci/build/build-vscode.sh \
173 --replace-fail '$(git rev-parse HEAD)' "${commit}"
174 substituteInPlace ./ci/build/build-release.sh \
175 --replace-fail '$(git rev-parse HEAD)' "${commit}"
181 # run yarn offline by default
182 echo '--install.offline true' >> .yarnrc
184 # set default yarn opts
185 ${lib.concatMapStrings (option: ''
186 yarn --offline config set ${option}
189 # set offline mirror to yarn cache we created in previous steps
190 yarn --offline config set yarn-offline-mirror "${finalAttrs.yarnCache}"
192 # skip unnecessary electron download
193 export ELECTRON_SKIP_BINARY_DOWNLOAD=1
195 # set nodedir to prevent node-gyp from downloading headers
196 # taken from https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific
197 mkdir -p $HOME/.node-gyp/${nodejs.version}
198 echo 9 > $HOME/.node-gyp/${nodejs.version}/installVersion
199 ln -sfv ${nodejs}/include $HOME/.node-gyp/${nodejs.version}
200 export npm_config_nodedir=${nodejs}
202 # use updated node-gyp. fixes the following error on Darwin:
203 # PermissionError: [Errno 1] Operation not permitted: '/usr/sbin/pkgutil'
204 export npm_config_node_gyp=${node-gyp}/lib/node_modules/node-gyp/bin/node-gyp.js
206 runHook postConfigure
216 export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
217 export SKIP_SUBMODULE_DEPS=1
218 export NODE_OPTIONS="--openssl-legacy-provider --max-old-space-size=4096"
220 # Remove all built-in extensions, as these are 3rd party extensions that
221 # get downloaded from the VS Code marketplace.
222 jq --slurp '.[0] * .[1]' "./lib/vscode/product.json" <(
225 "builtInExtensions": []
228 ) | sponge ./lib/vscode/product.json
230 # Disable automatic updates.
231 sed -i '/update.mode/,/\}/{s/default:.*/default: "none",/g}' \
232 lib/vscode/src/vs/platform/update/common/update.config.contribution.ts
234 # Patch out remote download of nodejs from build script.
235 patch -p1 -i ${./remove-node-download.patch}
237 # Install dependencies.
239 find . -name "yarn.lock" -printf "%h\n" | \
240 xargs -I {} yarn --cwd {} \
241 --offline --frozen-lockfile --ignore-scripts --ignore-engines
244 # Use esbuild from nixpkgs.
245 ${patchEsbuild "./lib/vscode/build" "0.12.6"}
246 ${patchEsbuild "./lib/vscode/extensions" "0.11.23"}
248 # Kerberos errors while building, so remove it for now as it is not
250 yarn remove kerberos --cwd lib/vscode/remote --offline --frozen-lockfile --ignore-scripts --ignore-engines
252 # Put ripgrep binary into bin, so post-install does not try to download it.
253 find -name ripgrep -type d \
254 -execdir mkdir -p {}/bin \; \
255 -execdir ln -s ${ripgrep}/bin/rg {}/bin/rg \;
257 # Run post-install scripts after patching.
258 find ./lib/vscode \( -path "*/node_modules/*" -or -path "*/extensions/*" \) \
259 -and -type f -name "yarn.lock" -printf "%h\n" | \
260 xargs -I {} sh -c 'jq -e ".scripts.postinstall" {}/package.json >/dev/null && yarn --cwd {} postinstall --frozen-lockfile --offline || true'
264 + lib.optionalString stdenv.hostPlatform.isDarwin ''
265 # Use prebuilt binary for @parcel/watcher, which requires macOS SDK 10.13+
266 # (see issue #101229).
267 pushd ./lib/vscode/remote/node_modules/@parcel/watcher
268 mkdir -p ./build/Release
269 mv ./prebuilds/darwin-x64/node.napi.glibc.node ./build/Release/watcher.node
270 jq "del(.scripts) | .gypfile = false" ./package.json | sponge ./package.json
275 # Build binary packages (argon2, node-pty, etc).
276 npm rebuild --offline
277 npm rebuild --offline --prefix lib/vscode/remote
279 # Build code-server and VS Code.
281 VERSION=${finalAttrs.version} yarn build:vscode
283 # Inject version into package.json.
284 jq --slurp '.[0] * .[1]' ./package.json <(
287 "version": "${finalAttrs.version}"
290 ) | sponge ./package.json
292 # Create release, keeping all dependencies.
293 KEEP_MODULES=1 yarn release
295 # Prune development dependencies. We only need to do this for the root as
296 # the VS Code build process already does this for VS Code.
297 npm prune --omit=dev --prefix release
305 mkdir -p $out/libexec/code-server $out/bin
307 # copy release to libexec path
308 cp -R -T release "$out/libexec/code-server"
311 makeWrapper "${nodejs}/bin/node" "$out/bin/code-server" \
312 --add-flags "$out/libexec/code-server/out/node/entry.js"
318 prefetchYarnCache = lib.overrideDerivation finalAttrs.yarnCache (d: {
319 outputHash = lib.fakeSha256;
322 inherit (nixosTests) code-server;
324 # vscode-with-extensions compatibility
325 executableName = "code-server";
326 longName = "Visual Studio Code Server";
330 changelog = "https://github.com/coder/code-server/blob/${finalAttrs.src.rev}/CHANGELOG.md";
331 description = "Run VS Code on a remote server";
333 code-server is VS Code running on a remote server, accessible through the
336 homepage = "https://github.com/coder/code-server";
337 license = lib.licenses.mit;
338 maintainers = with lib.maintainers; [
348 mainProgram = "code-server";