Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib, stdenvNoCC, rustPlatform, makeWrapper, Security, gnutar, gzip, nix, testers, fetchurl, prefetch-npm-deps, fetchNpmDeps }:
2
3{
4 prefetch-npm-deps = rustPlatform.buildRustPackage {
5 pname = "prefetch-npm-deps";
6 version = (lib.importTOML ./Cargo.toml).package.version;
7
8 src = lib.cleanSourceWith {
9 src = ./.;
10 filter = name: type:
11 let
12 name' = builtins.baseNameOf name;
13 in
14 name' != "default.nix" && name' != "target";
15 };
16
17 cargoLock.lockFile = ./Cargo.lock;
18
19 nativeBuildInputs = [ makeWrapper ];
20 buildInputs = lib.optional stdenvNoCC.isDarwin Security;
21
22 postInstall = ''
23 wrapProgram "$out/bin/prefetch-npm-deps" --prefix PATH : ${lib.makeBinPath [ gnutar gzip nix ]}
24 '';
25
26 passthru.tests =
27 let
28 makeTestSrc = { name, src }: stdenvNoCC.mkDerivation {
29 name = "${name}-src";
30
31 inherit src;
32
33 buildCommand = ''
34 mkdir -p $out
35 cp $src $out/package-lock.json
36 '';
37 };
38
39 makeTest = { name, src, hash, forceGitDeps ? false }: testers.invalidateFetcherByDrvHash fetchNpmDeps {
40 inherit name hash forceGitDeps;
41
42 src = makeTestSrc { inherit name src; };
43 };
44 in
45 {
46 lockfileV1 = makeTest {
47 name = "lockfile-v1";
48
49 src = fetchurl {
50 url = "https://raw.githubusercontent.com/jellyfin/jellyfin-web/v10.8.4/package-lock.json";
51 hash = "sha256-uQmc+S+V1co1Rfc4d82PpeXjmd1UqdsG492ADQFcZGA=";
52 };
53
54 hash = "sha256-wca1QvxUw3OrLStfYN9Co6oVBR1LbfcNUKlDqvObps4=";
55 };
56
57 lockfileV2 = makeTest {
58 name = "lockfile-v2";
59
60 src = fetchurl {
61 url = "https://raw.githubusercontent.com/jesec/flood/v4.7.0/package-lock.json";
62 hash = "sha256-qS29tq5QPnGxV+PU40VgMAtdwVLtLyyhG2z9GMeYtC4=";
63 };
64
65 hash = "sha256-tuEfyePwlOy2/mOPdXbqJskO6IowvAP4DWg8xSZwbJw=";
66 };
67
68 hashPrecedence = makeTest {
69 name = "hash-precedence";
70
71 src = fetchurl {
72 url = "https://raw.githubusercontent.com/matrix-org/matrix-appservice-irc/0.34.0/package-lock.json";
73 hash = "sha256-1+0AQw9EmbHiMPA/H8OP8XenhrkhLRYBRhmd1cNPFjk=";
74 };
75
76 hash = "sha256-oItUls7AXcCECuyA+crQO6B0kv4toIr8pBubNwB7kAM=";
77 };
78
79 hostedGitDeps = makeTest {
80 name = "hosted-git-deps";
81
82 src = fetchurl {
83 url = "https://cyberchaos.dev/yuka/trainsearch/-/raw/e3cba6427e8ecfd843d0f697251ddaf5e53c2327/package-lock.json";
84 hash = "sha256-X9mCwPqV5yP0S2GonNvpYnLSLJMd/SUIked+hMRxDpA=";
85 };
86
87 hash = "sha256-tEdElWJ+KBTxBobzXBpPopQSwK2usGW/it1+yfbVzBw=";
88 };
89
90 linkDependencies = makeTest {
91 name = "link-dependencies";
92
93 src = fetchurl {
94 url = "https://raw.githubusercontent.com/evcc-io/evcc/0.106.3/package-lock.json";
95 hash = "sha256-6ZTBMyuyPP/63gpQugggHhKVup6OB4hZ2rmSvPJ0yEs=";
96 };
97
98 hash = "sha256-VzQhArHoznYSXUT7l9HkJV4yoSOmoP8eYTLel1QwmB4=";
99 };
100
101 # This package contains both hosted Git shorthand, and a bundled dependency that happens to override an existing one.
102 etherpadLite1818 = makeTest {
103 name = "etherpad-lite-1.8.18";
104
105 src = fetchurl {
106 url = "https://raw.githubusercontent.com/ether/etherpad-lite/1.8.18/src/package-lock.json";
107 hash = "sha256-1fGNxYJi1I4cXK/jinNG+Y6tPEOhP3QAqWOBEQttS9E=";
108 };
109
110 hash = "sha256-+KA8/orSBJ4EhuSyQO8IKSxsN/FAsYU3lOzq+awuxNQ=";
111
112 forceGitDeps = true;
113 };
114 };
115
116 meta = with lib; {
117 description = "Prefetch dependencies from npm (for use with `fetchNpmDeps`)";
118 maintainers = with maintainers; [ winter ];
119 license = licenses.mit;
120 };
121 };
122
123 fetchNpmDeps =
124 { name ? "npm-deps"
125 , hash ? ""
126 , forceGitDeps ? false
127 , ...
128 } @ args:
129 let
130 hash_ =
131 if hash != "" then {
132 outputHash = hash;
133 } else {
134 outputHash = "";
135 outputHashAlgo = "sha256";
136 };
137
138 forceGitDeps_ = lib.optionalAttrs forceGitDeps { FORCE_GIT_DEPS = true; };
139 in
140 stdenvNoCC.mkDerivation (args // {
141 inherit name;
142
143 nativeBuildInputs = [ prefetch-npm-deps ];
144
145 buildPhase = ''
146 runHook preBuild
147
148 if [[ ! -e package-lock.json ]]; then
149 echo
150 echo "ERROR: The package-lock.json file does not exist!"
151 echo
152 echo "package-lock.json is required to make sure that npmDepsHash doesn't change"
153 echo "when packages are updated on npm."
154 echo
155 echo "Hint: You can copy a vendored package-lock.json file via postPatch."
156 echo
157
158 exit 1
159 fi
160
161 prefetch-npm-deps package-lock.json $out
162
163 runHook postBuild
164 '';
165
166 dontInstall = true;
167
168 outputHashMode = "recursive";
169 } // hash_ // forceGitDeps_);
170}