Home Explore Blog CI



nixpkgs

22th chunk of `doc/build-helpers/images/dockertools.section.md`
d42c62dc31c3fc6be93de514456062aa0dd9fd11be2aad2d0000000100000dc7
  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.
  This can be seen as an equivalent of the `NIX_BUILD_SHELL` [environment variable](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#environment-variables) for {manpage}`nix-shell(1)`.

  _Default value:_ the `bash` binary from the `bashInteractive` package.

`command` (String or Null; _optional_)

: If specified, this command will be run in the environment of the derivation in an interactive shell.
  A call to `exit` will be added after the command if it is specified, so the shell will exit after it's finished running.
  This can be seen as an equivalent of the `--command` option in {manpage}`nix-shell(1)`.

  _Default value:_ `null`.

`run` (String or Null; _optional_)

: Similar to the `command` attribute, but runs the command in a non-interactive shell instead.
  A call to `exit` will be added after the command if it is specified, so the shell will exit after it's finished running.
  This can be seen as an equivalent of the `--run` option in {manpage}`nix-shell(1)`.

  _Default value:_ `null`.

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

:::{.example #ex-dockerTools-streamNixShellImage-hello}
# Building a Docker image with `streamNixShellImage` with the build environment for the `hello` package

This example shows how to build the `hello` package inside a Docker container built with `streamNixShellImage`.
The Docker image generated will have a name like `hello-<version>-env` and tag `latest`.
This example is the `streamNixShellImage` equivalent of [](#ex-dockerTools-buildNixShellImage-hello).

```nix
{ dockerTools, hello }:
dockerTools.streamNixShellImage {
  drv = hello;
  tag = "latest";
}
```

The result of building this package is a script.
Running this script and piping it into `docker image load` gives you the same image that was built in [](#ex-dockerTools-buildNixShellImage-hello).

```shell
$ nix-build
(some output removed for clarity)
/nix/store/8vhznpz2frqazxnd8pgdvf38jscdypax-stream-hello-2.12.1-env

$ /nix/store/8vhznpz2frqazxnd8pgdvf38jscdypax-stream-hello-2.12.1-env | docker image load
(some output removed for clarity)
Loaded image: hello-2.12.1-env:latest
```

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

Title: streamNixShellImage Input Parameters and Basic Usage Example
Summary
This section details the input parameters for `streamNixShellImage`, including `shell`, `command`, and `run`. It then provides a basic example of using `streamNixShellImage` to build a Docker image containing the build environment for the `hello` package. It demonstrates how to execute the resulting script and load the image into Docker, followed by running the derivation and its output within a container.