about summary refs log tree commit diff
path: root/pkgs/development/tools/pnpm/fetch-deps/default.nix
blob: eb8b847960c5311ab492fdd245eb688d1cec3bad (plain) (blame)
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
{
  lib,
  stdenvNoCC,
  callPackage,
  jq,
  moreutils,
  cacert,
  makeSetupHook,
  pnpm,
  yq,
}:
{
  fetchDeps =
    {
      hash ? "",
      pname,
      ...
    }@args:
    let
      args' = builtins.removeAttrs args [
        "hash"
        "pname"
      ];
      hash' =
        if hash != "" then
          { outputHash = hash; }
        else
          {
            outputHash = "";
            outputHashAlgo = "sha256";
          };
    in
    stdenvNoCC.mkDerivation (finalAttrs: (
      args'
      // {
        name = "${pname}-pnpm-deps";

        nativeBuildInputs = [
          cacert
          jq
          moreutils
          pnpm
          yq
        ];

        installPhase = ''
          runHook preInstall

          lockfileVersion="$(yq -r .lockfileVersion pnpm-lock.yaml)"
          if [[ ''${lockfileVersion:0:1} -gt ${lib.versions.major pnpm.version} ]]; then
            echo "ERROR: lockfileVersion $lockfileVersion in pnpm-lock.yaml is too new for the provided pnpm version ${lib.versions.major pnpm.version}!"
            exit 1
          fi

          export HOME=$(mktemp -d)
          pnpm config set store-dir $out
          # Some packages produce platform dependent outputs. We do not want to cache those in the global store
          pnpm config set side-effects-cache false
          # As we pin pnpm versions, we don't really care about updates
          pnpm config set update-notifier false
          # pnpm is going to warn us about using --force
          # --force allows us to fetch all dependencies including ones that aren't meant for our host platform
          pnpm install --frozen-lockfile --ignore-script --force

          runHook postInstall
        '';

        fixupPhase = ''
          runHook preFixup

          # Remove timestamp and sort the json files
          rm -rf $out/v3/tmp
          for f in $(find $out -name "*.json"); do
            jq --sort-keys "del(.. | .checkedAt?)" $f | sponge $f
          done

          runHook postFixup
        '';

        passthru = {
          serve = callPackage ./serve.nix {
            inherit pnpm;
            pnpmDeps = finalAttrs.finalPackage;
          };
        };

        dontConfigure = true;
        dontBuild = true;
        outputHashMode = "recursive";
      }
      // hash'
    ));

  configHook = makeSetupHook {
    name = "pnpm-config-hook";
    propagatedBuildInputs = [ pnpm ];
  } ./pnpm-config-hook.sh;
}