Skip to content

Commit

Permalink
buildYarnPackage: Add support for packageOverrides
Browse files Browse the repository at this point in the history
This is similar to b5cd694, which added
the attribute to mkNodeModule and buildNpmPackage but didn't do it for
the buildYarnPackage flavour.

However, we're using lib.extends instead of a plain attribute set, since
we want to tread the original package set as a fixed point to be
compasable with multiple levels of overrides, similar to how it's done
with nixpkgs overlays.

In my case it's to quickly override a dependency fetched from GitHub,
eg. like this:

  let
    mydep = "https://github.com/someowner/somerepo.git";

    newMydep = pkgs.fetchFromGitHub {
      owner = "someowner";
      repo = "somerepo";
      rev = "...";
      sha256 = "...";
      fetchSubmodules = true;
      leaveDotGit = true;
    };

  in buildYarnPackage {
    # ...
    nativeBuildInputs = lib.singleton (pkgs.writeScriptBin "git" ''
      #!${pkgs.python3.interpreter}
      import sys, os
      for n, arg in enumerate(sys.argv):
        if arg == '${mydep}':
          sys.argv[n] = '${newMydep}'
      os.execv('${pkgs.git}/bin/git', sys.argv)
    '');
    packageOverrides = lib.singleton (self: super: {
      ${mydep} = super.${mydep} // { path = "dummy"; };
    });
    # ...
  }

I know that the way I've done the above is probably winning the first
price for "the ugliest override" but since I'm not particularly invested
in anything related to node.js, I'm usually pretty cruel when it comes
to it.

So the above example is just to have a complete almost-real-world
example for reference and in this case it's to work around serokell#7.

Signed-off-by: aszlig <[email protected]>
  • Loading branch information
aszlig committed Dec 10, 2020
1 parent bbe7d01 commit 1725fa1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ with stdenv.lib; let
depToFetch = args @ { resolved ? null, dependencies ? {}, ... }:
(optional (resolved != null) (depFetchOwn args)) ++ (depsToFetches dependencies);

# TODO: Make the override semantics similar to yarnCacheInput and
# deduplicate.
cacheInput = oFile: iFile: overrides:
writeText oFile (toJSON ((listToAttrs (depToFetch iFile))
// (builtins.mapAttrs (_: overrideToFetch) overrides)));

yarnCacheInput = oFile: iFile: overrides: let
self = listToAttrs (depToFetch iFile);
final = fix (foldl' (flip extends) (const self) overrides);
in writeText oFile (toJSON final);

patchShebangs = writeShellScriptBin "patchShebangs.sh" ''
set -e
source ${stdenv}/setup
Expand Down Expand Up @@ -226,7 +233,7 @@ in rec {

buildYarnPackage = args @ {
src, yarnBuild ? "yarn", yarnBuildMore ? "", integreties ? {},
buildInputs ? [], yarnFlags ? [], npmFlags ? [], ...
packageOverrides ? [], buildInputs ? [], yarnFlags ? [], npmFlags ? [], ...
}:
let
info = npmInfo src;
Expand Down Expand Up @@ -258,7 +265,7 @@ in rec {

yarnCachePhase = ''
mkdir -p yarn-cache
node ${./mkyarncache.js} ${cacheInput "yarn-cache-input.json" deps {}}
node ${./mkyarncache.js} ${yarnCacheInput "yarn-cache-input.json" deps packageOverrides}
'';

buildPhase = ''
Expand All @@ -281,7 +288,7 @@ in rec {
${untarAndWrap info.name [npmCmd yarnCmd]}
runHook postInstall
'';
} // commonEnv // removeAttrs args [ "integreties" ] // {
} // commonEnv // removeAttrs args [ "integreties" "packageOverrides" ] // {
buildInputs = [ _yarn ] ++ commonBuildInputs ++ buildInputs;
yarnFlags = [ "--offline" "--frozen-lockfile" "--non-interactive" ] ++ yarnFlags;
npmFlags = npmFlagsYarn ++ npmFlags;
Expand Down

0 comments on commit 1725fa1

Please sign in to comment.