summaryrefslogtreecommitdiff
path: root/pkgs/build-support/fetchdocker/default.nix
blob: d851f982086dea6e8132b2051e62c9850213065b (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
{
  stdenv,
  lib,
  coreutils,
  bash,
  gnutar,
  writeText,
}:
let
  stripScheme = builtins.replaceStrings [ "https://" "http://" ] [ "" "" ];
  stripNixStore = s: lib.removePrefix "${builtins.storeDir}/" s;
in
{
  name,
  registry ? "https://registry-1.docker.io/v2/",
  repository ? "library",
  imageName,
  tag,
  imageLayers,
  imageConfig,
  image ? "${stripScheme registry}/${repository}/${imageName}:${tag}",
}:

# Make sure there are *no* slashes in the repository or container
# names since we use these to make the output derivation name for the
# nix-store path.
assert null == lib.findFirst (c: "/" == c) null (lib.stringToCharacters repository);
assert null == lib.findFirst (c: "/" == c) null (lib.stringToCharacters imageName);

let
  # Abuse paths to collapse possible double slashes
  repoTag0 = toString (/. + "/${stripScheme registry}/${repository}/${imageName}");
  repoTag1 = lib.removePrefix "/" repoTag0;

  layers = map stripNixStore imageLayers;

  manifest = writeText "manifest.json" (
    builtins.toJSON [
      {
        Config = stripNixStore imageConfig;
        Layers = layers;
        RepoTags = [ "${repoTag1}:${tag}" ];
      }
    ]
  );

  repositories = writeText "repositories" (
    builtins.toJSON {
      ${repoTag1} = {
        ${tag} = lib.last layers;
      };
    }
  );

  imageFileStorePaths = writeText "imageFileStorePaths.txt" (
    lib.concatStringsSep "\n" ((lib.unique imageLayers) ++ [ imageConfig ])
  );
in
stdenv.mkDerivation {
  builder = ./fetchdocker-builder.sh;
  buildInputs = [ coreutils ];
  preferLocalBuild = true;

  inherit
    name
    imageName
    repository
    tag
    ;
  inherit
    bash
    gnutar
    manifest
    repositories
    ;
  inherit imageFileStorePaths;

  passthru = {
    inherit image;
  };
}