@@ -0,0 +1,199 @@
1+{ stdenv, lib, fetchurl, linkFarm, runCommand, nodejs, yarn }:
2+
3+let
4+ unlessNull = item: alt:
5+ if item == null then alt else item;
6+
7+ yarn2nix = mkYarnPackage {
8+ src = ./.;
9+ yarnNix = ./yarn.nix;
10+
11+ passthru = {
12+ inherit
13+ defaultYarnFlags
14+ linkNodeModulesHook
15+ mkYarnModules
16+ mkYarnNix
17+ mkYarnPackage
18+ # Export yarn again to make it easier to find out which yarn was used.
19+ yarn
20+ ;
21+ };
22+
23+ meta = with lib; {
24+ description = "generate nix expressions from a yarn.lock file";
25+ homepage = "https://github.com/moretea/yarn2nix";
26+ license = licenses.gpl3;
27+ maintainers = with maintainers; [ manveru zimbatm ];
28+ };
29+ };
30+
31+ # Generates the yarn.nix from the yarn.lock file
32+ mkYarnNix = yarnLock:
33+ runCommand "yarn.nix" {}
34+ "${yarn2nix}/bin/yarn2nix --lockfile ${yarnLock} --no-patch > $out";
35+
36+ # Loads the generated offline cache. This will be used by yarn as
37+ # the package source.
38+ importOfflineCache = yarnNix:
39+ let
40+ pkg = import yarnNix { inherit fetchurl linkFarm; };
41+ in
42+ pkg.offline_cache;
43+
44+ defaultYarnFlags = [
45+ "--offline"
46+ "--frozen-lockfile"
47+ "--ignore-engines"
48+ "--ignore-scripts"
49+ ];
50+
51+ mkYarnModules = {
52+ name,
53+ packageJSON,
54+ yarnLock,
55+ yarnNix ? mkYarnNix yarnLock,
56+ yarnFlags ? defaultYarnFlags,
57+ pkgConfig ? {},
58+ preBuild ? "",
59+ }:
60+ let
61+ offlineCache = importOfflineCache yarnNix;
62+ extraBuildInputs = (lib.flatten (builtins.map (key:
63+ pkgConfig.${key} . buildInputs or []
64+ ) (builtins.attrNames pkgConfig)));
65+ postInstall = (builtins.map (key:
66+ if (pkgConfig.${key} ? postInstall) then
67+ ''
68+ for f in $(find -L -path '*/node_modules/${key}' -type d); do
69+ (cd "$f" && (${pkgConfig.${key}.postInstall}))
70+ done
71+ ''
72+ else
73+ ""
74+ ) (builtins.attrNames pkgConfig));
75+ in
76+ stdenv.mkDerivation {
77+ inherit name preBuild;
78+ phases = ["configurePhase" "buildPhase"];
79+ buildInputs = [ yarn nodejs ] ++ extraBuildInputs;
80+
81+ configurePhase = ''
82+ # Yarn writes cache directories etc to $HOME.
83+ export HOME=$PWD/yarn_home
84+ '';
85+
86+ buildPhase = ''
87+ runHook preBuild
88+
89+ cp ${packageJSON} ./package.json
90+ cp ${yarnLock} ./yarn.lock
91+ chmod +w ./yarn.lock
92+
93+ yarn config --offline set yarn-offline-mirror ${offlineCache}
94+
95+ # Do not look up in the registry, but in the offline cache.
96+ # TODO: Ask upstream to fix this mess.
97+ sed -i -E 's|^(\s*resolved\s*")https?://.*/|\1|' yarn.lock
98+ yarn install ${lib.escapeShellArgs yarnFlags}
99+
100+ ${lib.concatStringsSep "\n" postInstall}
101+
102+ mkdir $out
103+ mv node_modules $out/
104+ patchShebangs $out
105+ '';
106+ };
107+
108+ # This can be used as a shellHook in mkYarnPackage. It brings the built node_modules into
109+ # the shell-hook environment.
110+ linkNodeModulesHook = ''
111+ if [[ -d node_modules || -L node_modules ]]; then
112+ echo "./node_modules is present. Replacing."
113+ rm -rf node_modules
114+ fi
115+
116+ ln -s "$node_modules" node_modules
117+ '';
118+
119+ mkYarnPackage = {
120+ name ? null,
121+ src,
122+ packageJSON ? src + "/package.json",
123+ yarnLock ? src + "/yarn.lock",
124+ yarnNix ? mkYarnNix yarnLock,
125+ yarnFlags ? defaultYarnFlags,
126+ yarnPreBuild ? "",
127+ pkgConfig ? {},
128+ extraBuildInputs ? [],
129+ publishBinsFor ? null,
130+ ...
131+ }@attrs:
132+ let
133+ package = lib.importJSON packageJSON;
134+ pname = package.name;
135+ version = package.version;
136+ deps = mkYarnModules {
137+ name = "${pname}-modules-${version}";
138+ preBuild = yarnPreBuild;
139+ inherit packageJSON yarnLock yarnNix yarnFlags pkgConfig;
140+ };
141+ publishBinsFor_ = unlessNull publishBinsFor [pname];
142+ in stdenv.mkDerivation (builtins.removeAttrs attrs ["pkgConfig"] // {
143+ inherit src;
144+
145+ name = unlessNull name "${pname}-${version}";
146+
147+ buildInputs = [ yarn nodejs ] ++ extraBuildInputs;
148+
149+ node_modules = deps + "/node_modules";
150+
151+ configurePhase = attrs.configurePhase or ''
152+ runHook preConfigure
153+
154+ if [ -d npm-packages-offline-cache ]; then
155+ echo "npm-pacakges-offline-cache dir present. Removing."
156+ rm -rf npm-packages-offline-cache
157+ fi
158+
159+ if [[ -d node_modules || -L node_modules ]]; then
160+ echo "./node_modules is present. Removing."
161+ rm -rf node_modules
162+ fi
163+
164+ mkdir -p node_modules
165+ ln -s $node_modules/* node_modules/
166+ ln -s $node_modules/.bin node_modules/
167+
168+ if [ -d node_modules/${pname} ]; then
169+ echo "Error! There is already an ${pname} package in the top level node_modules dir!"
170+ exit 1
171+ fi
172+
173+ runHook postConfigure
174+ '';
175+
176+ # Replace this phase on frontend packages where only the generated
177+ # files are an interesting output.
178+ installPhase = attrs.installPhase or ''
179+ runHook preInstall
180+
181+ mkdir -p $out
182+ cp -r node_modules $out/node_modules
183+ cp -r . $out/node_modules/${pname}
184+ rm -rf $out/node_modules/${pname}/node_modules
185+
186+ mkdir $out/bin
187+ node ${./fixup_bin.js} $out ${lib.concatStringsSep " " publishBinsFor_}
188+
189+ runHook postInstall
190+ '';
191+
192+ passthru = {
193+ inherit package deps;
194+ } // (attrs.passthru or {});
195+
196+ # TODO: populate meta automatically
197+ });
198+in
199+ yarn2nix