1 # pkgs.ociTools {#sec-pkgs-ociTools}
3 `pkgs.ociTools` is a set of functions for creating runtime container bundles according to the [OCI runtime specification v1.0.0](https://github.com/opencontainers/runtime-spec/blob/v1.0.0/spec.md).
4 It makes no assumptions about the container runner you choose to use to run the created container.
6 The set of functions in `pkgs.ociTools` currently does not handle the [OCI image specification](https://github.com/opencontainers/image-spec).
8 At a high-level an OCI implementation would download an OCI Image then unpack that image into an OCI Runtime filesystem bundle.
9 At this point the OCI Runtime Bundle would be run by an OCI Runtime.
10 `pkgs.ociTools` provides utilities to create OCI Runtime bundles.
12 ## buildContainer {#ssec-pkgs-ociTools-buildContainer}
14 This function creates an OCI runtime container (consisting of a `config.json` and a root filesystem directory) that runs a single command inside of it.
15 The nix store of the container will contain all referenced dependencies of the given command.
17 This function has an assumption that the container will run on POSIX platforms, and sets configurations (such as the user running the process or certain mounts) according to this assumption.
18 Because of this, a container built with `buildContainer` will not work on Windows or other non-POSIX platforms without modifications to the container configuration.
19 These modifications aren't supported by `buildContainer`.
21 For `linux` platforms, `buildContainer` also configures the following namespaces (see {manpage}`unshare(1)`) to isolate the OCI container from the global namespace:
22 PID, network, mount, IPC, and UTS.
24 Note that no user namespace is created, which means that you won't be able to run the container unless you are the `root` user.
26 ### Inputs {#ssec-pkgs-ociTools-buildContainer-inputs}
28 `buildContainer` expects an argument with the following attributes:
30 `args` (List of String)
32 : Specifies a set of arguments to run inside the container.
33 Any packages referenced by `args` will be made available inside the container.
35 `mounts` (Attribute Set; _optional_)
37 : Would specify additional mounts that the runtime must make available to the container.
40 As explained in [issue #290879](https://github.com/NixOS/nixpkgs/issues/290879), this attribute is currently ignored.
44 `buildContainer` includes a minimal set of necessary filesystems to be mounted into the container, and this set can't be changed with the `mounts` attribute.
47 _Default value:_ `{}`.
49 `readonly` (Boolean; _optional_)
51 : If `true`, sets the container's root filesystem as read-only.
53 _Default value:_ `false`.
57 : Specifies the operating system on which the container filesystem is based on.
58 If specified, its value should follow the [OCI Image Configuration Specification](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
59 According to the linked specification, all possible values for `$GOOS` in [the Go docs](https://go.dev/doc/install/source#environment) should be valid, but will commonly be one of `darwin` or `linux`.
61 _Default value:_ `"linux"`.
65 : Used to specify the architecture for which the binaries in the container filesystem have been compiled.
66 If specified, its value should follow the [OCI Image Configuration Specification](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
67 According to the linked specification, all possible values for `$GOARCH` in [the Go docs](https://go.dev/doc/install/source#environment) should be valid, but will commonly be one of `386`, `amd64`, `arm`, or `arm64`.
69 _Default value:_ `x86_64`.
71 ### Examples {#ssec-pkgs-ociTools-buildContainer-examples}
73 ::: {.example #ex-ociTools-buildContainer-bash}
74 # Creating an OCI runtime container that runs `bash`
76 This example uses `ociTools.buildContainer` to create a simple container that runs `bash`.
79 { ociTools, lib, bash }:
80 ociTools.buildContainer {
89 As an example of how to run the container generated by this package, we'll use `runc` to start the container.
90 Any other tool that supports OCI containers could be used instead.
94 (some output removed for clarity)
95 /nix/store/7f9hgx0arvhzp2a3qphp28rxbn748l25-join
97 $ cd /nix/store/7f9hgx0arvhzp2a3qphp28rxbn748l25-join
99 [nix-shell:/nix/store/7f9hgx0arvhzp2a3qphp28rxbn748l25-join]$ sudo runc run ocitools-example
101 GNU bash, version 5.2.26(1)-release (x86_64-pc-linux-gnu)
102 (some output removed for clarity)