Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib, stdenv, fetchFromGitHub, buildGoModule, makeWrapper, runCommand
2, cacert, moreutils, jq, git, rsync, pkg-config, yarn, python3
3, esbuild, nodejs-16_x, node-gyp, libsecret, xorg, ripgrep
4, AppKit, Cocoa, CoreServices, Security, cctools, xcbuild, quilt }:
5
6let
7 system = stdenv.hostPlatform.system;
8
9 nodejs = nodejs-16_x;
10 python = python3;
11 yarn' = yarn.override { inherit nodejs; };
12 defaultYarnOpts = [ ];
13
14 # replaces esbuild's download script with a binary from nixpkgs
15 patchEsbuild = path : version : ''
16 mkdir -p ${path}/node_modules/esbuild/bin
17 jq "del(.scripts.postinstall)" ${path}/node_modules/esbuild/package.json | sponge ${path}/node_modules/esbuild/package.json
18 sed -i 's/${version}/${esbuild.version}/g' ${path}/node_modules/esbuild/lib/main.js
19 ln -s -f ${esbuild}/bin/esbuild ${path}/node_modules/esbuild/bin/esbuild
20 '';
21
22in stdenv.mkDerivation rec {
23 pname = "code-server";
24 version = "4.8.3";
25
26 src = fetchFromGitHub {
27 owner = "coder";
28 repo = "code-server";
29 rev = "v${version}";
30 fetchSubmodules = true;
31 sha256 = "1h5ng60wf3gpsydfkv20x30xsw1f5zcvv77l1mzrqz1mhcw93lvz";
32 };
33
34 cloudAgent = buildGoModule rec {
35 pname = "cloud-agent";
36 version = "0.2.6";
37
38 src = fetchFromGitHub {
39 owner = "coder";
40 repo = "cloud-agent";
41 rev = "v${version}";
42 sha256 = "1s3jpgvzizc9skc27c3x35sya2p4ywhvdi3l73927z3j47wszy7f";
43 };
44
45 vendorSha256 = "14xzlbmki8fk8mbcci62q8sklyd0nyga07ww1ap0vdrv7d1g31hn";
46
47 postPatch = ''
48 # the cloud-agent release tag has an empty version string, so add it back in
49 substituteInPlace internal/version/version.go \
50 --replace 'var Version string' 'var Version string = "v${version}"'
51 '';
52 };
53
54 yarnCache = stdenv.mkDerivation {
55 name = "${pname}-${version}-${system}-yarn-cache";
56 inherit src;
57 nativeBuildInputs = [ yarn' git cacert ];
58 buildPhase = ''
59 export HOME=$PWD
60 export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
61
62 yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
63
64 yarn config set yarn-offline-mirror $out
65 find "$PWD" -name "yarn.lock" -printf "%h\n" | \
66 xargs -I {} yarn --cwd {} \
67 --frozen-lockfile --ignore-scripts --ignore-platform \
68 --ignore-engines --no-progress --non-interactive
69
70 find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
71 xargs -I {} yarn --cwd {} \
72 --ignore-scripts --ignore-engines
73 '';
74 outputHashMode = "recursive";
75 outputHashAlgo = "sha256";
76
77 # to get hash values use nix-build -A code-server.prefetchYarnCache
78 outputHash = "0jzzbmmgv1nfq975mi9ii9l6c4f1wy10fyy117xgm4s6vxana7qn";
79 };
80
81 nativeBuildInputs = [
82 nodejs yarn' python pkg-config makeWrapper git rsync jq moreutils quilt
83 ];
84 buildInputs = lib.optionals (!stdenv.isDarwin) [ libsecret ]
85 ++ (with xorg; [ libX11 libxkbfile ])
86 ++ lib.optionals stdenv.isDarwin [
87 AppKit Cocoa CoreServices Security cctools xcbuild
88 ];
89
90 patches = [
91 # remove download of coder-cloud agent
92 ./remove-cloud-agent-download.patch
93 # remove git calls from vscode build script
94 ./build-vscode-nogit.patch
95 ];
96
97 postPatch = ''
98 export HOME=$PWD
99
100 patchShebangs ./ci
101
102 # inject git commit
103 substituteInPlace ci/build/build-release.sh \
104 --replace '$(git rev-parse HEAD)' "$commit"
105 '';
106
107 configurePhase = ''
108 # run yarn offline by default
109 echo '--install.offline true' >> .yarnrc
110
111 # set default yarn opts
112 ${lib.concatMapStrings (option: ''
113 yarn --offline config set ${option}
114 '') defaultYarnOpts}
115
116 # set offline mirror to yarn cache we created in previous steps
117 yarn --offline config set yarn-offline-mirror "${yarnCache}"
118
119 # link coder-cloud agent from nix store
120 mkdir -p lib
121 ln -s "${cloudAgent}/bin/cloud-agent" ./lib/coder-cloud-agent
122
123 # skip unnecessary electron download
124 export ELECTRON_SKIP_BINARY_DOWNLOAD=1
125
126 # set nodedir to prevent node-gyp from downloading headers
127 # taken from https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific
128 mkdir -p $HOME/.node-gyp/${nodejs.version}
129 echo 9 > $HOME/.node-gyp/${nodejs.version}/installVersion
130 ln -sfv ${nodejs}/include $HOME/.node-gyp/${nodejs.version}
131 export npm_config_nodedir=${nodejs}
132
133 # use updated node-gyp. fixes the following error on Darwin:
134 # PermissionError: [Errno 1] Operation not permitted: '/usr/sbin/pkgutil'
135 export npm_config_node_gyp=${node-gyp}/lib/node_modules/node-gyp/bin/node-gyp.js
136 '';
137
138 buildPhase = ''
139 # install code-server dependencies
140 yarn --offline --ignore-scripts
141
142 # apply patches
143 quilt push -a
144
145 # patch shebangs of everything to allow binary packages to build
146 patchShebangs .
147
148 export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
149 export SKIP_SUBMODULE_DEPS=1
150 export NODE_OPTIONS=--openssl-legacy-provider
151
152 # rebuild binary packages now that scripts have been patched
153 echo "----- NPM rebuild"
154 npm rebuild --prefer-offline
155
156 # Replicate ci/dev/postinstall.sh
157 echo "----- Replicate ci/dev/postinstall.sh"
158 yarn --cwd "./vendor" install --modules-folder modules --offline --ignore-scripts --frozen-lockfile
159
160 # remove all built-in extensions, as these are 3rd party extensions that
161 # get downloaded from vscode marketplace
162 jq --slurp '.[0] * .[1]' "./lib/vscode/product.json" <(
163 cat << EOF
164 {
165 "builtInExtensions": []
166 }
167 EOF
168 ) | sponge ./lib/vscode/product.json
169
170 # disable automatic updates
171 sed -i '/update.mode/,/\}/{s/default:.*/default: "none",/g}' \
172 lib/vscode/src/vs/platform/update/common/update.config.contribution.ts
173
174 # Patch out remote download of nodejs from build script
175 patch -p1 -i ${./remove-node-download.patch}
176
177 # Fetch packages for vscode
178 find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
179 xargs -I {} yarn --cwd {} \
180 --frozen-lockfile --ignore-scripts --ignore-engines
181
182 # patch shebangs of everything to allow binary packages to build
183 patchShebangs .
184
185 ${patchEsbuild "./lib/vscode/build" "0.12.6"}
186 ${patchEsbuild "./lib/vscode/extensions" "0.11.23"}
187 '' + lib.optionalString stdenv.isDarwin ''
188 # use prebuilt binary for @parcel/watcher, which requires macOS SDK 10.13+
189 # (see issue #101229)
190 pushd ./lib/vscode/remote/node_modules/@parcel/watcher
191 mkdir -p ./build/Release
192 mv ./prebuilds/darwin-x64/node.napi.glibc.node ./build/Release/watcher.node
193 jq "del(.scripts) | .gypfile = false" ./package.json | sponge ./package.json
194 popd
195 '' + ''
196
197 # put ripgrep binary into bin, so postinstall does not try to download it
198 find -name ripgrep -type d \
199 -execdir mkdir -p {}/bin \; \
200 -execdir ln -s ${ripgrep}/bin/rg {}/bin/rg \;
201
202 # run postinstall scripts after patching
203 find ./lib/vscode -path "*node_modules" -prune -o \
204 -path "./*/*/*/*/*" -name "yarn.lock" -printf "%h\n" | \
205 xargs -I {} sh -c 'jq -e ".scripts.postinstall" {}/package.json >/dev/null && yarn --cwd {} postinstall --frozen-lockfile --offline || true'
206
207 # build code-server
208 yarn build
209
210 # build vscode
211 yarn build:vscode
212
213 # create release
214 yarn release
215 '';
216
217 installPhase = ''
218 mkdir -p $out/libexec/code-server $out/bin
219
220 # copy release to libexec path
221 cp -R -T release "$out/libexec/code-server"
222
223 # install only production dependencies
224 yarn --offline --cwd "$out/libexec/code-server" --production
225
226 # link coder-cloud agent from nix store
227 mkdir -p $out/libexec/code-server/lib
228 ln -s "${cloudAgent}/bin/cloud-agent" $out/libexec/code-server/lib/coder-cloud-agent
229
230 # create wrapper
231 makeWrapper "${nodejs-16_x}/bin/node" "$out/bin/code-server" \
232 --add-flags "$out/libexec/code-server/out/node/entry.js"
233 '';
234
235 passthru = {
236 prefetchYarnCache = lib.overrideDerivation yarnCache (d: {
237 outputHash = lib.fakeSha256;
238 });
239 };
240
241 meta = with lib; {
242 description = "Run VS Code on a remote server";
243 longDescription = ''
244 code-server is VS Code running on a remote server, accessible through the
245 browser.
246 '';
247 homepage = "https://github.com/coder/code-server";
248 license = licenses.mit;
249 maintainers = with maintainers; [ offline henkery ];
250 platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ];
251 };
252}