summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/lake/fetch-lake-deps.nix
blob: af55dff26b8fec676fe6015008e347b96cc92122 (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
# fetchLakeDeps: fixed-output derivation that fetches Lake dependencies.
#
# Reads lake-manifest.json from the source tree, clones each git
# dependency at its pinned revision, and produces a directory of
# package sources.  The output is hash-verified via `lakeHash`.
#
# This follows the same pattern as buildGoModule's `goModules` FOD.
{
  lib,
  stdenvNoCC,
  gitMinimal,
  cacert,
  jq,
}:

lib.extendMkDerivation {
  constructDrv = stdenvNoCC.mkDerivation;

  excludeDrvArgNames = [
    "excludePackages"
  ];

  extendDrvArgs =
    finalAttrs:
    {
      pname,
      version,
      src,
      hash,
      sourceRoot ? "",
      patches ? [ ],
      prePatch ? "",
      postPatch ? "",
      # Package names to skip (e.g. already packaged in nix).
      excludePackages ? [ ],
    }:
    {
      strictDeps = true;
      __structuredAttrs = true;

      pname = "${pname}-lake-deps";

      nativeBuildInputs = [
        gitMinimal
        cacert
        jq
      ];

      impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
        "GIT_PROXY_COMMAND"
        "SOCKS_SERVER"
      ];

      dontConfigure = true;

      buildPhase = ''
        runHook preBuild

        if [ ! -f lake-manifest.json ]; then
          echo "fetchLakeDeps: lake-manifest.json not found" >&2
          exit 1
        fi

        export HOME="$TMPDIR"
        export GIT_SSL_CAINFO="$NIX_SSL_CERT_FILE"

        mkdir -p "$TMPDIR/packages"

        jq -c --argjson exclude ${lib.escapeShellArg (builtins.toJSON excludePackages)} \
          '.packages[] | select(.type == "git") | select(.name as $n | $exclude | index($n) | not)' \
          lake-manifest.json | while IFS= read -r pkg; do
          name=$(echo "$pkg" | jq -r '.name')
          url=$(echo "$pkg" | jq -r '.url')
          rev=$(echo "$pkg" | jq -r '.rev')

          echo "fetchLakeDeps: cloning $name ($url @ $rev)"

          git clone --filter=blob:none --no-checkout "$url" "$TMPDIR/packages/$name"
          git -C "$TMPDIR/packages/$name" checkout "$rev" --quiet

          # Remove .git to make output deterministic
          rm -rf "$TMPDIR/packages/$name/.git"
        done

        runHook postBuild
      '';

      installPhase = ''
        runHook preInstall
        mv "$TMPDIR/packages" "$out"
        runHook postInstall
      '';

      dontFixup = true;

      outputHashMode = "recursive";
      outputHash = hash;
      outputHashAlgo = if hash == "" then "sha256" else null;
    };
}