git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #58360 from xtruder/pkgs/dockerTools/nix-prefetch-docker

dockerTools: add nix-prefetch-docker script

commitc087b608e8b5abc9c142b7ed7666b1c9dc047820 authorJaka Hudoklin <offlinehacker@users.noreply.github.com> dateSat 6 Apr 2019, 14:44 (8 years ago) parentcfb6adec1f0d19
4 files changed+230−12
doc/functions/dockertools.xml+29−12 pkgs/build-support/docker/nix-prefetch-docker+173 pkgs/build-support/docker/nix-prefetch-docker.nix+26 pkgs/top-level/all-packages.nix+2
doc/functions/dockertools.xml+29−12
@@ -437,18 +437,7 @@ pullImage {
437437 <callout arearefs='ex-dockerTools-pullImage-2'>
438438 <para>
439439 <varname>imageDigest</varname> specifies the digest of the image to be
440− downloaded. Skopeo can be used to get the digest of an image, with its
441− <varname>inspect</varname> subcommand. Since a given
442− <varname>imageName</varname> may transparently refer to a manifest list of
443− images which support multiple architectures and/or operating systems,
444− supply the `--override-os` and `--override-arch` arguments to specify
445− exactly which image you want. By default it will match the OS and
446− architecture of the host the command is run on.
447−<programlisting>
448−$ nix-shell --packages skopeo jq --command "skopeo --override-os linux --override-arch x86_64 inspect docker://docker.io/nixos/nix:1.11 | jq -r '.Digest'"
449−sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b
450−</programlisting>
451− This argument is required.
440+ downloaded. This argument is required.
452441 </para>
453442 </callout>
454443 <callout arearefs='ex-dockerTools-pullImage-3'>
@@ -486,6 +475,34 @@ sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b
486475 </para>
487476 </callout>
488477 </calloutlist>
478+
479+ <para>
480+ <literal>nix-prefetch-docker</literal> command can be used to get required
481+ image parameters:
482+
483+<programlisting>
484+$ nix run nixpkgs.nix-prefetch-docker -c nix-prefetch-docker --image-name mysql --image-tag 5
485+</programlisting>
486+
487+ Since a given <varname>imageName</varname> may transparently refer to a
488+ manifest list of images which support multiple architectures and/or
489+ operating systems, you can supply the <option>--os</option> and
490+ <option>--arch</option> arguments to specify exactly which image you want.
491+ By default it will match the OS and architecture of the host the command is
492+ run on.
493+
494+<programlisting>
495+$ nix-prefetch-docker --image-name mysql --image-tag 5 --arch x86_64 --os linux
496+</programlisting>
497+
498+ Desired image name and tag can be set using
499+ <option>--final-image-name</option> and <option>--final-image-tag</option>
500+ arguments:
501+
502+<programlisting>
503+$ nix-prefetch-docker --image-name mysql --image-tag 5 --final-image-name eu.gcr.io/my-project/mysql --final-image-tag prod
504+</programlisting>
505+ </para>
489506 </section>
490507
491508 <section xml:id="ssec-pkgs-dockerTools-exportImage">
pkgs/build-support/docker/nix-prefetch-dockernew file+173
@@ -0,0 +1,173 @@
1+#! /usr/bin/env bash
2+
3+set -e -o pipefail
4+
5+os=
6+arch=
7+imageName=
8+imageTag=
9+imageDigest=
10+finalImageName=
11+finalImageTag=
12+hashType=$NIX_HASH_ALGO
13+hashFormat=$hashFormat
14+format=nix
15+
16+usage(){
17+ echo >&2 "syntax: nix-prefetch-docker [options] [IMAGE_NAME [IMAGE_TAG|IMAGE_DIGEST]]
18+
19+Options:
20+ --os os OS to fetch image for
21+ --arch linux Arch to fetch image for
22+ --image-name name Name of the image to fetch
23+ --image-tag tag Image tag
24+ --image-digest digest Image digest
25+ --final-image-name name Desired name of the image
26+ --final-image-tag tag Desired image tag
27+ --json Output result in json format instead of nix
28+ --quiet Only print the final result
29+"
30+ exit 1
31+}
32+
33+get_image_digest(){
34+ local imageName=$1
35+ local imageTag=$2
36+
37+ if test -z "$imageTag"; then
38+ imageTag="latest"
39+ fi
40+
41+ skopeo inspect "docker://$imageName:$imageTag" | jq '.Digest' -r
42+}
43+
44+get_name() {
45+ local imageName=$1
46+ local imageTag=$2
47+
48+ echo "docker-image-$(echo "$imageName:$imageTag" | tr '/:' '-').tar"
49+}
50+
51+argi=0
52+argfun=""
53+for arg; do
54+ if test -z "$argfun"; then
55+ case $arg in
56+ --os) argfun=set_os;;
57+ --arch) argfun=set_arch;;
58+ --image-name) argfun=set_imageName;;
59+ --image-tag) argfun=set_imageTag;;
60+ --image-digest) argfun=set_imageDigest;;
61+ --final-image-name) argfun=set_finalImageName;;
62+ --final-image-tag) argfun=set_finalImageTag;;
63+ --quiet) QUIET=true;;
64+ --json) format=json;;
65+ --help) usage; exit;;
66+ *)
67+ : $((++argi))
68+ case $argi in
69+ 1) imageName=$arg;;
70+ 2) [[ $arg == *"sha256"* ]] && imageDigest=$arg || imageTag=$arg;;
71+ *) exit 1;;
72+ esac
73+ ;;
74+ esac
75+ else
76+ case $argfun in
77+ set_*)
78+ var=${argfun#set_}
79+ eval $var=$arg
80+ ;;
81+ esac
82+ argfun=""
83+ fi
84+done
85+
86+if test -z "$imageName"; then
87+ usage
88+fi
89+
90+if test -z "$os"; then
91+ os=linux
92+fi
93+
94+if test -z "$arch"; then
95+ arch=amd64
96+fi
97+
98+if test -z "$hashType"; then
99+ hashType=sha256
100+fi
101+
102+if test -z "$hashFormat"; then
103+ hashFormat=base32
104+fi
105+
106+if test -z "$finalImageName"; then
107+ finalImageName="$imageName"
108+fi
109+
110+if test -z "$finalImageTag"; then
111+ if test -z "$imageTag"; then
112+ finalImageTag="latest"
113+ else
114+ finalImageTag="$imageTag"
115+ fi
116+fi
117+
118+if test -z "$imageDigest"; then
119+ imageDigest=$(get_image_digest $imageName $imageTag)
120+fi
121+
122+sourceUrl="docker://$imageName@$imageDigest"
123+
124+tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/skopeo-copy-tmp-XXXXXXXX")"
125+trap "rm -rf \"$tmpPath\"" EXIT
126+
127+tmpFile="$tmpPath/$(get_name $finalImageName $finalImageTag)"
128+
129+if test -z "$QUIET"; then
130+ skopeo --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag"
131+else
132+ skopeo --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag" > /dev/null
133+fi
134+
135+# Compute the hash.
136+imageHash=$(nix-hash --flat --type $hashType --base32 "$tmpFile")
137+
138+# Add the downloaded file to Nix store.
139+finalPath=$(nix-store --add-fixed "$hashType" "$tmpFile")
140+
141+if test -z "$QUIET"; then
142+ echo "-> ImageName: $imageName"
143+ echo "-> ImageDigest: $imageDigest"
144+ echo "-> FinalImageName: $finalImageName"
145+ echo "-> FinalImageTag: $finalImageTag"
146+ echo "-> ImagePath: $finalPath"
147+ echo "-> ImageHash: $imageHash"
148+fi
149+
150+if [ "$format" == "nix" ]; then
151+cat <<EOF
152+{
153+ imageName = "$imageName";
154+ imageDigest = "$imageDigest";
155+ sha256 = "$imageHash";
156+ finalImageName = "$finalImageName";
157+ finalImageTag = "$finalImageTag";
158+}
159+EOF
160+
161+else
162+
163+cat <<EOF
164+{
165+ "imageName": "$imageName",
166+ "imageDigest": "$imageDigest",
167+ "sha256": "$imageHash",
168+ "finalImageName": "$finalImageName",
169+ "finalImageTag": "$finalImageTag"
170+}
171+EOF
172+
173+fi
pkgs/build-support/docker/nix-prefetch-docker.nixnew file+26
@@ -0,0 +1,26 @@
1+{ stdenv, makeWrapper, nix, skopeo }:
2+
3+with stdenv.lib;
4+
5+stdenv.mkDerivation {
6+ name = "nix-prefetch-docker";
7+
8+ nativeBuildInputs = [ makeWrapper ];
9+
10+ unpackPhase = ":";
11+
12+ installPhase = ''
13+ install -vD ${./nix-prefetch-docker} $out/bin/$name;
14+ wrapProgram $out/bin/$name \
15+ --prefix PATH : ${makeBinPath [ nix skopeo ]} \
16+ --set HOME /homeless-shelter
17+ '';
18+
19+ preferLocalBuild = true;
20+
21+ meta = {
22+ description = "Script used to obtain source hashes for dockerTools.pullImage";
23+ maintainers = with maintainers; [ offline ];
24+ platforms = platforms.unix;
25+ };
26+}
pkgs/top-level/all-packages.nix+2
@@ -167,6 +167,8 @@ in
167167
168168 dockerTools = callPackage ../build-support/docker { };
169169
170+ nix-prefetch-docker = callPackage ../build-support/docker/nix-prefetch-docker.nix { };
171+
170172 docker-compose = python3Packages.callPackage ../applications/virtualization/docker-compose {};
171173
172174 docker-ls = callPackage ../tools/misc/docker-ls { };