1{
2 lib,
3 stdenvNoCC,
4 writeScript,
5 callPackages,
6 fetchurl,
7 installShellFiles,
8 nodejs,
9 testers,
10 withNode ? true,
11 version,
12 hash,
13 buildPackages,
14}:
15let
16 majorVersion = lib.versions.major version;
17in
18stdenvNoCC.mkDerivation (finalAttrs: {
19 pname = "pnpm";
20 inherit version;
21
22 src = fetchurl {
23 url = "https://registry.npmjs.org/pnpm/-/pnpm-${finalAttrs.version}.tgz";
24 inherit hash;
25 };
26 # Remove binary files from src, we don't need them, and this way we make sure
27 # our distribution is free of binaryNativeCode
28 preConfigure = ''
29 rm -r dist/reflink.*node dist/vendor
30 '';
31
32 buildInputs = lib.optionals withNode [ nodejs ];
33
34 nativeBuildInputs = [
35 installShellFiles
36 nodejs
37 ];
38
39 installPhase = ''
40 runHook preInstall
41
42 install -d $out/{bin,libexec}
43 cp -R . $out/libexec/pnpm
44 ln -s $out/libexec/pnpm/bin/pnpm.cjs $out/bin/pnpm
45 ln -s $out/libexec/pnpm/bin/pnpx.cjs $out/bin/pnpx
46
47 runHook postInstall
48 '';
49
50 postInstall =
51 if lib.toInt (lib.versions.major version) < 9 then
52 ''
53 export HOME="$PWD"
54 node $out/bin/pnpm install-completion bash
55 node $out/bin/pnpm install-completion fish
56 node $out/bin/pnpm install-completion zsh
57 sed -i '1 i#compdef pnpm' .config/tabtab/zsh/pnpm.zsh
58 installShellCompletion \
59 .config/tabtab/bash/pnpm.bash \
60 .config/tabtab/fish/pnpm.fish \
61 .config/tabtab/zsh/pnpm.zsh
62 ''
63 else
64 ''
65 node $out/bin/pnpm completion bash >pnpm.bash
66 node $out/bin/pnpm completion fish >pnpm.fish
67 node $out/bin/pnpm completion zsh >pnpm.zsh
68 sed -i '1 i#compdef pnpm' pnpm.zsh
69 installShellCompletion pnpm.{bash,fish,zsh}
70 '';
71
72 passthru =
73 let
74 fetchDepsAttrs = callPackages ./fetch-deps {
75 pnpm = buildPackages."pnpm_${lib.versions.major version}";
76 };
77 in
78 {
79 inherit (fetchDepsAttrs) fetchDeps configHook;
80 inherit majorVersion;
81
82 tests.version = lib.optionalAttrs withNode (
83 testers.testVersion { package = finalAttrs.finalPackage; }
84 );
85 updateScript = writeScript "pnpm-update-script" ''
86 #!/usr/bin/env nix-shell
87 #!nix-shell -i bash -p curl jq common-updater-scripts
88 set -eou pipefail
89
90 curl_github() {
91 curl -L ''${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} "$@"
92 }
93
94 latestTag=$(curl_github https://api.github.com/repos/pnpm/pnpm/releases?per_page=100 | jq -r --arg major "v${majorVersion}" '[.[].tag_name | select(startswith($major))][0]')
95
96 # Exit if there is no tag with this major version
97 if [ "$latestTag" = "null" ]; then
98 echo "No releases starting with v${majorVersion}"
99 exit 0
100 fi
101
102 latestVersion="''${latestTag#v}"
103
104 update-source-version pnpm_${majorVersion} "$latestVersion" --file=./pkgs/development/tools/pnpm/default.nix
105 '';
106 };
107
108 meta = {
109 description = "Fast, disk space efficient package manager for JavaScript";
110 homepage = "https://pnpm.io/";
111 changelog = "https://github.com/pnpm/pnpm/releases/tag/v${finalAttrs.version}";
112 license = lib.licenses.mit;
113 maintainers = with lib.maintainers; [
114 Scrumplex
115 gepbird
116 ];
117 platforms = lib.platforms.all;
118 mainProgram = "pnpm";
119 };
120})