summaryrefslogtreecommitdiff
path: root/pkgs/development/tools/pnpm/generic.nix
blob: aa50a4fd3a52aed2bedc71507d4bc882d5a8e12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
  lib,
  stdenvNoCC,
  writeScript,
  fetchPnpmDeps,
  pnpmConfigHook,
  fetchurl,
  installShellFiles,
  nodejs,
  testers,
  buildPackages,
  bashNonInteractive,
  tests,

  withNode ? true,
  version,
  hash,
}:
let
  majorVersion = lib.versions.major version;
in
stdenvNoCC.mkDerivation (finalAttrs: {
  pname = "pnpm";
  inherit version;

  src = fetchurl {
    url = "https://registry.npmjs.org/pnpm/-/pnpm-${finalAttrs.version}.tgz";
    inherit hash;
  };

  nativeBuildInputs = [
    installShellFiles
    nodejs
  ];

  buildInputs = [
    bashNonInteractive # needed for node-gyp wrapper script
  ]
  ++ lib.optionals withNode [ nodejs ];

  # Remove binary files from src, we don't need them, and this way we make sure
  # our distribution is free of binaryNativeCode
  postUnpack = ''
    rm -r package/dist/reflink.*node package/dist/vendor
  '';

  installPhase =
    let
      # Use ESM pnpm for versions > 11
      ext = if lib.versionOlder finalAttrs.version "11" then "cjs" else "mjs";
    in
    ''
      runHook preInstall

      install -d $out/{bin,libexec}
      cp -R . $out/libexec/pnpm
      ln -s $out/libexec/pnpm/bin/pnpm.${ext} $out/bin/pnpm
      ln -s $out/libexec/pnpm/bin/pnpx.${ext} $out/bin/pnpx

      runHook postInstall
    '';

  postInstall =
    if lib.toInt (lib.versions.major version) < 9 then
      ''
        export HOME="$PWD"
        node $out/bin/pnpm install-completion bash
        node $out/bin/pnpm install-completion fish
        node $out/bin/pnpm install-completion zsh
        sed -i '1 i#compdef pnpm' .config/tabtab/zsh/pnpm.zsh
        installShellCompletion \
          .config/tabtab/bash/pnpm.bash \
          .config/tabtab/fish/pnpm.fish \
          .config/tabtab/zsh/pnpm.zsh
      ''
    else
      ''
        node $out/bin/pnpm completion bash >pnpm.bash
        node $out/bin/pnpm completion fish >pnpm.fish
        node $out/bin/pnpm completion zsh >pnpm.zsh
        sed -i '1 i#compdef pnpm' pnpm.zsh
        installShellCompletion pnpm.{bash,fish,zsh}
      '';

  passthru =
    let
      pnpm' = buildPackages."pnpm_${lib.versions.major version}";
    in
    {
      fetchDeps =
        lib.warn
          "pnpm.fetchDeps: The package attribute is deprecated. Use the top-level fetchPnpmDeps attribute instead"
          (
            { ... }@args:
            fetchPnpmDeps (
              args
              // {
                pnpm = pnpm';
              }
            )
          );
      configHook =
        lib.warn
          "pnpm.configHook: The package attribute is deprecated. Use the top-level pnpmConfigHook attribute instead"
          (
            pnpmConfigHook.overrideAttrs (prevAttrs: {
              propagatedBuildInputs = prevAttrs.propagatedBuildInputs or [ ] ++ [
                pnpm'
              ];
            })
          );
      inherit nodejs majorVersion;

      tests = {
        inherit (tests) pnpm;
        version = lib.optionalAttrs withNode (testers.testVersion { package = finalAttrs.finalPackage; });
      };
      updateScript = writeScript "pnpm-update-script" ''
        #!/usr/bin/env nix-shell
        #!nix-shell -i bash -p curl jq common-updater-scripts
        set -eou pipefail

        curl_github() {
            curl -L ''${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} "$@"
        }

        latestTag=$(
          curl_github https://api.github.com/repos/pnpm/pnpm/releases?per_page=100 | \
          jq -r --arg major "v${majorVersion}" \
            '[.[] | select(.tag_name | startswith($major)) | select(.prerelease == false)][0].tag_name'
        )

        # Exit if there is no tag with this major version
        if [ "$latestTag" = "null" ]; then
          echo "No releases starting with v${majorVersion}"
          exit 0
        fi

        latestVersion="''${latestTag#v}"

        update-source-version pnpm_${majorVersion} "$latestVersion" --file=./pkgs/development/tools/pnpm/default.nix
      '';
    };

  strictDeps = true;
  __structuredAttrs = true;

  dontBuild = true;
  dontConfigure = true;

  meta = {
    description = "Fast, disk space efficient package manager for JavaScript";
    homepage = "https://pnpm.io/";
    changelog = "https://github.com/pnpm/pnpm/releases/tag/v${finalAttrs.version}";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [
      Scrumplex
      gepbird
    ];
    platforms = lib.platforms.all;
    mainProgram = "pnpm";
  };
})