arduino-create-agent: init at 1.6.1 (#300694)
[NixPkgs.git] / pkgs / build-support / node / fetch-yarn-deps / default.nix
blob29a76d3d9f3e8b3e75b820ac34a16bfe8964b2bb
2   stdenv,
3   lib,
4   makeWrapper,
5   installShellFiles,
6   nodejsInstallManuals,
7   nodejsInstallExecutables,
8   coreutils,
9   nix-prefetch-git,
10   fetchurl,
11   jq,
12   nodejs,
13   nodejs-slim,
14   prefetch-yarn-deps,
15   fixup-yarn-lock,
16   yarn,
17   makeSetupHook,
18   cacert,
19   callPackage,
20   nix,
23 let
24   yarnpkg-lockfile-tar = fetchurl {
25     url = "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz";
26     hash = "sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==";
27   };
29   tests = callPackage ./tests { };
32   prefetch-yarn-deps = stdenv.mkDerivation {
33     name = "prefetch-yarn-deps";
35     dontUnpack = true;
36     dontBuild = true;
38     nativeBuildInputs = [ makeWrapper ];
39     buildInputs = [ nodejs-slim ];
41     installPhase = ''
42       runHook preInstall
44       mkdir -p $out/bin $out/libexec
46       tar --strip-components=1 -xf ${yarnpkg-lockfile-tar} package/index.js
47       mv index.js $out/libexec/yarnpkg-lockfile.js
48       cp ${./common.js} $out/libexec/common.js
49       cp ${./index.js} $out/libexec/index.js
51       patchShebangs $out/libexec
52       makeWrapper $out/libexec/index.js $out/bin/prefetch-yarn-deps \
53         --prefix PATH : ${
54           lib.makeBinPath [
55             coreutils
56             nix-prefetch-git
57             nix
58           ]
59         }
61       runHook postInstall
62     '';
64     passthru = {
65       inherit tests;
66     };
67   };
69   fixup-yarn-lock = stdenv.mkDerivation {
70     name = "fixup-yarn-lock";
72     dontUnpack = true;
73     dontBuild = true;
75     nativeBuildInputs = [ makeWrapper ];
76     buildInputs = [ nodejs-slim ];
78     installPhase = ''
79       runHook preInstall
81       mkdir -p $out/bin $out/libexec
83       tar --strip-components=1 -xf ${yarnpkg-lockfile-tar} package/index.js
84       mv index.js $out/libexec/yarnpkg-lockfile.js
85       cp ${./common.js} $out/libexec/common.js
86       cp ${./fixup.js} $out/libexec/fixup.js
88       patchShebangs $out/libexec
89       makeWrapper $out/libexec/fixup.js $out/bin/fixup-yarn-lock
91       runHook postInstall
92     '';
94     passthru = {
95       inherit tests;
96     };
97   };
99   fetchYarnDeps =
100     let
101       f =
102         {
103           name ? "offline",
104           src ? null,
105           hash ? "",
106           sha256 ? "",
107           ...
108         }@args:
109         let
110           hash_ =
111             if hash != "" then
112               {
113                 outputHashAlgo = null;
114                 outputHash = hash;
115               }
116             else if sha256 != "" then
117               {
118                 outputHashAlgo = "sha256";
119                 outputHash = sha256;
120               }
121             else
122               {
123                 outputHashAlgo = "sha256";
124                 outputHash = lib.fakeSha256;
125               };
126         in
127         stdenv.mkDerivation (
128           {
129             inherit name;
131             dontUnpack = src == null;
132             dontInstall = true;
134             nativeBuildInputs = [
135               prefetch-yarn-deps
136               cacert
137             ];
138             GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt";
139             NODE_EXTRA_CA_CERTS = "${cacert}/etc/ssl/certs/ca-bundle.crt";
141             buildPhase = ''
142               runHook preBuild
144               yarnLock=''${yarnLock:=$PWD/yarn.lock}
145               mkdir -p $out
146               (cd $out; prefetch-yarn-deps --verbose --builder $yarnLock)
148               runHook postBuild
149             '';
151             outputHashMode = "recursive";
152           }
153           // hash_
154           // (removeAttrs args (
155             [
156               "name"
157               "hash"
158               "sha256"
159             ]
160             ++ (lib.optional (src == null) "src")
161           ))
162         );
163     in
164     lib.setFunctionArgs f (lib.functionArgs f) // { inherit tests; };
166   yarnConfigHook = makeSetupHook {
167     name = "yarn-config-hook";
168     propagatedBuildInputs = [
169       yarn
170       fixup-yarn-lock
171     ];
172     meta = {
173       description = "Install nodejs dependencies from an offline yarn cache produced by fetchYarnDeps";
174     };
175   } ./yarn-config-hook.sh;
177   yarnBuildHook = makeSetupHook {
178     name = "yarn-build-hook";
179     meta = {
180       description = "Run yarn build in buildPhase";
181     };
182   } ./yarn-build-hook.sh;
184   yarnInstallHook = makeSetupHook {
185     name = "yarn-install-hook";
186     propagatedBuildInputs = [
187       yarn
188       nodejsInstallManuals
189       nodejsInstallExecutables
190     ];
191     substitutions = {
192       jq = lib.getExe jq;
193     };
194   } ./yarn-install-hook.sh;