From 708e36eaf4ae45a8d5726bfa4b5ba3eb33707a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Tue, 25 Aug 2020 18:20:16 +0200 Subject: [PATCH] Cleanup of yarn-project.nix --- README.md | 20 +++++----- src/default.nix.in | 4 +- src/index.ts | 8 +++- src/yarn-project.nix.in | 86 ++++++++++++++--------------------------- 4 files changed, 49 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 70e3ac3..7814dd4 100644 --- a/README.md +++ b/README.md @@ -18,24 +18,24 @@ The `yarn nixify` command always updates `yarn-project.nix`, but only writes a to be customized, for example: ```nix -{ pkgs ? import {} }: +{ pkgs ? import { } }: let - project = pkgs.callPackage ./yarn-project.nix { + # Example of providing a different source. + src = fetchFromGitHub { + owner = "johndoe"; + repo = "myproject"; + rev = "v1.0.0"; + sha256 = "1hdhafj726g45gh7nj8qv1xls8mps3vhzq3aasdymbdqcb1clhkz"; + }; - # Example of providing a different source. - src = fetchFromGitHub { - owner = "johndoe"; - repo = "myproject"; - rev = "v1.0.0"; - sha256 = "1hdhafj726g45gh7nj8qv1xls8mps3vhzq3aasdymbdqcb1clhkz"; - }; + project = pkgs.callPackage ./yarn-project.nix { # Example of selecting a specific version of Node.js. nodejs = pkgs.nodejs-14_x; - }; + } src; in project.overrideAttrs (oldAttrs: { diff --git a/src/default.nix.in b/src/default.nix.in index 046e40c..fa07aa2 100644 --- a/src/default.nix.in +++ b/src/default.nix.in @@ -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 {} }: +{ pkgs ? import { } }: -pkgs.callPackage ./yarn-project.nix {} +pkgs.callPackage ./yarn-project.nix { } ./. diff --git a/src/index.ts b/src/index.ts index 6b9687d..148c535 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)};`, ]); } diff --git a/src/yarn-project.nix.in b/src/yarn-project.nix.in index 33ebd41..d34c87b 100644 --- a/src/yarn-project.nix.in +++ b/src/yarn-project.nix.in @@ -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; - }; }