From 9d54ea9b2d5a4fab5d6702081ad8ceacb71360d1 Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Mon, 21 Oct 2019 10:57:56 +0200 Subject: doc: move image builders into new images chapter --- doc/builders/images/appimagetools.xml | 102 ++++++ doc/builders/images/dockertools.xml | 484 +++++++++++++++++++++++++++ doc/builders/images/ocitools.xml | 62 ++++ doc/builders/images/snap/example-firefox.nix | 28 ++ doc/builders/images/snap/example-hello.nix | 12 + doc/builders/images/snaptools.xml | 59 ++++ 6 files changed, 747 insertions(+) create mode 100644 doc/builders/images/appimagetools.xml create mode 100644 doc/builders/images/dockertools.xml create mode 100644 doc/builders/images/ocitools.xml create mode 100644 doc/builders/images/snap/example-firefox.nix create mode 100644 doc/builders/images/snap/example-hello.nix create mode 100644 doc/builders/images/snaptools.xml (limited to 'doc/builders/images') diff --git a/doc/builders/images/appimagetools.xml b/doc/builders/images/appimagetools.xml new file mode 100644 index 000000000000..37e4251cda2e --- /dev/null +++ b/doc/builders/images/appimagetools.xml @@ -0,0 +1,102 @@ +
+ pkgs.appimageTools + + + pkgs.appimageTools is a set of functions for extracting and wrapping AppImage files. They are meant to be used if traditional packaging from source is infeasible, or it would take too long. To quickly run an AppImage file, pkgs.appimage-run can be used as well. + + + + + The appimageTools API is unstable and may be subject to backwards-incompatible changes in the future. + + + +
+ AppImage formats + + + There are different formats for AppImages, see the specification for details. + + + + + + Type 1 images are ISO 9660 files that are also ELF executables. + + + + + Type 2 images are ELF executables with an appended filesystem. + + + + + + They can be told apart with file -k: + + + +$ file -k type1.AppImage +type1.AppImage: ELF 64-bit LSB executable, x86-64, version 1 (SYSV) ISO 9660 CD-ROM filesystem data 'AppImage' (Lepton 3.x), scale 0-0, +spot sensor temperature 0.000000, unit celsius, color scheme 0, calibration: offset 0.000000, slope 0.000000, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.18, BuildID[sha1]=d629f6099d2344ad82818172add1d38c5e11bc6d, stripped\012- data + +$ file -k type2.AppImage +type2.AppImage: ELF 64-bit LSB executable, x86-64, version 1 (SYSV) (Lepton 3.x), scale 232-60668, spot sensor temperature -4.187500, color scheme 15, show scale bar, calibration: offset -0.000000, slope 0.000000 (Lepton 2.x), scale 4111-45000, spot sensor temperature 412442.250000, color scheme 3, minimum point enabled, calibration: offset -75402534979642766821519867692934234112.000000, slope 5815371847733706829839455140374904832.000000, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.18, BuildID[sha1]=79dcc4e55a61c293c5e19edbd8d65b202842579f, stripped\012- data + + + + Note how the type 1 AppImage is described as an ISO 9660 CD-ROM filesystem, and the type 2 AppImage is not. + +
+ +
+ Wrapping + + + Depending on the type of AppImage you're wrapping, you'll have to use wrapType1 or wrapType2. + + + +appimageTools.wrapType2 { # or wrapType1 + name = "patchwork"; + src = fetchurl { + url = https://github.com/ssbc/patchwork/releases/download/v3.11.4/Patchwork-3.11.4-linux-x86_64.AppImage; + sha256 = "1blsprpkvm0ws9b96gb36f0rbf8f5jgmw4x6dsb1kswr4ysf591s"; + }; + extraPkgs = pkgs: with pkgs; [ ]; +} + + + + + name specifies the name of the resulting image. + + + + + src specifies the AppImage file to extract. + + + + + extraPkgs allows you to pass a function to include additional packages inside the FHS environment your AppImage is going to run in. There are a few ways to learn which dependencies an application needs: + + + + Looking through the extracted AppImage files, reading its scripts and running patchelf and ldd on its executables. This can also be done in appimage-run, by setting APPIMAGE_DEBUG_EXEC=bash. + + + + + Running strace -vfefile on the wrapped executable, looking for libraries that can't be found. + + + + + + +
+
diff --git a/doc/builders/images/dockertools.xml b/doc/builders/images/dockertools.xml new file mode 100644 index 000000000000..2243453c3e97 --- /dev/null +++ b/doc/builders/images/dockertools.xml @@ -0,0 +1,484 @@ +
+ pkgs.dockerTools + + + pkgs.dockerTools is a set of functions for creating and manipulating Docker images according to the Docker Image Specification v1.2.0 . Docker itself is not used to perform any of the operations done by these functions. + + + + + The dockerTools API is unstable and may be subject to backwards-incompatible changes in the future. + + + +
+ buildImage + + + This function is analogous to the docker build command, in that it can be used to build a Docker-compatible repository tarball containing a single image with one or multiple layers. As such, the result is suitable for being loaded in Docker with docker load. + + + + The parameters of buildImage with relative example values are described below: + + + + Docker build + +buildImage { + name = "redis"; + tag = "latest"; + + fromImage = someBaseImage; + fromImageName = null; + fromImageTag = "latest"; + + contents = pkgs.redis; + runAsRoot = '' + #!${pkgs.runtimeShell} + mkdir -p /data + ''; + + config = { + Cmd = [ "/bin/redis-server" ]; + WorkingDir = "/data"; + Volumes = { + "/data" = {}; + }; + }; +} + + + + + The above example will build a Docker image redis/latest from the given base image. Loading and running this image in Docker results in redis-server being started automatically. + + + + + + name specifies the name of the resulting image. This is the only required argument for buildImage. + + + + + tag specifies the tag of the resulting image. By default it's null, which indicates that the nix output hash will be used as tag. + + + + + fromImage is the repository tarball containing the base image. It must be a valid Docker image, such as exported by docker save. By default it's null, which can be seen as equivalent to FROM scratch of a Dockerfile. + + + + + fromImageName can be used to further specify the base image within the repository, in case it contains multiple images. By default it's null, in which case buildImage will peek the first image available in the repository. + + + + + fromImageTag can be used to further specify the tag of the base image within the repository, in case an image contains multiple tags. By default it's null, in which case buildImage will peek the first tag available for the base image. + + + + + contents is a derivation that will be copied in the new layer of the resulting image. This can be similarly seen as ADD contents/ / in a Dockerfile. By default it's null. + + + + + runAsRoot is a bash script that will run as root in an environment that overlays the existing layers of the base image with the new resulting layer, including the previously copied contents derivation. This can be similarly seen as RUN ... in a Dockerfile. + + + Using this parameter requires the kvm device to be available. + + + + + + + config is used to specify the configuration of the containers that will be started off the built image in Docker. The available options are listed in the Docker Image Specification v1.2.0 . + + + + + + After the new layer has been created, its closure (to which contents, config and runAsRoot contribute) will be copied in the layer itself. Only new dependencies that are not already in the existing layers will be copied. + + + + At the end of the process, only one new single layer will be produced and added to the resulting image. + + + + The resulting repository will only list the single image image/tag. In the case of it would be redis/latest. + + + + It is possible to inspect the arguments with which an image was built using its buildArgs attribute. + + + + + If you see errors similar to getProtocolByName: does not exist (no such protocol name: tcp) you may need to add pkgs.iana-etc to contents. + + + + + + If you see errors similar to Error_Protocol ("certificate has unknown CA",True,UnknownCa) you may need to add pkgs.cacert to contents. + + + + + Impurely Defining a Docker Layer's Creation Date + + By default buildImage will use a static date of one second past the UNIX Epoch. This allows buildImage to produce binary reproducible images. When listing images with docker images, the newly created images will be listed like this: + + + + You can break binary reproducibility but have a sorted, meaningful CREATED column by setting created to now. + + + + and now the Docker CLI will display a reasonable date and sort the images as expected: + + however, the produced images will not be binary reproducible. + + +
+ +
+ buildLayeredImage + + + Create a Docker image with many of the store paths being on their own layer to improve sharing between images. + + + + + + name + + + + The name of the resulting image. + + + + + + tag optional + + + + Tag of the generated image. + + + Default: the output path's hash + + + + + + contents optional + + + + Top level paths in the container. Either a single derivation, or a list of derivations. + + + Default: [] + + + + + + config optional + + + + Run-time configuration of the container. A full list of the options are available at in the Docker Image Specification v1.2.0 . + + + Default: {} + + + + + + created optional + + + + Date and time the layers were created. Follows the same now exception supported by buildImage. + + + Default: 1970-01-01T00:00:01Z + + + + + + maxLayers optional + + + + Maximum number of layers to create. + + + Default: 100 + + + Maximum: 125 + + + + + + extraCommands optional + + + + Shell commands to run while building the final layer, without access to most of the layer contents. Changes to this layer are "on top" of all the other layers, so can create additional directories and files. + + + + + +
+ Behavior of <varname>contents</varname> in the final image + + + Each path directly listed in contents will have a symlink in the root of the image. + + + + For example: + + will create symlinks for all the paths in the hello package: + /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/bin/hello +/share/info/hello.info -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/info/hello.info +/share/locale/bg/LC_MESSAGES/hello.mo -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/locale/bg/LC_MESSAGES/hello.mo +]]> + +
+ +
+ Automatic inclusion of <varname>config</varname> references + + + The closure of config is automatically included in the closure of the final image. + + + + This allows you to make very simple Docker images with very little code. This container will start up and run hello: + + +
+ +
+ Adjusting <varname>maxLayers</varname> + + + Increasing the maxLayers increases the number of layers which have a chance to be shared between different images. + + + + Modern Docker installations support up to 128 layers, however older versions support as few as 42. + + + + If the produced image will not be extended by other Docker builds, it is safe to set maxLayers to 128. However it will be impossible to extend the image further. + + + + The first (maxLayers-2) most "popular" paths will have their own individual layers, then layer #maxLayers-1 will contain all the remaining "unpopular" paths, and finally layer #maxLayers will contain the Image configuration. + + + + Docker's Layers are not inherently ordered, they are content-addressable and are not explicitly layered until they are composed in to an Image. + +
+
+ +
+ pullImage + + + This function is analogous to the docker pull command, in that it can be used to pull a Docker image from a Docker registry. By default Docker Hub is used to pull images. + + + + Its parameters are described in the example below: + + + + Docker pull + +pullImage { + imageName = "nixos/nix"; + imageDigest = "sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b"; + finalImageName = "nix"; + finalImageTag = "1.11"; + sha256 = "0mqjy3zq2v6rrhizgb9nvhczl87lcfphq9601wcprdika2jz7qh8"; + os = "linux"; + arch = "x86_64"; +} + + + + + + + imageName specifies the name of the image to be downloaded, which can also include the registry namespace (e.g. nixos). This argument is required. + + + + + imageDigest specifies the digest of the image to be downloaded. This argument is required. + + + + + finalImageName, if specified, this is the name of the image to be created. Note it is never used to fetch the image since we prefer to rely on the immutable digest ID. By default it's equal to imageName. + + + + + finalImageTag, if specified, this is the tag of the image to be created. Note it is never used to fetch the image since we prefer to rely on the immutable digest ID. By default it's latest. + + + + + sha256 is the checksum of the whole fetched image. This argument is required. + + + + + os, if specified, is the operating system of the fetched image. By default it's linux. + + + + + arch, if specified, is the cpu architecture of the fetched image. By default it's x86_64. + + + + + + nix-prefetch-docker command can be used to get required image parameters: + +$ nix run nixpkgs.nix-prefetch-docker -c nix-prefetch-docker --image-name mysql --image-tag 5 + + Since a given imageName may transparently refer to a manifest list of images which support multiple architectures and/or operating systems, you can supply the and arguments to specify exactly which image you want. By default it will match the OS and architecture of the host the command is run on. + +$ nix-prefetch-docker --image-name mysql --image-tag 5 --arch x86_64 --os linux + + Desired image name and tag can be set using and arguments: + +$ nix-prefetch-docker --image-name mysql --image-tag 5 --final-image-name eu.gcr.io/my-project/mysql --final-image-tag prod + + +
+ +
+ exportImage + + + This function is analogous to the docker export command, in that it can be used to flatten a Docker image that contains multiple layers. It is in fact the result of the merge of all the layers of the image. As such, the result is suitable for being imported in Docker with docker import. + + + + + Using this function requires the kvm device to be available. + + + + + The parameters of exportImage are the following: + + + + Docker export + +exportImage { + fromImage = someLayeredImage; + fromImageName = null; + fromImageTag = null; + + name = someLayeredImage.name; +} + + + + + The parameters relative to the base image have the same synopsis as described in , except that fromImage is the only required argument in this case. + + + + The name argument is the name of the derivation output, which defaults to fromImage.name. + +
+ +
+ shadowSetup + + + This constant string is a helper for setting up the base files for managing users and groups, only if such files don't exist already. It is suitable for being used in a runAsRoot script for cases like in the example below: + + + + Shadow base files + +buildImage { + name = "shadow-basic"; + + runAsRoot = '' + #!${pkgs.runtimeShell} + ${shadowSetup} + groupadd -r redis + useradd -r -g redis redis + mkdir /data + chown redis:redis /data + ''; +} + + + + + Creating base files like /etc/passwd or /etc/login.defs is necessary for shadow-utils to manipulate users and groups. + +
+
diff --git a/doc/builders/images/ocitools.xml b/doc/builders/images/ocitools.xml new file mode 100644 index 000000000000..f61075b242f8 --- /dev/null +++ b/doc/builders/images/ocitools.xml @@ -0,0 +1,62 @@ +
+ pkgs.ociTools + + + pkgs.ociTools is a set of functions for creating containers according to the OCI container specification v1.0.0. Beyond that it makes no assumptions about the container runner you choose to use to run the created container. + + +
+ buildContainer + + + This function creates a simple OCI container that runs a single command inside of it. An OCI container consists of a config.json and a rootfs directory.The nix store of the container will contain all referenced dependencies of the given command. + + + + The parameters of buildContainer with an example value are described below: + + + + Build Container + +buildContainer { + args = [ (with pkgs; writeScript "run.sh" '' + #!${bash}/bin/bash + ${coreutils}/bin/exec ${bash}/bin/bash + '').outPath ]; + + mounts = { + "/data" = { + type = "none"; + source = "/var/lib/mydata"; + options = [ "bind" ]; + }; + }; + + readonly = false; +} + + + + + + args specifies a set of arguments to run inside the container. This is the only required argument for buildContainer. All referenced packages inside the derivation will be made available inside the container + + + + + mounts specifies additional mount points chosen by the user. By default only a minimal set of necessary filesystems are mounted into the container (e.g procfs, cgroupfs) + + + + + readonly makes the container's rootfs read-only if it is set to true. The default value is false false. + + + + +
+
diff --git a/doc/builders/images/snap/example-firefox.nix b/doc/builders/images/snap/example-firefox.nix new file mode 100644 index 000000000000..d58c98a65a2e --- /dev/null +++ b/doc/builders/images/snap/example-firefox.nix @@ -0,0 +1,28 @@ +let + inherit (import { }) snapTools firefox; +in snapTools.makeSnap { + meta = { + name = "nix-example-firefox"; + summary = firefox.meta.description; + architectures = [ "amd64" ]; + apps.nix-example-firefox = { + command = "${firefox}/bin/firefox"; + plugs = [ + "pulseaudio" + "camera" + "browser-support" + "avahi-observe" + "cups-control" + "desktop" + "desktop-legacy" + "gsettings" + "home" + "network" + "mount-observe" + "removable-media" + "x11" + ]; + }; + confinement = "strict"; + }; +} diff --git a/doc/builders/images/snap/example-hello.nix b/doc/builders/images/snap/example-hello.nix new file mode 100644 index 000000000000..123da80c5477 --- /dev/null +++ b/doc/builders/images/snap/example-hello.nix @@ -0,0 +1,12 @@ +let + inherit (import { }) snapTools hello; +in snapTools.makeSnap { + meta = { + name = "hello"; + summary = hello.meta.description; + description = hello.meta.longDescription; + architectures = [ "amd64" ]; + confinement = "strict"; + apps.hello.command = "${hello}/bin/hello"; + }; +} diff --git a/doc/builders/images/snaptools.xml b/doc/builders/images/snaptools.xml new file mode 100644 index 000000000000..422fcfa37d88 --- /dev/null +++ b/doc/builders/images/snaptools.xml @@ -0,0 +1,59 @@ +
+ pkgs.snapTools + + + pkgs.snapTools is a set of functions for creating Snapcraft images. Snap and Snapcraft is not used to perform these operations. + + +
+ The makeSnap Function + + + makeSnap takes a single named argument, meta. This argument mirrors the upstream snap.yaml format exactly. + + + + The base should not be be specified, as makeSnap will force set it. + + + + Currently, makeSnap does not support creating GUI stubs. + +
+ +
+ Build a Hello World Snap + + + Making a Hello World Snap + + The following expression packages GNU Hello as a Snapcraft snap. + + + + nix-build this expression and install it with snap install ./result --dangerous. hello will now be the Snapcraft version of the package. + + +
+ +
+ Build a Hello World Snap + + + Making a Graphical Snap + + Graphical programs require many more integrations with the host. This example uses Firefox as an example, because it is one of the most complicated programs we could package. + + + + nix-build this expression and install it with snap install ./result --dangerous. nix-example-firefox will now be the Snapcraft version of the Firefox package. + + + The specific meaning behind plugs can be looked up in the Snapcraft interface documentation. + + +
+
-- cgit 1.4.1