nixpkgs/pkgs/servers/code-server/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

304 lines
8.4 KiB
Nix
Raw Normal View History

{ lib
, stdenv
, fetchFromGitHub
, buildGoModule
, makeWrapper
, cacert
, moreutils
, jq
, git
, rsync
, pkg-config
, yarn
, python3
, esbuild
, nodejs
, node-gyp
, libsecret
, xorg
, ripgrep
, AppKit
, Cocoa
, CoreServices
, Security
, cctools
, xcbuild
, quilt
, nixosTests
}:
2020-05-08 11:33:07 +00:00
let
system = stdenv.hostPlatform.system;
python = python3;
2020-05-08 11:33:07 +00:00
yarn' = yarn.override { inherit nodejs; };
2022-11-16 11:25:43 +00:00
defaultYarnOpts = [ ];
2020-05-08 11:33:07 +00:00
2023-01-28 04:20:00 +00:00
esbuild' = esbuild.override {
buildGoModule = args: buildGoModule (args // rec {
version = "0.16.17";
src = fetchFromGitHub {
owner = "evanw";
repo = "esbuild";
rev = "v${version}";
hash = "sha256-8L8h0FaexNsb3Mj6/ohA37nYLFogo5wXkAhGztGUUsQ=";
};
vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ=";
});
};
2022-01-10 00:36:05 +00:00
# replaces esbuild's download script with a binary from nixpkgs
patchEsbuild = path: version: ''
2022-01-10 00:36:05 +00:00
mkdir -p ${path}/node_modules/esbuild/bin
jq "del(.scripts.postinstall)" ${path}/node_modules/esbuild/package.json | sponge ${path}/node_modules/esbuild/package.json
2023-01-28 04:20:00 +00:00
sed -i 's/${version}/${esbuild'.version}/g' ${path}/node_modules/esbuild/lib/main.js
ln -s -f ${esbuild'}/bin/esbuild ${path}/node_modules/esbuild/bin/esbuild
2022-01-10 00:36:05 +00:00
'';
commit = "2798322b03e7f446f59c5142215c11711ed7a427";
in
stdenv.mkDerivation (finalAttrs: {
2020-05-08 11:33:07 +00:00
pname = "code-server";
version = "4.13.0";
2020-05-08 11:33:07 +00:00
src = fetchFromGitHub {
2022-11-16 11:25:43 +00:00
owner = "coder";
2020-05-08 11:33:07 +00:00
repo = "code-server";
rev = "v${finalAttrs.version}";
2022-11-16 11:25:43 +00:00
fetchSubmodules = true;
hash = "sha256-4hkKGQU9G3CllD+teWXnYoHaY3YdDz25fwaMUS5OlfM=";
2020-05-08 11:33:07 +00:00
};
yarnCache = stdenv.mkDerivation {
name = "${finalAttrs.pname}-${finalAttrs.version}-${system}-yarn-cache";
inherit (finalAttrs) src;
2021-10-30 22:19:46 +00:00
nativeBuildInputs = [ yarn' git cacert ];
2020-05-08 11:33:07 +00:00
buildPhase = ''
export HOME=$PWD
2021-10-30 22:19:46 +00:00
export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
yarn --cwd "./vendor" install --modules-folder modules --ignore-scripts --frozen-lockfile
2020-05-08 11:33:07 +00:00
yarn config set yarn-offline-mirror $out
find "$PWD" -name "yarn.lock" -printf "%h\n" | \
xargs -I {} yarn --cwd {} \
--frozen-lockfile --ignore-scripts --ignore-platform \
--ignore-engines --no-progress --non-interactive
2022-11-16 11:25:43 +00:00
find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
xargs -I {} yarn --cwd {} \
--ignore-scripts --ignore-engines
2020-05-08 11:33:07 +00:00
'';
2020-05-08 11:33:07 +00:00
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = "sha256-xLcrOVhKC0cOPcS5XwIMyv1KiEE0azZ1z+wS9PPKjAQ=";
2020-05-08 11:33:07 +00:00
};
nativeBuildInputs = [
nodejs
yarn'
python
pkg-config
makeWrapper
git
rsync
jq
moreutils
quilt
2020-05-08 11:33:07 +00:00
];
2021-01-18 00:03:35 +00:00
buildInputs = lib.optionals (!stdenv.isDarwin) [ libsecret ]
++ (with xorg; [ libX11 libxkbfile ])
++ lib.optionals stdenv.isDarwin [
AppKit
Cocoa
CoreServices
Security
cctools
xcbuild
];
2021-01-18 00:03:35 +00:00
patches = [
# Remove all git calls from the VS Code build script except `git rev-parse
# HEAD` which is replaced in postPatch with the commit.
2022-11-16 11:25:43 +00:00
./build-vscode-nogit.patch
2021-01-18 00:03:35 +00:00
];
2020-05-08 11:33:07 +00:00
2020-10-18 19:10:41 +00:00
postPatch = ''
2020-05-08 11:33:07 +00:00
export HOME=$PWD
patchShebangs ./ci
# inject git commit
substituteInPlace ./ci/build/build-vscode.sh \
--replace '$(git rev-parse HEAD)' "${commit}"
substituteInPlace ./ci/build/build-release.sh \
--replace '$(git rev-parse HEAD)' "${commit}"
2020-05-08 11:33:07 +00:00
'';
configurePhase = ''
runHook preConfigure
2021-01-18 00:03:35 +00:00
# run yarn offline by default
echo '--install.offline true' >> .yarnrc
2020-05-08 11:33:07 +00:00
# set default yarn opts
2021-01-15 07:07:56 +00:00
${lib.concatMapStrings (option: ''
2020-05-08 11:33:07 +00:00
yarn --offline config set ${option}
'') defaultYarnOpts}
# set offline mirror to yarn cache we created in previous steps
yarn --offline config set yarn-offline-mirror "${finalAttrs.yarnCache}"
2020-05-08 11:33:07 +00:00
2020-10-18 19:10:41 +00:00
# skip unnecessary electron download
export ELECTRON_SKIP_BINARY_DOWNLOAD=1
2022-01-10 00:36:05 +00:00
# set nodedir to prevent node-gyp from downloading headers
# taken from https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific
mkdir -p $HOME/.node-gyp/${nodejs.version}
echo 9 > $HOME/.node-gyp/${nodejs.version}/installVersion
ln -sfv ${nodejs}/include $HOME/.node-gyp/${nodejs.version}
export npm_config_nodedir=${nodejs}
# use updated node-gyp. fixes the following error on Darwin:
# PermissionError: [Errno 1] Operation not permitted: '/usr/sbin/pkgutil'
export npm_config_node_gyp=${node-gyp}/lib/node_modules/node-gyp/bin/node-gyp.js
runHook postConfigure
2020-05-08 11:33:07 +00:00
'';
buildPhase = ''
runHook preBuild
2020-05-08 11:33:07 +00:00
# install code-server dependencies
2021-10-30 22:19:46 +00:00
yarn --offline --ignore-scripts
2020-05-08 11:33:07 +00:00
2022-11-16 11:25:43 +00:00
# apply patches
quilt push -a
2021-10-30 22:19:46 +00:00
# patch shebangs of everything to allow binary packages to build
patchShebangs .
2022-11-16 11:25:43 +00:00
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
export SKIP_SUBMODULE_DEPS=1
export NODE_OPTIONS=--openssl-legacy-provider
2021-10-30 22:19:46 +00:00
# rebuild binary packages now that scripts have been patched
2022-11-16 11:25:43 +00:00
echo "----- NPM rebuild"
npm rebuild --prefer-offline
2021-10-30 22:19:46 +00:00
# Replicate ci/dev/postinstall.sh
echo "----- Replicate ci/dev/postinstall.sh"
yarn --cwd "./vendor" install --modules-folder modules --offline --ignore-scripts --frozen-lockfile
# remove all built-in extensions, as these are 3rd party extensions that
# get downloaded from vscode marketplace
2022-11-16 11:25:43 +00:00
jq --slurp '.[0] * .[1]' "./lib/vscode/product.json" <(
2021-10-30 22:19:46 +00:00
cat << EOF
{
"builtInExtensions": []
}
EOF
2022-11-16 11:25:43 +00:00
) | sponge ./lib/vscode/product.json
2021-10-30 22:19:46 +00:00
# disable automatic updates
sed -i '/update.mode/,/\}/{s/default:.*/default: "none",/g}' \
2022-11-16 11:25:43 +00:00
lib/vscode/src/vs/platform/update/common/update.config.contribution.ts
2021-10-30 22:19:46 +00:00
2022-01-10 00:36:05 +00:00
# Patch out remote download of nodejs from build script
patch -p1 -i ${./remove-node-download.patch}
2022-11-16 11:25:43 +00:00
# Fetch packages for vscode
find ./lib/vscode -name "yarn.lock" -printf "%h\n" | \
2021-10-30 22:19:46 +00:00
xargs -I {} yarn --cwd {} \
2022-11-16 11:25:43 +00:00
--frozen-lockfile --ignore-scripts --ignore-engines
2022-01-10 00:36:05 +00:00
2021-10-30 22:19:46 +00:00
# patch shebangs of everything to allow binary packages to build
patchShebangs .
2022-11-16 11:25:43 +00:00
${patchEsbuild "./lib/vscode/build" "0.12.6"}
${patchEsbuild "./lib/vscode/extensions" "0.11.23"}
'' + lib.optionalString stdenv.isDarwin ''
2022-01-10 00:36:05 +00:00
# use prebuilt binary for @parcel/watcher, which requires macOS SDK 10.13+
# (see issue #101229)
2022-11-16 11:25:43 +00:00
pushd ./lib/vscode/remote/node_modules/@parcel/watcher
2022-01-10 00:36:05 +00:00
mkdir -p ./build/Release
mv ./prebuilds/darwin-x64/node.napi.glibc.node ./build/Release/watcher.node
jq "del(.scripts) | .gypfile = false" ./package.json | sponge ./package.json
popd
'' + ''
2022-11-16 11:25:43 +00:00
# put ripgrep binary into bin, so postinstall does not try to download it
find -name ripgrep -type d \
-execdir mkdir -p {}/bin \; \
-execdir ln -s ${ripgrep}/bin/rg {}/bin/rg \;
2020-05-08 11:33:07 +00:00
2021-10-30 22:19:46 +00:00
# run postinstall scripts after patching
find ./lib/vscode \( -path "*/node_modules/*" -or -path "*/extensions/*" \) \
-and -type f -name "yarn.lock" -printf "%h\n" | \
2021-10-30 22:19:46 +00:00
xargs -I {} sh -c 'jq -e ".scripts.postinstall" {}/package.json >/dev/null && yarn --cwd {} postinstall --frozen-lockfile --offline || true'
2020-05-08 11:33:07 +00:00
# build code-server
yarn build
# build vscode
VERSION=${finalAttrs.version} yarn build:vscode
2020-05-08 11:33:07 +00:00
# inject version into package.json
jq --slurp '.[0] * .[1]' ./package.json <(
cat << EOF
{
"version": "${finalAttrs.version}"
}
EOF
) | sponge ./package.json
2020-05-08 11:33:07 +00:00
# create release
yarn release
runHook postBuild
2020-05-08 11:33:07 +00:00
'';
installPhase = ''
runHook preInstall
2020-05-08 11:33:07 +00:00
mkdir -p $out/libexec/code-server $out/bin
# copy release to libexec path
cp -R -T release "$out/libexec/code-server"
# install only production dependencies
yarn --offline --cwd "$out/libexec/code-server" --production
# create wrapper
makeWrapper "${nodejs}/bin/node" "$out/bin/code-server" \
2020-05-08 11:33:07 +00:00
--add-flags "$out/libexec/code-server/out/node/entry.js"
runHook postInstall
2020-05-08 11:33:07 +00:00
'';
passthru = {
prefetchYarnCache = lib.overrideDerivation finalAttrs.yarnCache (d: {
2021-01-15 07:07:56 +00:00
outputHash = lib.fakeSha256;
2020-05-08 11:33:07 +00:00
});
2023-05-17 06:22:43 +00:00
tests = {
inherit (nixosTests) code-server;
};
2020-05-08 11:33:07 +00:00
};
meta = {
description = "Run VS Code on a remote server";
2020-05-08 11:33:07 +00:00
longDescription = ''
code-server is VS Code running on a remote server, accessible through the
browser.
'';
2022-11-16 11:25:43 +00:00
homepage = "https://github.com/coder/code-server";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ offline henkery ];
2021-01-18 00:03:35 +00:00
platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ];
2020-05-08 11:33:07 +00:00
};
})