summaryrefslogtreecommitdiff
path: root/pkgs/build-support/fetchrepoproject/default.nix
blob: 5f10c7eebf45c66811705cf7fc92b8876f227de4 (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
{
  lib,
  stdenvNoCC,
  gitRepo,
  cacert,
  copyPathsToStore,
}:
lib.fetchers.withNormalizedHash { } (
  {
    name,
    manifest,
    rev ? "HEAD",
    outputHash,
    outputHashAlgo,
    # Optional parameters:
    repoRepoURL ? "",
    repoRepoRev ? "",
    referenceDir ? "",
    manifestName ? "",
    localManifests ? [ ],
    createMirror ? false,
    useArchive ? false,
  }:

  assert repoRepoRev != "" -> repoRepoURL != "";
  assert createMirror -> !useArchive;

  let
    inherit (lib)
      concatMapStringsSep
      concatStringsSep
      fetchers
      optionalString
      ;

    extraRepoInitFlags = [
      (optionalString (repoRepoURL != "") "--repo-url=${repoRepoURL}")
      (optionalString (repoRepoRev != "") "--repo-branch=${repoRepoRev}")
      (optionalString (referenceDir != "") "--reference=${referenceDir}")
      (optionalString (manifestName != "") "--manifest-name=${manifestName}")
    ];

    repoInitFlags = [
      "--manifest-url=${manifest}"
      "--manifest-branch=${rev}"
      "--depth=1"
      (optionalString createMirror "--mirror")
      (optionalString useArchive "--archive")
    ]
    ++ extraRepoInitFlags;

    local_manifests = copyPathsToStore localManifests;

  in
  stdenvNoCC.mkDerivation {
    inherit name;

    inherit
      cacert
      manifest
      rev
      repoRepoURL
      repoRepoRev
      referenceDir
      ; # TODO

    inherit outputHash outputHashAlgo;
    outputHashMode = "recursive";

    preferLocalBuild = true;
    enableParallelBuilding = true;

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

    nativeBuildInputs = [
      gitRepo
      cacert
    ];

    GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt";

    buildCommand = ''
      # Path must be absolute (e.g. for GnuPG: ~/.repoconfig/gnupg/pubring.kbx)
      export HOME="$(pwd)"

      mkdir $out
      cd $out

      mkdir .repo
      ${optionalString (local_manifests != [ ]) ''
        mkdir .repo/local_manifests
        for local_manifest in ${concatMapStringsSep " " toString local_manifests}; do
          cp $local_manifest .repo/local_manifests/$(stripHash $local_manifest)
        done
      ''}

      repo init ${concatStringsSep " " repoInitFlags}
      repo sync --jobs=$NIX_BUILD_CORES --current-branch

      # TODO: The git-index files (and probably the files in .repo as well) have
      # different contents each time and will therefore change the final hash
      # (i.e. creating a mirror probably won't work).
      ${optionalString (!createMirror) ''
        rm -rf .repo
        find -type d -name '.git' -prune -exec rm -rf {} +
      ''}
    '';
  }
)