Replace yarn alias with a shell script

This makes the yarn command available in Nix shell and direnv.
This commit is contained in:
Stéphan Kochen
2022-10-18 19:44:12 +02:00
parent 096172f23f
commit d23c611065
2 changed files with 25 additions and 26 deletions
+5 -2
View File
@@ -46,13 +46,16 @@ export const renderTmpl = (
let result = tmpl;
for (const [name, value] of Object.entries(vars)) {
if (typeof value === "string") {
result = result.replace(`@@${name}@@`, value);
result = result.replace(new RegExp(`@@${name}@@`, "g"), value);
}
if (typeof value === "boolean") {
while (true) {
const lines = result.split("\n");
const startIdx = lines.indexOf(`#@@ IF ${name}`);
const endIdx = lines.indexOf(`#@@ ENDIF ${name}`);
if (startIdx !== -1 && endIdx > startIdx) {
if (startIdx === -1 || endIdx < startIdx) {
break;
}
if (value) {
lines.splice(endIdx, 1);
lines.splice(startIdx, 1);
+16 -20
View File
@@ -1,49 +1,49 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
{ lib, nodejs, stdenv, fetchurl, writeText, git, cacert, writeShellApplication }:
{ lib, stdenv, nodejs, git, cacert, fetchurl, writeShellScript, writeShellScriptBin }:
{ src, overrideAttrs ? null, ... } @ args:
let
yarnPath = ./@@YARN_PATH@@;
lockfile = ./@@LOCKFILE@@;
cacheFolder = @@CACHE_FOLDER@@;
#@@ IF NEED_ISOLATED_BUILD_SUPPRORT
lockfile = ./@@LOCKFILE@@;
#@@ ENDIF NEED_ISOLATED_BUILD_SUPPRORT
# Call overrideAttrs on a derivation if a function is provided.
optionalOverride = fn: drv:
if fn == null then drv else drv.overrideAttrs fn;
# Simple stub that provides the global yarn command.
yarn = writeShellScriptBin "yarn" ''
exec '${nodejs}/bin/node' '${./@@YARN_PATH@@}' "$@"
'';
# Common attributes between Yarn derivations.
drvCommon = {
# Make sure the build uses the right Node.js version everywhere.
buildInputs = [ nodejs ];
buildInputs = [ nodejs yarn ];
# Tell node-gyp to use the provided Node.js headers for native code builds.
npm_config_nodedir = nodejs;
# Tell node-pre-gyp to never fetch binaries / always build from source.
npm_config_build_from_source = "true";
# Defines the shell alias to run Yarn.
postHook = ''
yarn() {
CI=1 node "${yarnPath}" "$@"
}
'';
};
# Create derivations for fetching dependencies.
cacheDrvs = let
builder = builtins.toFile "builder.sh" ''
builder = writeShellScript "yarn-cache-builder" ''
source $stdenv/setup
cd "$src"
HOME="$TMP" yarn_cache_folder="$TMP" CI=1 \
node '${yarnPath}' nixify fetch-one $locator
yarn nixify fetch-one $locator
# Because we change the cache dir, Yarn may generate a different name.
mv "$TMP/$(sed 's/-[^-]*\.[^-]*$//' <<< "$outputFilename")"-* $out
'';
in lib.mapAttrs (locator: { filename, sha512 }: stdenv.mkDerivation {
inherit src builder locator;
name = lib.strings.sanitizeDerivationName locator;
buildInputs = [ nodejs git cacert ];
buildInputs = [ yarn git cacert ];
outputFilename = filename;
outputHashMode = "flat";
outputHashAlgo = "sha512";
@@ -52,7 +52,7 @@ let
# Create a shell snippet to copy dependencies from a list of derivations.
mkCacheBuilderForDrvs = drvs:
writeText "collect-cache.sh" (lib.concatMapStrings (drv: ''
writeShellScript "collect-yarn-cache" (lib.concatMapStrings (drv: ''
cp ${drv} '${drv.outputFilename}'
'') drvs);
@@ -172,14 +172,10 @@ let
passthru = {
inherit nodejs;
yarn = writeShellApplication {
name = "yarn";
runtimeInputs = [ nodejs ];
text = ''
${yarnPath} --cwd ${project}/libexec/${project.name} "$@"
yarn = writeShellScriptBin "yarn" ''
exec '${yarn}/bin/yarn' --cwd '${project}/libexec/${project.name}' "$@"
'';
};
};
});
@@CACHE_ENTRIES@@