Home Explore Blog Models CI



nixpkgs

24th chunk of `doc/build-helpers/images/dockertools.section.md`
cb691e151db80a26213adffe840c1f5a2a5308eb226651040000000100000ceb
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!
```
:::

:::{.example #ex-dockerTools-streamNixShellImage-extendingBuildInputs}
# Adding extra packages to a Docker image built with `streamNixShellImage`

This example shows how to add extra packages to an image built with `streamNixShellImage`.
In this case, we'll add the `cowsay` package.
The Docker image generated will have a name like `hello-<version>-env` and tag `latest`.
This example uses [](#ex-dockerTools-streamNixShellImage-hello) as a starting point.

```nix
{
  dockerTools,
  cowsay,
  hello,
}:
dockerTools.streamNixShellImage {
  tag = "latest";
  drv = hello.overrideAttrs (old: {
    nativeBuildInputs = old.nativeBuildInputs or [ ] ++ [ cowsay ];
  });
}
```

The result of building this package is a script which can be run and piped into `docker image load` to load the generated image.

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

$ /nix/store/h5abh0vljgzg381lna922gqknx6yc0v7-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, we can verify the extra package is available by running `cowsay`:

```shell
$ docker container run -it hello-2.12.1-env:latest
[nix-shell:~]$ cowsay "Hello, world!"
 _______________
< Hello, world! >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
```
:::

:::{.example #ex-dockerTools-streamNixShellImage-addingShellHook}
# Adding a `shellHook` to a Docker image built with `streamNixShellImage`

This example shows how to add a `shellHook` command to an image built with `streamNixShellImage`.
In this case, we'll simply output the string `Hello, world!`.
The Docker image generated will have a name like `hello-<version>-env` and tag `latest`.
This example uses [](#ex-dockerTools-streamNixShellImage-hello) as a starting point.

```nix
{ dockerTools, hello }:
dockerTools.streamNixShellImage {
  tag = "latest";
  drv = hello.overrideAttrs (old: {
    shellHook = ''
      ${old.shellHook or ""}
      echo "Hello, world!"
    '';
  });
}
```

The result of building this package is a script which can be run and piped into `docker image load` to load the generated image.

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

$ /nix/store/iz4dhdvgzazl5vrgyz719iwjzjy6xlx1-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, we can see the result of the `shellHook`:

```shell
$ docker container run -it hello-2.12.1-env:latest
Hello, world!

[nix-shell:~]$
```
:::

Title: Customizing `streamNixShellImage` with Extra Packages and Shell Hooks
Summary
This chunk concludes the demonstration of building the `hello` derivation within a `streamNixShellImage` Docker container. It then introduces two new examples for customizing such images: first, `ex-dockerTools-streamNixShellImage-extendingBuildInputs`, which shows how to add additional packages like `cowsay` to the image by overriding `nativeBuildInputs` of the derivation. Second, `ex-dockerTools-streamNixShellImage-addingShellHook`, which demonstrates how to embed a custom `shellHook` (e.g., printing "Hello, world!") that executes automatically when an interactive container from the image is started. Both examples illustrate the Nix code and the corresponding shell commands for building, loading, and running the customized Docker images.