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