yarnInstallHook: init (#328544)

This commit is contained in:
Philip Taron
2024-09-04 14:33:47 -07:00
committed by GitHub
10 changed files with 133 additions and 13286 deletions
@@ -467,6 +467,7 @@ Yarn based projects use a `yarn.lock` file instead of a `package-lock.json` to p
- `yarnConfigHook`: Fetches the dependencies from the offline cache and installs them into `node_modules`.
- `yarnBuildHook`: Runs `yarn build` or a specified `yarn` command that builds the project.
- `yarnInstallHook`: Runs `yarn install --production` to prune dependencies and installs the project into `$out`.
An example usage of the above attributes is:
@@ -501,9 +502,9 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [
yarnConfigHook
yarnBuildHook
yarnInstallHook
# Needed for executing package.json scripts
nodejs
npmHooks.npmInstallHook
];
meta = {
@@ -512,8 +513,6 @@ stdenv.mkDerivation (finalAttrs: {
})
```
Note that there is no setup hook for installing yarn based packages - `npmHooks.npmInstallHook` should fit most cases, but sometimes you may need to override the `installPhase` completely.
#### `yarnConfigHook` arguments {#javascript-yarnconfighook}
By default, `yarnConfigHook` relies upon the attribute `${yarnOfflineCache}` (or `${offlineCache}` if the former is not set) to find the location of the offline cache produced by `fetchYarnDeps`. To disable this phase, you can set `dontYarnInstallDeps = true` or override the `configurePhase`.
@@ -525,9 +524,15 @@ This script by default runs `yarn --offline build`, and it relies upon the proje
- `yarnBuildScript`: Sets a different `yarn --offline` subcommand (defaults to `build`).
- `yarnBuildFlags`: Single string list of additional flags to pass the above command, or a Nix list of such additional flags.
#### `yarnInstallHook` arguments {#javascript-yarninstallhook}
To install the package `yarnInstallHook` uses both `npm` and `yarn` to cleanup project files and dependencies. To disable this phase, you can set `dontYarnInstall = true` or override the `installPhase`. Below is a list of additional `mkDerivation` arguments read by this hook:
- `yarnKeepDevDeps`: Disables the removal of devDependencies from `node_modules` before installation.
### yarn2nix {#javascript-yarn2nix}
WARNING: The `yarn2nix` functions have been deprecated in favor of the new `yarnConfigHook` and `yarnBuildHook`. Documentation for them still appears here for the sake of the packages that still use them. See also a tracking issue [#324246](https://github.com/NixOS/nixpkgs/issues/324246).
WARNING: The `yarn2nix` functions have been deprecated in favor of the new `yarnConfigHook`, `yarnBuildHook` and `yarnInstallHook`. Documentation for them still appears here for the sake of the packages that still use them. See also a tracking issue [#324246](https://github.com/NixOS/nixpkgs/issues/324246).
#### Preparation {#javascript-yarn2nix-preparation}
@@ -2,9 +2,14 @@
stdenv,
lib,
makeWrapper,
installShellFiles,
nodejsInstallManuals,
nodejsInstallExecutables,
coreutils,
nix-prefetch-git,
fetchurl,
jq,
nodejs,
nodejs-slim,
prefetch-yarn-deps,
fixup-yarn-lock,
@@ -175,4 +180,16 @@ in
description = "Run yarn build in buildPhase";
};
} ./yarn-build-hook.sh;
yarnInstallHook = makeSetupHook {
name = "yarn-install-hook";
propagatedBuildInputs = [
yarn
nodejsInstallManuals
nodejsInstallExecutables
];
substitutions = {
jq = lib.getExe jq;
};
} ./yarn-install-hook.sh;
}
@@ -0,0 +1,79 @@
# shellcheck shell=bash
yarnInstallHook() {
echo "Executing yarnInstallHook"
runHook preInstall
local -r packageOut="$out/lib/node_modules/$(@jq@ --raw-output '.name' ./package.json)"
mkdir -p "$packageOut"
local -ar yarnArgs=(
--ignore-engines
--ignore-platform
--ignore-scripts
--no-progress
--non-interactive
--offline
)
local -r tmpDir="$(mktemp -d yarnInstallHook.XXXXXX)"
# yarn pack does not work at all with bundleDependencies.
# Since we are imediately unpacking, we can just remove them from package.json
# This will NOT be fixed in yarn v1: https://github.com/yarnpkg/yarn/issues/6794
mv ./package.json "$tmpDir/package.json.orig"
# Note: two spellings are accepted, 'bundleDependencies' and 'bundledDependencies'
@jq@ 'del(.bundleDependencies)|del(.bundledDependencies)' "$tmpDir/package.json.orig" > ./package.json
# TODO: figure out a way to avoid redundant compress/decompress steps
yarn pack \
--filename "$tmpDir/yarn-pack.tgz" \
"${yarnArgs[@]}"
tar xzf "$tmpDir/yarn-pack.tgz" \
-C "$packageOut" \
--strip-components 1 \
package/
mv "$tmpDir/package.json.orig" ./package.json
nodejsInstallExecutables ./package.json
nodejsInstallManuals ./package.json
local -r nodeModulesPath="$packageOut/node_modules"
if [ ! -d "$nodeModulesPath" ]; then
if [ -z "${yarnKeepDevDeps-}" ]; then
# Yarn has a 'prune' command, but it's only a stub that directs you to use install
if ! yarn install \
--frozen-lockfile \
--force \
--production=true \
"${yarnArgs[@]}"
then
echo
echo
echo "ERROR: yarn prune step failed"
echo
echo 'If yarn tried to download additional dependencies above, try setting `yarnKeepDevDeps = true`.'
echo
exit 1
fi
fi
find node_modules -maxdepth 1 -type d -empty -delete
cp -r node_modules "$nodeModulesPath"
fi
runHook postInstall
echo "Finished yarnInstallHook"
}
if [ -z "${dontYarnInstall-}" ] && [ -z "${installPhase-}" ]; then
installPhase=yarnInstallHook
fi
File diff suppressed because it is too large Load Diff
+21 -10
View File
@@ -1,33 +1,44 @@
{
stdenvNoCC,
lib,
buildNpmPackage,
fetchFromGitHub,
fetchYarnDeps,
yarnConfigHook,
yarnBuildHook,
yarnInstallHook,
nodejs,
nix-update-script,
testers,
writeText,
runCommand,
blade-formatter,
nodejs,
}:
buildNpmPackage rec {
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "blade-formatter";
version = "1.41.1";
src = fetchFromGitHub {
owner = "shufo";
repo = "blade-formatter";
rev = "v${version}";
rev = "v${finalAttrs.version}";
hash = "sha256-iaWpIa+H+ocAXGc042PfmCu9UcJZeso9ripWB2/1oTs=";
};
postPatch = ''
cp ${./package-lock.json} ./package-lock.json
'';
yarnOfflineCache = fetchYarnDeps {
yarnLock = finalAttrs.src + "/yarn.lock";
hash = "sha256-zn0PgLIWk23EhYeOKF2RkpvLOusVrqoBazKcJpIAzm8=";
};
npmDepsHash = "sha256-wEz0DTbg+Fdmsf0Qyeu9QS+I8gkPJeaJC/3HuP913og=";
nativeBuildInputs = [
yarnConfigHook
yarnBuildHook
yarnInstallHook
nodejs
];
passthru = {
updateScript = ./update.sh;
updateScript = nix-update-script { };
tests = {
version = testers.testVersion {
package = blade-formatter;
@@ -64,4 +75,4 @@ buildNpmPackage rec {
mainProgram = "blade-formatter";
inherit (nodejs.meta) platforms;
};
}
})
-38
View File
@@ -1,38 +0,0 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p ripgrep common-updater-scripts prefetch-npm-deps jq sd
set -xeu -o pipefail
PACKAGE_DIR="$(realpath "$(dirname "$0")")"
cd "$PACKAGE_DIR/.."
while ! test -f default.nix; do cd .. ; done
NIXPKGS_DIR="$PWD"
new_version="$(
list-git-tags --url=https://github.com/shufo/blade-formatter \
| rg '^v([\d.]*)' -r '$1' \
| sort --version-sort \
| tail -n1
)"
cd "$NIXPKGS_DIR"
update-source-version blade-formatter "$new_version"
TMPDIR="$(mktemp -d)"
cd "$TMPDIR"
src="$(nix-build --no-link "$NIXPKGS_DIR" -A blade-formatter.src)"
cp $src/package.json .
npm update
cp ./package-lock.json "$PACKAGE_DIR"
prev_npm_hash="$(nix-instantiate "$NIXPKGS_DIR" \
--eval --json \
-A blade-formatter.npmDepsHash \
| jq -r .
)"
new_npm_hash="$(prefetch-npm-deps ./package-lock.json)"
sd --fixed-strings "$prev_npm_hash" "$new_npm_hash" "$PACKAGE_DIR/package.nix"
rm -rf "$TMPDIR"
+2 -4
View File
@@ -1,4 +1,4 @@
{ lib, stdenv, fetchFromGitHub, fetchYarnDeps, yarnConfigHook, npmHooks, nodejs, testers }:
{ lib, stdenv, fetchFromGitHub, fetchYarnDeps, yarnConfigHook, yarnInstallHook, nodejs, testers }:
stdenv.mkDerivation (finalAttrs: {
pname = "codefresh";
@@ -17,11 +17,9 @@ stdenv.mkDerivation (finalAttrs: {
};
nativeBuildInputs = [
yarnConfigHook
npmHooks.npmInstallHook
yarnInstallHook
nodejs
];
# Tries to fetch stuff from the internet
dontNpmPrune = true;
passthru.tests.version = testers.testVersion {
package = finalAttrs.finalPackage;
+2 -5
View File
@@ -4,8 +4,8 @@
, fetchYarnDeps
, yarnConfigHook
, yarnBuildHook
, yarnInstallHook
, nodejs
, npmHooks
}:
let
@@ -37,12 +37,9 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [
yarnConfigHook
yarnBuildHook
yarnInstallHook
nodejs
npmHooks.npmInstallHook
];
# From some reason causes the build to fail due to dependencies not available
# offline
dontNpmPrune = true;
meta = with lib; {
homepage = "https://github.com/element-hq/element-call";
+2 -4
View File
@@ -4,8 +4,8 @@
, fetchYarnDeps
, yarnConfigHook
, yarnBuildHook
, yarnInstallHook
, nodejs
, npmHooks
}:
stdenv.mkDerivation (finalAttrs: {
@@ -27,8 +27,8 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [
yarnConfigHook
yarnBuildHook
yarnInstallHook
nodejs
npmHooks.npmInstallHook
];
# Upstream doesn't include a script in package.json that only builds without
# testing, and tests fail because they need to access online websites. Hence
@@ -39,8 +39,6 @@ stdenv.mkDerivation (finalAttrs: {
postBuild = ''
yarn --offline run rollup -c
'';
# Tries to download stuff from the internet in this phase.
dontNpmPrune = true;
meta = {
changelog = "https://github.com/postlight/parser/blob/${finalAttrs.src.rev}/CHANGELOG.md";
+1
View File
@@ -971,6 +971,7 @@ with pkgs;
prefetch-yarn-deps
yarnConfigHook
yarnBuildHook
yarnInstallHook
fetchYarnDeps;
find-cursor = callPackage ../tools/X11/find-cursor { };