1{ lib, stdenv, fetchFromGitHub, buildGoModule, makeWrapper, runCommand
2, moreutils, jq, git, cacert, zip, rsync, pkg-config, yarn, python3
3, esbuild, nodejs-14_x, libsecret, xorg, ripgrep
4, AppKit, Cocoa, Security, cctools }:
5
6let
7 system = stdenv.hostPlatform.system;
8
9 nodejs = nodejs-14_x;
10 python = python3;
11 yarn' = yarn.override { inherit nodejs; };
12 defaultYarnOpts = [ "frozen-lockfile" "non-interactive" "no-progress"];
13
14in stdenv.mkDerivation rec {
15 pname = "code-server";
16 version = "3.12.0";
17 commit = "798dc0baf284416dbbf951e4ef596beeab6cb6c4";
18
19 src = fetchFromGitHub {
20 owner = "cdr";
21 repo = "code-server";
22 rev = "v${version}";
23 sha256 = "17v3sz0wjrmikmzyh9xswr4kf1vcj9njlibqb4wwj0pq0d72wdvl";
24 };
25
26 cloudAgent = buildGoModule rec {
27 pname = "cloud-agent";
28 version = "0.2.3";
29
30 src = fetchFromGitHub {
31 owner = "cdr";
32 repo = "cloud-agent";
33 rev = "v${version}";
34 sha256 = "14i1qq273f0yn5v52ryiqwj7izkd1yd212di4gh4bqypmmzhw3jj";
35 };
36
37 vendorSha256 = "0k9v10wkzx53r5syf6bmm81gr4s5dalyaa07y9zvx6vv5r2h0661";
38
39 postPatch = ''
40 # the cloud-agent release tag has an empty version string, so add it back in
41 substituteInPlace internal/version/version.go \
42 --replace 'var Version string' 'var Version string = "v${version}"'
43 '';
44 };
45
46 yarnCache = stdenv.mkDerivation {
47 name = "${pname}-${version}-${system}-yarn-cache";
48 inherit src;
49 nativeBuildInputs = [ yarn' git cacert ];
50 buildPhase = ''
51 export HOME=$PWD
52 export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
53
54 yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
55
56 yarn config set yarn-offline-mirror $out
57 find "$PWD" -name "yarn.lock" -printf "%h\n" | \
58 xargs -I {} yarn --cwd {} \
59 --frozen-lockfile --ignore-scripts --ignore-platform \
60 --ignore-engines --no-progress --non-interactive
61 '';
62 outputHashMode = "recursive";
63 outputHashAlgo = "sha256";
64
65 # to get hash values use nix-build -A code-server.prefetchYarnCache
66 outputHash = {
67 x86_64-linux = "1clfdl9hy5j2dj6jj6a9vgq0wzllfj0h2hbb73959k3w85y4ad2w";
68 aarch64-linux = "1clfdl9hy5j2dj6jj6a9vgq0wzllfj0h2hbb73959k3w85y4ad2w";
69 x86_64-darwin = "1clfdl9hy5j2dj6jj6a9vgq0wzllfj0h2hbb73959k3w85y4ad2w";
70 }.${system} or (throw "Unsupported system ${system}");
71 };
72
73 # Extract the Node.js source code which is used to compile packages with
74 # native bindings
75 nodeSources = runCommand "node-sources" {} ''
76 tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
77 mv node-* $out
78 '';
79
80 nativeBuildInputs = [
81 nodejs yarn' python pkg-config zip makeWrapper git rsync jq moreutils
82 ];
83 buildInputs = lib.optionals (!stdenv.isDarwin) [ libsecret ]
84 ++ (with xorg; [ libX11 libxkbfile ])
85 ++ lib.optionals stdenv.isDarwin [
86 AppKit Cocoa Security cctools
87 ];
88
89 patches = [
90 # remove download of coder-cloud agent
91 ./remove-cloud-agent-download.patch
92 ];
93
94 postPatch = ''
95 export HOME=$PWD
96
97 patchShebangs ./ci
98
99 # inject git commit
100 substituteInPlace ci/build/build-release.sh \
101 --replace '$(git rev-parse HEAD)' "$commit"
102 '';
103
104 configurePhase = ''
105 # run yarn offline by default
106 echo '--install.offline true' >> .yarnrc
107
108 # set default yarn opts
109 ${lib.concatMapStrings (option: ''
110 yarn --offline config set ${option}
111 '') defaultYarnOpts}
112
113 # set offline mirror to yarn cache we created in previous steps
114 yarn --offline config set yarn-offline-mirror "${yarnCache}"
115
116 # link coder-cloud agent from nix store
117 mkdir -p lib
118 ln -s "${cloudAgent}/bin/cloud-agent" ./lib/coder-cloud-agent
119
120 # skip unnecessary electron download
121 export ELECTRON_SKIP_BINARY_DOWNLOAD=1
122 '' + lib.optionalString stdenv.isLinux ''
123 # set nodedir, so we can build binaries later
124 npm config set nodedir "${nodeSources}"
125 '';
126
127 buildPhase = ''
128 # install code-server dependencies
129 yarn --offline --ignore-scripts
130
131 # patch shebangs of everything to allow binary packages to build
132 patchShebangs .
133
134 # Skip shellcheck download
135 jq "del(.scripts.preinstall)" node_modules/shellcheck/package.json | sponge node_modules/shellcheck/package.json
136
137 # rebuild binary packages now that scripts have been patched
138 npm rebuild
139
140 # Replicate ci/dev/postinstall.sh
141 echo "----- Replicate ci/dev/postinstall.sh"
142 yarn --cwd "./vendor" install --modules-folder modules --offline --ignore-scripts --frozen-lockfile
143
144 # Replicate vendor/postinstall.sh
145 echo " ----- Replicate vendor/postinstall.sh"
146 yarn --cwd "./vendor/modules/code-oss-dev" --offline --frozen-lockfile --ignore-scripts install
147
148 # remove all built-in extensions, as these are 3rd party extensions that
149 # get downloaded from vscode marketplace
150 jq --slurp '.[0] * .[1]' "vendor/modules/code-oss-dev/product.json" <(
151 cat << EOF
152 {
153 "builtInExtensions": []
154 }
155 EOF
156 ) | sponge vendor/modules/code-oss-dev/product.json
157
158 # disable automatic updates
159 sed -i '/update.mode/,/\}/{s/default:.*/default: "none",/g}' \
160 vendor/modules/code-oss-dev/src/vs/platform/update/common/update.config.contribution.ts
161
162 # put ripgrep binary into bin, so postinstall does not try to download it
163 find -name vscode-ripgrep -type d \
164 -execdir mkdir -p {}/bin \; \
165 -execdir ln -s ${ripgrep}/bin/rg {}/bin/rg \;
166
167 # Playwright is only needed for tests, we can disable it for builds.
168 # There's an environment variable to disable downloads, but the package makes a breaking call to
169 # sw_vers before that variable is checked.
170 patch -p1 -i ${./playwright.patch}
171
172 # Replicate install vscode dependencies without running script for all vscode packages
173 # that require patching for postinstall scripts to succeed
174 find ./vendor/modules/code-oss-dev -path "*node_modules" -prune -o \
175 -path "./*/*/*/*/*" -name "yarn.lock" -printf "%h\n" | \
176 xargs -I {} yarn --cwd {} \
177 --frozen-lockfile --offline --ignore-scripts --ignore-engines
178
179 # patch shebangs of everything to allow binary packages to build
180 patchShebangs .
181
182 # patch build esbuild
183 mkdir -p vendor/modules/code-oss-dev/build/node_modules/esbuild/bin
184 jq "del(.scripts.postinstall)" vendor/modules/code-oss-dev/build/node_modules/esbuild/package.json | sponge vendor/modules/code-oss-dev/build/node_modules/esbuild/package.json
185 sed -i 's/0.12.6/${esbuild.version}/g' vendor/modules/code-oss-dev/build/node_modules/esbuild/lib/main.js
186 ln -s -f ${esbuild}/bin/esbuild vendor/modules/code-oss-dev/build/node_modules/esbuild/bin/esbuild
187
188 # patch extensions esbuild
189 mkdir -p vendor/modules/code-oss-dev/extensions/node_modules/esbuild/bin
190 jq "del(.scripts.postinstall)" vendor/modules/code-oss-dev/extensions/node_modules/esbuild/package.json | sponge vendor/modules/code-oss-dev/extensions/node_modules/esbuild/package.json
191 sed -i 's/0.11.12/${esbuild.version}/g' vendor/modules/code-oss-dev/extensions/node_modules/esbuild/lib/main.js
192 ln -s -f ${esbuild}/bin/esbuild vendor/modules/code-oss-dev/extensions/node_modules/esbuild/bin/esbuild
193
194 # rebuild binaries, we use npm here, as yarn does not provide an alternative
195 # that would not attempt to try to reinstall everything and break our
196 # patching attempts
197 npm rebuild --prefix vendor/modules/code-oss-dev --update-binary
198
199 # run postinstall scripts after patching
200 find ./vendor/modules/code-oss-dev -path "*node_modules" -prune -o \
201 -path "./*/*/*/*/*" -name "yarn.lock" -printf "%h\n" | \
202 xargs -I {} sh -c 'jq -e ".scripts.postinstall" {}/package.json >/dev/null && yarn --cwd {} postinstall --frozen-lockfile --offline || true'
203
204 # build code-server
205 yarn build
206
207 # build vscode
208 yarn build:vscode
209
210 # create release
211 yarn release
212 '';
213
214 installPhase = ''
215 mkdir -p $out/libexec/code-server $out/bin
216
217 # copy release to libexec path
218 cp -R -T release "$out/libexec/code-server"
219
220 # install only production dependencies
221 yarn --offline --cwd "$out/libexec/code-server" --production
222
223 # link coder-cloud agent from nix store
224 mkdir -p $out/libexec/code-server/lib
225 ln -s "${cloudAgent}/bin/cloud-agent" $out/libexec/code-server/lib/coder-cloud-agent
226
227 # create wrapper
228 makeWrapper "${nodejs-14_x}/bin/node" "$out/bin/code-server" \
229 --add-flags "$out/libexec/code-server/out/node/entry.js"
230 '';
231
232 passthru = {
233 prefetchYarnCache = lib.overrideDerivation yarnCache (d: {
234 outputHash = lib.fakeSha256;
235 });
236 };
237
238 meta = with lib; {
239 description = "Run VS Code on a remote server";
240 longDescription = ''
241 code-server is VS Code running on a remote server, accessible through the
242 browser.
243 '';
244 homepage = "https://github.com/cdr/code-server";
245 license = licenses.mit;
246 maintainers = with maintainers; [ offline ];
247 platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ];
248 };
249}