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
+3 -3
View File
@@ -22,8 +22,6 @@ to be customized, for example:
let let
project = pkgs.callPackage ./yarn-project.nix {
# Example of providing a different source. # Example of providing a different source.
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "johndoe"; owner = "johndoe";
@@ -32,10 +30,12 @@ let
sha256 = "1hdhafj726g45gh7nj8qv1xls8mps3vhzq3aasdymbdqcb1clhkz"; sha256 = "1hdhafj726g45gh7nj8qv1xls8mps3vhzq3aasdymbdqcb1clhkz";
}; };
project = pkgs.callPackage ./yarn-project.nix {
# Example of selecting a specific version of Node.js. # Example of selecting a specific version of Node.js.
nodejs = pkgs.nodejs-14_x; nodejs = pkgs.nodejs-14_x;
}; } src;
in project.overrideAttrs (oldAttrs: { in project.overrideAttrs (oldAttrs: {
+1 -1
View File
@@ -3,4 +3,4 @@
{ 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); const filename = ppath.basename(cachePath);
if (!cacheFiles.has(filename)) continue; if (!cacheFiles.has(filename)) continue;
let name = structUtils.slugifyIdent(pkg).replace(/^@/, "_at_");
if (pkg.version) {
name += `-${pkg.version}`;
}
const sha512 = checksum.split(`/`).pop(); const sha512 = checksum.split(`/`).pop();
cacheEntries.push([ cacheEntries.push([
`name = ${JSON.stringify(name)};`,
`filename = ${JSON.stringify(filename)};`, `filename = ${JSON.stringify(filename)};`,
`sha512 = ${JSON.stringify(sha512)};`, `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. # 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, coreutils, nodejs, stdenv, src ? ./. }: { lib, coreutils, nodejs, stdenv }: src:
with lib;
let let
# Variables provided by the generator. yarnPath = @@YARN_PATH@@;
project-name = @@PROJECT_NAME@@; cacheFolder = @@CACHE_FOLDER@@;
yarn-path = @@YARN_PATH@@; cacheEntries = @@CACHE_ENTRIES@@;
cache-folder = @@CACHE_FOLDER@@;
cache-entries = @@CACHE_ENTRIES@@;
# Defines the shell alias to run Yarn.
yarn-alias = ''
yarn() {
CI=1 node "$NIX_YARN_PATH" "$@"
}
'';
# Fetch a single dependency. # Fetch a single dependency.
fetch-one = { filename, sha512, locator-hash }: stdenv.mkDerivation { fetchOne = let
name = replaceStrings [ "@" ] [ "-" ] filename;
buildInputs = [ nodejs ];
builder = builtins.toFile "builder.sh" '' builder = builtins.toFile "builder.sh" ''
source $stdenv/setup source $stdenv/setup
cd "$src"
# Yarn may need a writable home directory for the global cache mirror. HOME="$TMP" yarn_cache_folder="$TMP" CI=1 \
# TODO: Can't disable the mirror, because it changes cache filenames. node '${yarnPath}' nixify fetch-one $locatorHash
export HOME="$PWD" mv "$TMP/$outputFilename" $out
# 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
''; '';
in { name, filename, sha512, locatorHash }: stdenv.mkDerivation {
inherit name src builder locatorHash;
buildInputs = [ nodejs ];
outputFilename = filename;
outputHashMode = "flat"; outputHashMode = "flat";
outputHashAlgo = "sha512"; outputHashAlgo = "sha512";
outputHash = sha512; outputHash = sha512;
}; };
# Shell snippet to collect all project dependencies. # Shell snippet to collect all project dependencies.
collect-cache = concatMapStrings (args: '' collectCache = lib.concatMapStrings (args: ''
cp ${fetch-one args} './${args.filename}' cp ${fetchOne args} '${args.filename}'
'') cache-entries; '') cacheEntries;
in stdenv.mkDerivation { in stdenv.mkDerivation {
name = project-name; name = @@PROJECT_NAME@@;
inherit src; inherit src;
# 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.
@@ -68,24 +42,28 @@ in stdenv.mkDerivation {
npm_config_build_from_source = "true"; npm_config_build_from_source = "true";
# Make sure the build uses the right Node.js version everywhere. # Make sure the build uses the right Node.js version everywhere.
buildInputs = [ coreutils nodejs ]; buildInputs = [ nodejs ];
# Define the Yarn alias in the build environment. # Defines the shell alias to run Yarn.
postHook = yarn-alias; postHook = ''
yarn() {
CI=1 node "$NIX_YARN_PATH" "$@"
}
'';
configurePhase = '' configurePhase = ''
runHook preConfigure runHook preConfigure
# Copy over the Yarn cache. # Copy over the Yarn cache.
# TODO: Can we do without the copy somehow? Links don't work. # TODO: Can we do without the copy somehow? Links don't work.
rm -fr '${cache-folder}' rm -fr '${cacheFolder}'
mkdir -p '${cache-folder}' mkdir -p '${cacheFolder}'
pushd '${cache-folder}' > /dev/null pushd '${cacheFolder}' > /dev/null
${collect-cache} ${collectCache}
popd > /dev/null popd > /dev/null
# Store the absolute path to Yarn for the 'yarn' alias. # 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. # Run normal Yarn install to complete dependency installation.
yarn install --immutable --immutable-cache yarn install --immutable --immutable-cache
@@ -108,15 +86,11 @@ in stdenv.mkDerivation {
cd "$out/libexec/$sourceRoot" cd "$out/libexec/$sourceRoot"
# Update the path to Yarn. # 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. # Invoke a plugin internal command to setup binaries.
yarn nixify install-bin $out/bin yarn nixify install-bin $out/bin
runHook postInstall runHook postInstall
''; '';
passthru = {
inherit offline-cache;
};
} }