Home Explore Blog CI



nixpkgs

21th chunk of `doc/build-helpers/images/dockertools.section.md`
88de1d5ef703b1b14d7eee289054bae7221ba39c5bd206f00000000100000fc9
After starting an interactive container, the derivation can be built by running `buildDerivation`, and the output can be executed as expected:

```shell
$ docker container run -it hello-2.12.1-env:latest
[nix-shell:~]$ buildDerivation
Running phase: unpackPhase
unpacking source archive /nix/store/pa10z4ngm0g83kx9mssrqzz30s84vq7k-hello-2.12.1.tar.gz
source root is hello-2.12.1
(some output removed for clarity)
Running phase: fixupPhase
shrinking RPATHs of ELF executables and libraries in /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1
shrinking /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1/bin/hello
checking for references to /build/ in /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1...
gzipping man pages under /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1/share/man/
patching script interpreter paths in /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1
stripping (with command strip and flags -S -p) in  /nix/store/f2vs29jibd7lwxyj35r9h87h6brgdysz-hello-2.12.1/bin

[nix-shell:~]$ $out/bin/hello
Hello, world!
```
:::

## streamNixShellImage {#ssec-pkgs-dockerTools-streamNixShellImage}

`streamNixShellImage` builds a **script** which, when run, will stream to stdout a Docker-compatible repository tarball of an image that sets up an environment similar to that of running `nix-shell` on a derivation.
This means that `streamNixShellImage` does not output an image into the Nix store, but only a script that builds the image, saving on IO and disk/cache space, particularly with large images.
See [](#ex-dockerTools-streamNixShellImage-hello) to understand how to load in Docker the image generated by this script.

The environment set up by `streamNixShellImage` somewhat resembles the Nix sandbox typically used by `nix-build`, with a major difference being that access to the internet is allowed.
It also behaves like an interactive `nix-shell`, running things like `shellHook` (see [](#ex-dockerTools-streamNixShellImage-addingShellHook)) and setting an interactive prompt.
If the derivation is buildable (i.e. `nix-build` can be used on it), running `buildDerivation` in the container will build the derivation, with all its outputs being available in the correct `/nix/store` paths, pointed to by the respective environment variables (e.g. `$out`).

::: {.caution}
The environment in the image doesn't match `nix-shell` or `nix-build` exactly, and this function is known not to work correctly for fixed-output derivations, content-addressed derivations, impure derivations and other special types of derivations.
:::

### Inputs {#ssec-pkgs-dockerTools-streamNixShellImage-inputs}

`streamNixShellImage` expects one argument with the following attributes:

`drv` (Attribute Set)

: The derivation for which the environment in the image will be set up.
  Adding packages to the Docker image is possible by extending the list of `nativeBuildInputs` of this derivation.
  See [](#ex-dockerTools-streamNixShellImage-extendingBuildInputs) for how to do that.
  Similarly, you can extend the image initialization script by extending `shellHook`.
  [](#ex-dockerTools-streamNixShellImage-addingShellHook) shows how to do that.

`name` (String; _optional_)

: The name of the generated image.

  _Default value:_ the value of `drv.name + "-env"`.

`tag` (String or Null; _optional_)

: Tag of the generated image.
  If `null`, the hash of the nix derivation that builds the Docker image will be used as the tag.

  _Default value:_ `null`.

`uid` (Number; _optional_)

: The user ID to run the container as.
  This can be seen as a `nixbld` build user.

  _Default value:_ 1000.

`gid` (Number; _optional_)

: The group ID to run the container as.
  This can be seen as a `nixbld` build group.

  _Default value:_ 1000.

`homeDirectory` (String; _optional_)

: The home directory of the user the container is running as.

  _Default value:_ `/build`.

`shell` (String; _optional_)

: The path to the `bash` binary to use as the shell.
  This shell is started when running the image.

Title: Building and Running Derivations in Docker with streamNixShellImage
Summary
This section continues the `buildNixShellImage` example, showing how to run a built derivation within a Docker container. It then introduces `streamNixShellImage`, which creates a script to build a Docker image resembling a `nix-shell` environment. It details the inputs for `streamNixShellImage`, including `drv` (the derivation), `name`, `tag`, `uid`, `gid`, `homeDirectory`, and `shell`.