Cleanup of yarn-project.nix

This commit is contained in:
Stéphan Kochen
2020-08-25 18:20:16 +02:00
parent 7682962581
commit 708e36eaf4
4 changed files with 49 additions and 69 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
# This is a minimal `default.nix` by yarn-plugin-nixify. You can customize it
# as needed, it will not be overwritten by the plugin.
{ pkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> { } }:
pkgs.callPackage ./yarn-project.nix {}
pkgs.callPackage ./yarn-project.nix { } ./.
+7 -1
View File
@@ -71,11 +71,17 @@ const generate = async (project: Project, cache: Cache, report: Report) => {
const filename = ppath.basename(cachePath);
if (!cacheFiles.has(filename)) continue;
let name = structUtils.slugifyIdent(pkg).replace(/^@/, "_at_");
if (pkg.version) {
name += `-${pkg.version}`;
}
const sha512 = checksum.split(`/`).pop();
cacheEntries.push([
`name = ${JSON.stringify(name)};`,
`filename = ${JSON.stringify(filename)};`,
`sha512 = ${JSON.stringify(sha512)};`,
`locator-hash = ${JSON.stringify(pkg.locatorHash)};`,
`locatorHash = ${JSON.stringify(pkg.locatorHash)};`,
]);
}
+30 -56
View File
@@ -1,65 +1,39 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
{ lib, coreutils, nodejs, stdenv, src ? ./. }:
with lib;
{ lib, coreutils, nodejs, stdenv }: src:
let
# Variables provided by the generator.
project-name = @@PROJECT_NAME@@;
yarn-path = @@YARN_PATH@@;
cache-folder = @@CACHE_FOLDER@@;
cache-entries = @@CACHE_ENTRIES@@;
# Defines the shell alias to run Yarn.
yarn-alias = ''
yarn() {
CI=1 node "$NIX_YARN_PATH" "$@"
}
'';
yarnPath = @@YARN_PATH@@;
cacheFolder = @@CACHE_FOLDER@@;
cacheEntries = @@CACHE_ENTRIES@@;
# Fetch a single dependency.
fetch-one = { filename, sha512, locator-hash }: stdenv.mkDerivation {
name = replaceStrings [ "@" ] [ "-" ] filename;
buildInputs = [ nodejs ];
fetchOne = let
builder = builtins.toFile "builder.sh" ''
source $stdenv/setup
# Yarn may need a writable home directory for the global cache mirror.
# TODO: Can't disable the mirror, because it changes cache filenames.
export HOME="$PWD"
# Fetch into the build directory.
export yarn_cache_folder="$PWD"
# Setup to environment so we can run Yarn.
pushd '${src}' > /dev/null
export NIX_YARN_PATH="$(readlink -f '${yarn-path}')"
${yarn-alias}
# Invoke a plugin internal command to build the cache.
yarn nixify fetch-one '${locator-hash}'
popd > /dev/null
# The cache file that was just fetched is our output.
mv '${filename}' $out
cd "$src"
HOME="$TMP" yarn_cache_folder="$TMP" CI=1 \
node '${yarnPath}' nixify fetch-one $locatorHash
mv "$TMP/$outputFilename" $out
'';
in { name, filename, sha512, locatorHash }: stdenv.mkDerivation {
inherit name src builder locatorHash;
buildInputs = [ nodejs ];
outputFilename = filename;
outputHashMode = "flat";
outputHashAlgo = "sha512";
outputHash = sha512;
};
# Shell snippet to collect all project dependencies.
collect-cache = concatMapStrings (args: ''
cp ${fetch-one args} './${args.filename}'
'') cache-entries;
collectCache = lib.concatMapStrings (args: ''
cp ${fetchOne args} '${args.filename}'
'') cacheEntries;
in stdenv.mkDerivation {
name = project-name;
name = @@PROJECT_NAME@@;
inherit src;
# Tell node-gyp to use the provided Node.js headers for native code builds.
@@ -68,24 +42,28 @@ in stdenv.mkDerivation {
npm_config_build_from_source = "true";
# Make sure the build uses the right Node.js version everywhere.
buildInputs = [ coreutils nodejs ];
buildInputs = [ nodejs ];
# Define the Yarn alias in the build environment.
postHook = yarn-alias;
# Defines the shell alias to run Yarn.
postHook = ''
yarn() {
CI=1 node "$NIX_YARN_PATH" "$@"
}
'';
configurePhase = ''
runHook preConfigure
# Copy over the Yarn cache.
# TODO: Can we do without the copy somehow? Links don't work.
rm -fr '${cache-folder}'
mkdir -p '${cache-folder}'
pushd '${cache-folder}' > /dev/null
${collect-cache}
rm -fr '${cacheFolder}'
mkdir -p '${cacheFolder}'
pushd '${cacheFolder}' > /dev/null
${collectCache}
popd > /dev/null
# Store the absolute path to Yarn for the 'yarn' alias.
export NIX_YARN_PATH="$(readlink -f '${yarn-path}')"
export NIX_YARN_PATH="$(readlink -f '${yarnPath}')"
# Run normal Yarn install to complete dependency installation.
yarn install --immutable --immutable-cache
@@ -108,15 +86,11 @@ in stdenv.mkDerivation {
cd "$out/libexec/$sourceRoot"
# Update the path to Yarn.
export NIX_YARN_PATH="$(readlink -f '${yarn-path}')"
export NIX_YARN_PATH="$(readlink -f '${yarnPath}')"
# Invoke a plugin internal command to setup binaries.
yarn nixify install-bin $out/bin
runHook postInstall
'';
passthru = {
inherit offline-cache;
};
}