31 system = stdenv.hostPlatform.system;
34 yarn' = yarn.override { inherit nodejs; };
35 defaultYarnOpts = [ ];
37 esbuild' = esbuild.override {
38 buildGoModule = args: buildGoModule (args // rec {
40 src = fetchFromGitHub {
44 hash = "sha256-8L8h0FaexNsb3Mj6/ohA37nYLFogo5wXkAhGztGUUsQ=";
46 vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ=";
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
58 # Comment from @code-asher, the code-server maintainer
59 # See https://github.com/NixOS/nixpkgs/pull/240001#discussion_r1244303617
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
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).
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 = "1962f48b7f71772dc2c060dbaa5a6b4c0792a549";
76 stdenv.mkDerivation (finalAttrs: {
77 pname = "code-server";
80 src = fetchFromGitHub {
83 rev = "v${finalAttrs.version}";
84 fetchSubmodules = true;
85 hash = "sha256-w0+lg/DcxKLrAz6DQGQ9+yPn42LrQ95Yn16IKNfqPvE=";
88 yarnCache = stdenv.mkDerivation {
89 name = "${finalAttrs.pname}-${finalAttrs.version}-${system}-yarn-cache";
90 inherit (finalAttrs) src;
92 nativeBuildInputs = [ yarn' git cacert ];
98 export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
100 yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
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
108 find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
109 xargs -I {} yarn --cwd {} \
110 --ignore-scripts --ignore-engines
115 outputHashMode = "recursive";
116 outputHashAlgo = "sha256";
117 outputHash = "sha256-LCmygPid6VJqR1PCOMk/Hc6bo4nwsLwYr7O1p3FQVvQ=";
120 nativeBuildInputs = [
136 ] ++ lib.optionals (!stdenv.isDarwin) [
138 ] ++ lib.optionals stdenv.isDarwin [
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
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}"
168 # run yarn offline by default
169 echo '--install.offline true' >> .yarnrc
171 # set default yarn opts
172 ${lib.concatMapStrings (option: ''
173 yarn --offline config set ${option}
176 # set offline mirror to yarn cache we created in previous steps
177 yarn --offline config set yarn-offline-mirror "${finalAttrs.yarnCache}"
179 # skip unnecessary electron download
180 export ELECTRON_SKIP_BINARY_DOWNLOAD=1
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}
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
193 runHook postConfigure
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"
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" <(
211 "builtInExtensions": []
214 ) | sponge ./lib/vscode/product.json
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
220 # Patch out remote download of nodejs from build script.
221 patch -p1 -i ${./remove-node-download.patch}
223 # Install dependencies.
225 find . -name "yarn.lock" -printf "%h\n" | \
226 xargs -I {} yarn --cwd {} \
227 --offline --frozen-lockfile --ignore-scripts --ignore-engines
230 # Use esbuild from nixpkgs.
231 ${patchEsbuild "./lib/vscode/build" "0.12.6"}
232 ${patchEsbuild "./lib/vscode/extensions" "0.11.23"}
234 # Kerberos errors while building, so remove it for now as it is not
236 yarn remove kerberos --cwd lib/vscode/remote --offline --frozen-lockfile --ignore-scripts --ignore-engines
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 \;
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'
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
259 # Build binary packages (argon2, node-pty, etc).
260 npm rebuild --offline
261 npm rebuild --offline --prefix lib/vscode/remote
263 # Build code-server and VS Code.
265 VERSION=${finalAttrs.version} yarn build:vscode
267 # Inject version into package.json.
268 jq --slurp '.[0] * .[1]' ./package.json <(
271 "version": "${finalAttrs.version}"
274 ) | sponge ./package.json
276 # Create release, keeping all dependencies.
277 KEEP_MODULES=1 yarn release
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
289 mkdir -p $out/libexec/code-server $out/bin
291 # copy release to libexec path
292 cp -R -T release "$out/libexec/code-server"
295 makeWrapper "${nodejs}/bin/node" "$out/bin/code-server" \
296 --add-flags "$out/libexec/code-server/out/node/entry.js"
302 prefetchYarnCache = lib.overrideDerivation finalAttrs.yarnCache (d: {
303 outputHash = lib.fakeSha256;
306 inherit (nixosTests) code-server;
308 # vscode-with-extensions compatibility
309 executableName = "code-server";
310 longName = "Visual Studio Code Server";
314 changelog = "https://github.com/coder/code-server/blob/${finalAttrs.src.rev}/CHANGELOG.md";
315 description = "Run VS Code on a remote server";
317 code-server is VS Code running on a remote server, accessible through the
320 homepage = "https://github.com/coder/code-server";
321 license = lib.licenses.mit;
322 maintainers = with lib.maintainers; [ offline henkery code-asher ];
323 platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ];
324 mainProgram = "code-server";