1 { lib, stdenvNoCC, rustPlatform, makeWrapper, pkg-config, curl, gnutar, gzip, testers, fetchurl, cacert, prefetch-npm-deps, fetchNpmDeps }:
4 prefetch-npm-deps = rustPlatform.buildRustPackage {
5 pname = "prefetch-npm-deps";
6 version = (lib.importTOML ./Cargo.toml).package.version;
8 src = lib.cleanSourceWith {
12 name' = builtins.baseNameOf name;
14 name' != "default.nix" && name' != "target";
17 cargoLock.lockFile = ./Cargo.lock;
19 nativeBuildInputs = [ makeWrapper pkg-config ];
20 buildInputs = [ curl ];
23 wrapProgram "$out/bin/prefetch-npm-deps" --prefix PATH : ${lib.makeBinPath [ gnutar gzip ]}
28 makeTestSrc = { name, src }: stdenvNoCC.mkDerivation {
35 cp $src $out/package-lock.json
39 makeTest = { name, src, hash, forceGitDeps ? false, forceEmptyCache ? false }: testers.invalidateFetcherByDrvHash fetchNpmDeps {
40 inherit name hash forceGitDeps forceEmptyCache;
42 src = makeTestSrc { inherit name src; };
46 lockfileV1 = makeTest {
50 url = "https://raw.githubusercontent.com/jellyfin/jellyfin-web/v10.8.4/package-lock.json";
51 hash = "sha256-uQmc+S+V1co1Rfc4d82PpeXjmd1UqdsG492ADQFcZGA=";
54 hash = "sha256-wca1QvxUw3OrLStfYN9Co6oVBR1LbfcNUKlDqvObps4=";
57 lockfileV2 = makeTest {
61 url = "https://raw.githubusercontent.com/jesec/flood/v4.7.0/package-lock.json";
62 hash = "sha256-qS29tq5QPnGxV+PU40VgMAtdwVLtLyyhG2z9GMeYtC4=";
65 hash = "sha256-tuEfyePwlOy2/mOPdXbqJskO6IowvAP4DWg8xSZwbJw=";
68 hashPrecedence = makeTest {
69 name = "hash-precedence";
72 url = "https://raw.githubusercontent.com/matrix-org/matrix-appservice-irc/0.34.0/package-lock.json";
73 hash = "sha256-1+0AQw9EmbHiMPA/H8OP8XenhrkhLRYBRhmd1cNPFjk=";
76 hash = "sha256-oItUls7AXcCECuyA+crQO6B0kv4toIr8pBubNwB7kAM=";
79 hostedGitDeps = makeTest {
80 name = "hosted-git-deps";
83 url = "https://cyberchaos.dev/yuka/trainsearch/-/raw/e3cba6427e8ecfd843d0f697251ddaf5e53c2327/package-lock.json";
84 hash = "sha256-X9mCwPqV5yP0S2GonNvpYnLSLJMd/SUIked+hMRxDpA=";
87 hash = "sha256-tEdElWJ+KBTxBobzXBpPopQSwK2usGW/it1+yfbVzBw=";
90 linkDependencies = makeTest {
91 name = "link-dependencies";
94 url = "https://raw.githubusercontent.com/evcc-io/evcc/0.106.3/package-lock.json";
95 hash = "sha256-6ZTBMyuyPP/63gpQugggHhKVup6OB4hZ2rmSvPJ0yEs=";
98 hash = "sha256-VzQhArHoznYSXUT7l9HkJV4yoSOmoP8eYTLel1QwmB4=";
101 # This package has no resolved deps whatsoever, which will not actually work but does test the forceEmptyCache option.
102 emptyCache = makeTest {
103 name = "empty-cache";
106 url = "https://raw.githubusercontent.com/bufbuild/protobuf-es/v1.2.1/package-lock.json";
107 hash = "sha256-UdBUEb4YRHsbvyjymIyjemJEiaI9KQRirqt+SFSK0wA=";
110 hash = "sha256-Cdv40lQjRszzJtJydZt25uYfcJVeJGwH54A+agdH9wI=";
112 forceEmptyCache = true;
115 # This package contains both hosted Git shorthand, and a bundled dependency that happens to override an existing one.
116 etherpadLite1818 = makeTest {
117 name = "etherpad-lite-1.8.18";
120 url = "https://raw.githubusercontent.com/ether/etherpad-lite/1.8.18/src/package-lock.json";
121 hash = "sha256-1fGNxYJi1I4cXK/jinNG+Y6tPEOhP3QAqWOBEQttS9E=";
124 hash = "sha256-+KA8/orSBJ4EhuSyQO8IKSxsN/FAsYU3lOzq+awuxNQ=";
129 # This package has a lockfile v1 git dependency with no `dependencies` attribute, since it sementically has no dependencies.
130 jitsiMeet9111 = makeTest {
131 name = "jitsi-meet-9111";
134 url = "https://raw.githubusercontent.com/jitsi/jitsi-meet/stable/jitsi-meet_9111/package-lock.json";
135 hash = "sha256-NU+eQD4WZ4BMur8uX79uk8wUPsZvIT02KhPWHTmaihk=";
138 hash = "sha256-FhxlJ0HdJMPiWe7+n1HaGLWOr/2HJEPwiS65uqXZM8Y=";
143 description = "Prefetch dependencies from npm (for use with `fetchNpmDeps`)";
144 mainProgram = "prefetch-npm-deps";
145 maintainers = with maintainers; [ lilyinstarlight winter ];
146 license = licenses.mit;
153 , forceGitDeps ? false
154 , forceEmptyCache ? false
163 outputHashAlgo = "sha256";
166 forceGitDeps_ = lib.optionalAttrs forceGitDeps { FORCE_GIT_DEPS = true; };
167 forceEmptyCache_ = lib.optionalAttrs forceEmptyCache { FORCE_EMPTY_CACHE = true; };
169 stdenvNoCC.mkDerivation (args // {
172 nativeBuildInputs = [ prefetch-npm-deps ];
177 if [[ ! -e package-lock.json ]]; then
179 echo "ERROR: The package-lock.json file does not exist!"
181 echo "package-lock.json is required to make sure that npmDepsHash doesn't change"
182 echo "when packages are updated on npm."
184 echo "Hint: You can copy a vendored package-lock.json file via postPatch."
190 prefetch-npm-deps package-lock.json $out
197 # NIX_NPM_TOKENS environment variable should be a JSON mapping in the shape of:
198 # `{ "registry.example.com": "example-registry-bearer-token", ... }`
199 impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_NPM_TOKENS" ];
201 SSL_CERT_FILE = if (hash_.outputHash == "" || hash_.outputHash == lib.fakeSha256 || hash_.outputHash == lib.fakeSha512 || hash_.outputHash == lib.fakeHash)
202 then "${cacert}/etc/ssl/certs/ca-bundle.crt"
203 else "/no-cert-file.crt";
205 outputHashMode = "recursive";
206 } // hash_ // forceGitDeps_ // forceEmptyCache_);