core/pkgs/build-support/fetchdocker/default.nix

47 lines
1.6 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ stdenv, lib, coreutils, bash, gnutar, writeText }:
let
2024-05-13 21:24:10 +00:00
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}" }:
2024-05-02 00:46:19 +00:00
# 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.
2024-05-13 21:24:10 +00:00
assert null
== lib.findFirst (c: "/" == c) null (lib.stringToCharacters repository);
assert null
== lib.findFirst (c: "/" == c) null (lib.stringToCharacters imageName);
2024-05-02 00:46:19 +00:00
let
# Abuse paths to collapse possible double slashes
2024-05-13 21:24:10 +00:00
repoTag0 = builtins.toString
(/. + "/${stripScheme registry}/${repository}/${imageName}");
2024-05-02 00:46:19 +00:00
repoTag1 = lib.removePrefix "/" repoTag0;
layers = builtins.map stripNixStore imageLayers;
2024-05-13 21:24:10 +00:00
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;
2024-05-02 00:46:19 +00:00
buildInputs = [ coreutils ];
preferLocalBuild = true;
inherit name imageName repository tag;
inherit bash gnutar manifest repositories;
inherit imageFileStorePaths;
2024-05-13 21:24:10 +00:00
passthru = { inherit image; };
2024-05-02 00:46:19 +00:00
}