···1919 # we don't limit this action to only NixOS repo since the checks are cheap and useful developer feedback
2020 steps:
2121 - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
2222- - uses: cachix/install-nix-action@7ac1ec25491415c381d9b62f0657c7a028df52a7 # v24
2323- - uses: cachix/cachix-action@6a2e08b5ebf7a9f285ff57b1870a4262b06e0bee # v13
2222+ - uses: cachix/install-nix-action@6004951b182f8860210c8d6f0d808ec5b1a33d28 # v25
2323+ - uses: cachix/cachix-action@18cf96c7c98e048e10a83abd92116114cd8504be # v14
2424 with:
2525 # This cache is for the nixpkgs repo checks and should not be trusted or used elsewhere.
2626 name: nixpkgs-ci
···11# pkgs.dockerTools {#sec-pkgs-dockerTools}
2233-`pkgs.dockerTools` is a set of functions for creating and manipulating Docker images according to the [Docker Image Specification v1.2.0](https://github.com/moby/moby/blob/master/image/spec/v1.2.md#docker-image-specification-v120). Docker itself is not used to perform any of the operations done by these functions.
33+`pkgs.dockerTools` is a set of functions for creating and manipulating Docker images according to the [Docker Image Specification v1.3.0](https://github.com/moby/moby/blob/46f7ab808b9504d735d600e259ca0723f76fb164/image/spec/spec.md#image-json-field-descriptions).
44+Docker itself is not used to perform any of the operations done by these functions.
4556## buildImage {#ssec-pkgs-dockerTools-buildImage}
6777-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`.
88+This function builds a Docker-compatible repository tarball containing a single image.
99+As such, the result is suitable for being loaded in Docker with `docker load` (see [](#ex-dockerTools-buildImage) for how to do this).
1010+1111+This function will create a single layer for all files (and dependencies) that are specified in its argument.
1212+Only new dependencies that are not already in the existing layers will be copied.
1313+If you prefer to create multiple layers for the files and dependencies you want to add to the image, see [](#ssec-pkgs-dockerTools-buildLayeredImage) or [](#ssec-pkgs-dockerTools-streamLayeredImage) instead.
1414+1515+This function allows a script to be run during the layer generation process, allowing custom behaviour to affect the final results of the image (see the documentation of the `runAsRoot` and `extraCommands` attributes).
1616+1717+The resulting repository tarball will list a single image as specified by the `name` and `tag` attributes.
1818+By default, that image will use a static creation date (see documentation for the `created` attribute).
1919+This allows `buildImage` to produce reproducible images.
2020+2121+:::{.tip}
2222+When running an image built with `buildImage`, you might encounter certain errors depending on what you included in the image, especially if you did not start with any base image.
2323+2424+If you encounter errors similar to `getProtocolByName: does not exist (no such protocol name: tcp)`, you may need to add the contents of `pkgs.iana-etc` in the `copyToRoot` attribute.
2525+Similarly, if you encounter errors similar to `Error_Protocol ("certificate has unknown CA",True,UnknownCa)`, you may need to add the contents of `pkgs.cacert` in the `copyToRoot` attribute.
2626+:::
2727+2828+### Inputs {#ssec-pkgs-dockerTools-buildImage-inputs}
2929+3030+`buildImage` expects an argument with the following attributes:
3131+3232+`name` (String)
3333+3434+: The name of the generated image.
3535+3636+`tag` (String or Null; _optional_)
3737+3838+: Tag of the generated image.
3939+ If `null`, the hash of the nix derivation will be used as the tag.
4040+4141+ _Default value:_ `null`.
4242+4343+`fromImage` (Path or Null; _optional_)
4444+4545+: The repository tarball of an image to be used as the base for the generated image.
4646+ It must be a valid Docker image, such as one exported by `docker save`, or another image built with the `dockerTools` utility functions.
4747+ This can be seen as an equivalent of `FROM fromImage` in a `Dockerfile`.
4848+ A value of `null` can be seen as an equivalent of `FROM scratch`.
4949+5050+ If specified, the layer created by `buildImage` will be appended to the layers defined in the base image, resulting in an image with at least two layers (one or more layers from the base image, and the layer created by `buildImage`).
5151+ Otherwise, the resulting image with contain the single layer created by `buildImage`.
5252+5353+ _Default value:_ `null`.
5454+5555+`fromImageName` (String or Null; _optional_)
5656+5757+: Used to specify the image within the repository tarball in case it contains multiple images.
5858+ A value of `null` means that `buildImage` will use the first image available in the repository.
5959+6060+ :::{.note}
6161+ This must be used with `fromImageTag`. Using only `fromImageName` without `fromImageTag` will make `buildImage` use the first image available in the repository.
6262+ :::
6363+6464+ _Default value:_ `null`.
6565+6666+`fromImageTag` (String or Null; _optional_)
6767+6868+: Used to specify the image within the repository tarball in case it contains multiple images.
6969+ A value of `null` means that `buildImage` will use the first image available in the repository.
7070+7171+ :::{.note}
7272+ This must be used with `fromImageName`. Using only `fromImageTag` without `fromImageName` will make `buildImage` use the first image available in the repository
7373+ :::
7474+7575+ _Default value:_ `null`.
7676+7777+`copyToRoot` (Path, List of Paths, or Null; _optional_)
7878+7979+: Files to add to the generated image.
8080+ Anything that coerces to a path (e.g. a derivation) can also be used.
8181+ This can be seen as an equivalent of `ADD contents/ /` in a `Dockerfile`.
88299-The parameters of `buildImage` with relative example values are described below:
8383+ _Default value:_ `null`.
10841111-[]{#ex-dockerTools-buildImage}
1212-[]{#ex-dockerTools-buildImage-runAsRoot}
8585+`keepContentsDirlinks` (Boolean; _optional_)
8686+8787+: When adding files to the generated image (as specified by `copyToRoot`), this attribute controls whether to preserve symlinks to directories.
8888+ If `false`, the symlinks will be transformed into directories.
8989+ This behaves the same as `rsync -k` when `keepContentsDirlinks` is `false`, and the same as `rsync -K` when `keepContentsDirlinks` is `true`.
9090+9191+ _Default value:_ `false`.
9292+9393+`runAsRoot` (String or Null; _optional_)
9494+9595+: A bash script that will run as root inside a VM that contains the existing layers of the base image and the new generated layer (including the files from `copyToRoot`).
9696+ The script will be run with a working directory of `/`.
9797+ This can be seen as an equivalent of `RUN ...` in a `Dockerfile`.
9898+ A value of `null` means that this step in the image generation process will be skipped.
9999+100100+ See [](#ex-dockerTools-buildImage-runAsRoot) for how to work with this attribute.
101101+102102+ :::{.caution}
103103+ Using this attribute requires the `kvm` device to be available, see [`system-features`](https://nixos.org/manual/nix/stable/command-ref/conf-file.html#conf-system-features).
104104+ If the `kvm` device isn't available, you should consider using [`buildLayeredImage`](#ssec-pkgs-dockerTools-buildLayeredImage) or [`streamLayeredImage`](#ssec-pkgs-dockerTools-streamLayeredImage) instead.
105105+ Those functions allow scripts to be run as root without access to the `kvm` device.
106106+ :::
107107+108108+ :::{.note}
109109+ At the time the script in `runAsRoot` is run, the files specified directly in `copyToRoot` will be present in the VM, but their dependencies might not be there yet.
110110+ Copying their dependencies into the generated image is a step that happens after `runAsRoot` finishes running.
111111+ :::
112112+113113+ _Default value:_ `null`.
114114+115115+`extraCommands` (String; _optional_)
116116+117117+: A bash script that will run before the layer created by `buildImage` is finalised.
118118+ The script will be run on some (opaque) working directory which will become `/` once the layer is created.
119119+ This is similar to `runAsRoot`, but the script specified in `extraCommands` is **not** run as root, and does not involve creating a VM.
120120+ It is simply run as part of building the derivation that outputs the layer created by `buildImage`.
121121+122122+ See [](#ex-dockerTools-buildImage-extraCommands) for how to work with this attribute, and subtle differences compared to `runAsRoot`.
123123+124124+ _Default value:_ `""`.
125125+126126+`config` (Attribute Set; _optional_)
127127+128128+: Used to specify the configuration of the containers that will be started off the generated image.
129129+ Must be an attribute set, with each attribute as listed in the [Docker Image Specification v1.3.0](https://github.com/moby/moby/blob/46f7ab808b9504d735d600e259ca0723f76fb164/image/spec/spec.md#image-json-field-descriptions).
130130+131131+ _Default value:_ `null`.
132132+133133+`architecture` (String; _optional_)
134134+135135+: Used to specify the image architecture.
136136+ This is useful for multi-architecture builds that don't need cross compiling.
137137+ If specified, its value should follow the [OCI Image Configuration Specification](https://github.com/opencontainers/image-spec/blob/main/config.md#properties), which should still be compatible with Docker.
138138+ 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`.
139139+140140+ _Default value:_ the same value from `pkgs.go.GOARCH`.
141141+142142+`diskSize` (Number; _optional_)
143143+144144+: Controls the disk size (in megabytes) of the VM used to run the script specified in `runAsRoot`.
145145+ This attribute is ignored if `runAsRoot` is `null`.
146146+147147+ _Default value:_ 1024.
148148+149149+`buildVMMemorySize` (Number; _optional_)
150150+151151+: Controls the amount of memory (in megabytes) provisioned for the VM used to run the script specified in `runAsRoot`.
152152+ This attribute is ignored if `runAsRoot` is `null`.
153153+154154+ _Default value:_ 512.
155155+156156+`created` (String; _optional_)
157157+158158+: Specifies the time of creation of the generated image.
159159+ This should be either a date and time formatted according to [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) or `"now"`, in which case `buildImage` will use the current date.
160160+161161+ See [](#ex-dockerTools-buildImage-creatednow) for how to use `"now"`.
162162+163163+ :::{.caution}
164164+ Using `"now"` means that the generated image will not be reproducible anymore (because the date will always change whenever it's built).
165165+ :::
166166+167167+ _Default value:_ `"1970-01-01T00:00:01Z"`.
168168+169169+`uid` (Number; _optional_)
170170+171171+: The uid of the user that will own the files packed in the new layer built by `buildImage`.
172172+173173+ _Default value:_ 0.
174174+175175+`gid` (Number; _optional_)
176176+177177+: The gid of the group that will own the files packed in the new layer built by `buildImage`.
178178+179179+ _Default value:_ 0.
180180+181181+`contents` **DEPRECATED**
182182+183183+: This attribute is deprecated, and users are encouraged to use `copyToRoot` instead.
184184+185185+### Passthru outputs {#ssec-pkgs-dockerTools-buildImage-passthru-outputs}
186186+187187+`buildImage` defines a few [`passthru`](#var-stdenv-passthru) attributes:
188188+189189+`buildArgs` (Attribute Set)
190190+191191+: The argument passed to `buildImage` itself.
192192+ This allows you to inspect all attributes specified in the argument, as described above.
193193+194194+`layer` (Attribute Set)
195195+196196+: The derivation with the layer created by `buildImage`.
197197+ This allows easier inspection of the contents added by `buildImage` in the generated image.
198198+199199+`imageTag` (String)
200200+201201+: The tag of the generated image.
202202+ This is useful if no tag was specified in the attributes of the argument to `buildImage`, because an automatic tag will be used instead.
203203+ `imageTag` allows you to retrieve the value of the tag used in this case.
204204+205205+### Examples {#ssec-pkgs-dockerTools-buildImage-examples}
206206+207207+:::{.example #ex-dockerTools-buildImage}
208208+# Building a Docker image
209209+210210+The following package builds a Docker image that runs the `redis-server` executable from the `redis` package.
211211+The Docker image will have name `redis` and tag `latest`.
1321214213```nix
1515-buildImage {
214214+{ dockerTools, buildEnv, redis }:
215215+dockerTools.buildImage {
16216 name = "redis";
17217 tag = "latest";
182181919- fromImage = someBaseImage;
2020- fromImageName = null;
2121- fromImageTag = "latest";
2222-2323- copyToRoot = pkgs.buildEnv {
219219+ copyToRoot = buildEnv {
24220 name = "image-root";
2525- paths = [ pkgs.redis ];
221221+ paths = [ redis ];
26222 pathsToLink = [ "/bin" ];
27223 };
2822429225 runAsRoot = ''
3030- #!${pkgs.runtimeShell}
31226 mkdir -p /data
32227 '';
33228···36231 WorkingDir = "/data";
37232 Volumes = { "/data" = { }; };
38233 };
3939-4040- diskSize = 1024;
4141- buildVMMemorySize = 512;
42234}
43235```
442364545-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.
237237+The result of building this package is a `.tar.gz` file that can be loaded into Docker:
462384747-- `name` specifies the name of the resulting image. This is the only required argument for `buildImage`.
239239+```shell
240240+$ nix-build
241241+(some output removed for clarity)
242242+building '/nix/store/yw0adm4wpsw1w6j4fb5hy25b3arr9s1v-docker-image-redis.tar.gz.drv'...
243243+Adding layer...
244244+tar: Removing leading `/' from member names
245245+Adding meta...
246246+Cooking the image...
247247+Finished.
248248+/nix/store/p4dsg62inh9d2ksy3c7bv58xa851dasr-docker-image-redis.tar.gz
482494949-- `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.
250250+$ docker load -i /nix/store/p4dsg62inh9d2ksy3c7bv58xa851dasr-docker-image-redis.tar.gz
251251+(some output removed for clarity)
252252+Loaded image: redis:latest
253253+```
254254+:::
502555151-- `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`.
256256+:::{.example #ex-dockerTools-buildImage-runAsRoot}
257257+# Building a Docker image with `runAsRoot`
522585353-- `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.
259259+The following package builds a Docker image with the `hello` executable from the `hello` package.
260260+It uses `runAsRoot` to create a directory and a file inside the image.
542615555-- `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.
262262+This works the same as [](#ex-dockerTools-buildImage-extraCommands), but uses `runAsRoot` instead of `extraCommands`.
562635757-- `copyToRoot` 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`.
5858-5959-- `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`.
6060-6161-> **_NOTE:_** Using this parameter requires the `kvm` device to be available.
6262-6363-- `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](https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions).
6464-6565-- `architecture` is _optional_ and used to specify the image architecture, this is useful for multi-architecture builds that don't need cross compiling. If not specified it will default to `hostPlatform`.
264264+```nix
265265+{ dockerTools, buildEnv, hello }:
266266+dockerTools.buildImage {
267267+ name = "hello";
268268+ tag = "latest";
662696767-- `diskSize` is used to specify the disk size of the VM used to build the image in megabytes. By default it's 1024 MiB.
270270+ copyToRoot = buildEnv {
271271+ name = "image-root";
272272+ paths = [ hello ];
273273+ pathsToLink = [ "/bin" ];
274274+ };
682756969-- `buildVMMemorySize` is used to specify the memory size of the VM to build the image in megabytes. By default it's 512 MiB.
276276+ runAsRoot = ''
277277+ mkdir -p /data
278278+ echo "some content" > my-file
279279+ '';
702807171-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.
281281+ config = {
282282+ Cmd = [ "/bin/hello" ];
283283+ WorkingDir = "/data";
284284+ };
285285+}
286286+```
287287+:::
722887373-At the end of the process, only one new single layer will be produced and added to the resulting image.
289289+:::{.example #ex-dockerTools-buildImage-extraCommands}
290290+# Building a Docker image with `extraCommands`
742917575-The resulting repository will only list the single image `image/tag`. In the case of [the `buildImage` example](#ex-dockerTools-buildImage), it would be `redis/latest`.
292292+The following package builds a Docker image with the `hello` executable from the `hello` package.
293293+It uses `extraCommands` to create a directory and a file inside the image.
762947777-It is possible to inspect the arguments with which an image was built using its `buildArgs` attribute.
295295+This works the same as [](#ex-dockerTools-buildImage-runAsRoot), but uses `extraCommands` instead of `runAsRoot`.
296296+Note that with `extraCommands`, we can't directly reference `/` and must create files and directories as if we were already on `/`.
782977979-> **_NOTE:_** 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`.
298298+```nix
299299+{ dockerTools, buildEnv, hello }:
300300+dockerTools.buildImage {
301301+ name = "hello";
302302+ tag = "latest";
803038181-> **_NOTE:_** If you see errors similar to `Error_Protocol ("certificate has unknown CA",True,UnknownCa)` you may need to add `pkgs.cacert` to `contents`.
304304+ copyToRoot = buildEnv {
305305+ name = "image-root";
306306+ paths = [ hello ];
307307+ pathsToLink = [ "/bin" ];
308308+ };
823098383-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:
310310+ extraCommands = ''
311311+ mkdir -p data
312312+ echo "some content" > my-file
313313+ '';
843148585-```ShellSession
8686-$ docker images
8787-REPOSITORY TAG IMAGE ID CREATED SIZE
8888-hello latest 08c791c7846e 48 years ago 25.2MB
315315+ config = {
316316+ Cmd = [ "/bin/hello" ];
317317+ WorkingDir = "/data";
318318+ };
319319+}
89320```
321321+:::
903229191-You can break binary reproducibility but have a sorted, meaningful `CREATED` column by setting `created` to `now`.
323323+:::{.example #ex-dockerTools-buildImage-creatednow}
324324+# Building a Docker image with a creation date set to the current time
325325+326326+Note that using a value of `"now"` in the `created` attribute will break reproducibility.
9232793328```nix
9494-pkgs.dockerTools.buildImage {
329329+{ dockerTools, buildEnv, hello }:
330330+dockerTools.buildImage {
95331 name = "hello";
96332 tag = "latest";
333333+97334 created = "now";
9898- copyToRoot = pkgs.buildEnv {
335335+336336+ copyToRoot = buildEnv {
99337 name = "image-root";
100100- paths = [ pkgs.hello ];
338338+ paths = [ hello ];
101339 pathsToLink = [ "/bin" ];
102340 };
103341···105343}
106344```
107345108108-Now the Docker CLI will display a reasonable date and sort the images as expected:
346346+After importing the generated repository tarball with Docker, its CLI will display a reasonable date and sort the images as expected:
109347110348```ShellSession
111349$ docker images
112350REPOSITORY TAG IMAGE ID CREATED SIZE
113351hello latest de2bf4786de6 About a minute ago 25.2MB
114352```
353353+:::
115354116116-However, the produced images will not be binary reproducible.
355355+## buildLayeredImage {#ssec-pkgs-dockerTools-buildLayeredImage}
356356+357357+`buildLayeredImage` uses [`streamLayeredImage`](#ssec-pkgs-dockerTools-streamLayeredImage) underneath to build a compressed Docker-compatible repository tarball.
358358+Basically, `buildLayeredImage` runs the script created by `streamLayeredImage` to save the compressed image in the Nix store.
359359+`buildLayeredImage` supports the same options as `streamLayeredImage`, see [`streamLayeredImage`](#ssec-pkgs-dockerTools-streamLayeredImage) for details.
360360+361361+:::{.note}
362362+Despite the similar name, [`buildImage`](#ssec-pkgs-dockerTools-buildImage) works completely differently from `buildLayeredImage` and `streamLayeredImage`.
363363+364364+Even though some of the arguments may seem related, they cannot be interchanged.
365365+:::
366366+367367+You can use this function to load an image in Docker with `docker load`.
368368+See [](#ex-dockerTools-buildLayeredImage-hello) to see how to do that.
369369+370370+### Examples {#ssec-pkgs-dockerTools-buildLayeredImage-examples}
371371+372372+:::{.example #ex-dockerTools-buildLayeredImage-hello}
373373+# Building a layered Docker image
374374+375375+The following package builds a layered Docker image that runs the `hello` executable from the `hello` package.
376376+The Docker image will have name `hello` and tag `latest`.
377377+378378+```nix
379379+{ dockerTools, hello }:
380380+dockerTools.buildLayeredImage {
381381+ name = "hello";
382382+ tag = "latest";
383383+384384+ contents = [ hello ];
385385+386386+ config.Cmd = [ "/bin/hello" ];
387387+}
388388+```
389389+390390+The result of building this package is a `.tar.gz` file that can be loaded into Docker:
391391+392392+```shell
393393+$ nix-build
394394+(some output removed for clarity)
395395+building '/nix/store/bk8bnrbw10nq7p8pvcmdr0qf57y6scha-hello.tar.gz.drv'...
396396+No 'fromImage' provided
397397+Creating layer 1 from paths: ['/nix/store/i93s7xxblavsacpy82zdbn4kplsyq48l-libunistring-1.1']
398398+Creating layer 2 from paths: ['/nix/store/ji01n9vinnj22nbrb86nx8a1ssgpilx8-libidn2-2.3.4']
399399+Creating layer 3 from paths: ['/nix/store/ldrslljw4rg026nw06gyrdwl78k77vyq-xgcc-12.3.0-libgcc']
400400+Creating layer 4 from paths: ['/nix/store/9y8pmvk8gdwwznmkzxa6pwyah52xy3nk-glibc-2.38-27']
401401+Creating layer 5 from paths: ['/nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1']
402402+Creating layer 6 with customisation...
403403+Adding manifests...
404404+Done.
405405+/nix/store/hxcz7snvw7f8rzhbh6mv8jq39d992905-hello.tar.gz
406406+407407+$ docker load -i /nix/store/hxcz7snvw7f8rzhbh6mv8jq39d992905-hello.tar.gz
408408+(some output removed for clarity)
409409+Loaded image: hello:latest
410410+```
411411+:::
412412+413413+## streamLayeredImage {#ssec-pkgs-dockerTools-streamLayeredImage}
414414+415415+`streamLayeredImage` builds a **script** which, when run, will stream to stdout a Docker-compatible repository tarball containing a single image, using multiple layers to improve sharing between images.
416416+This means that `streamLayeredImage` 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.
417417+418418+You can use this function to load an image in Docker with `docker load`.
419419+See [](#ex-dockerTools-streamLayeredImage-hello) to see how to do that.
420420+421421+For this function, you specify a [store path](https://nixos.org/manual/nix/stable/store/store-path) or a list of store paths to be added to the image, and the functions will automatically include any dependencies of those paths in the image.
422422+The function will attempt to create one layer per object in the Nix store that needs to be added to the image.
423423+In case there are more objects to include than available layers, the function will put the most ["popular"](https://github.com/NixOS/nixpkgs/tree/release-23.11/pkgs/build-support/references-by-popularity) objects in their own layers, and group all remaining objects into a single layer.
424424+425425+An additional layer will be created with symlinks to the store paths you specified to be included in the image.
426426+These symlinks are built with [`symlinkJoin`](#trivial-builder-symlinkJoin), so they will be included in the root of the image.
427427+See [](#ex-dockerTools-streamLayeredImage-exploringlayers) to understand how these symlinks are laid out in the generated image.
428428+429429+`streamLayeredImage` allows scripts to be run when creating the additional layer with symlinks, allowing custom behaviour to affect the final results of the image (see the documentation of the `extraCommands` and `fakeRootCommands` attributes).
430430+431431+The resulting repository tarball will list a single image as specified by the `name` and `tag` attributes.
432432+By default, that image will use a static creation date (see documentation for the `created` attribute).
433433+This allows the function to produce reproducible images.
117434118118-## buildLayeredImage {#ssec-pkgs-dockerTools-buildLayeredImage}
435435+### Inputs {#ssec-pkgs-dockerTools-streamLayeredImage-inputs}
119436120120-Create a Docker image with many of the store paths being on their own layer to improve sharing between images. The image is realized into the Nix store as a gzipped tarball. Depending on the intended usage, many users might prefer to use `streamLayeredImage` instead, which this function uses internally.
437437+`streamLayeredImage` expects one argument with the following attributes:
121438122122-`name`
439439+`name` (String)
123440124124-: The name of the resulting image.
441441+: The name of the generated image.
125442126126-`tag` _optional_
443443+`tag` (String; _optional_)
127444128445: Tag of the generated image.
446446+ If `null`, the hash of the nix derivation will be used as the tag.
129447130130- *Default:* the output path's hash
448448+ _Default value:_ `null`.
131449132132-`fromImage` _optional_
450450+`fromImage`(Path or Null; _optional_)
133451134134-: The repository tarball containing the base image. It must be a valid Docker image, such as one exported by `docker save`.
452452+: The repository tarball of an image to be used as the base for the generated image.
453453+ It must be a valid Docker image, such as one exported by `docker save`, or another image built with the `dockerTools` utility functions.
454454+ This can be seen as an equivalent of `FROM fromImage` in a `Dockerfile`.
455455+ A value of `null` can be seen as an equivalent of `FROM scratch`.
135456136136- *Default:* `null`, which can be seen as equivalent to `FROM scratch` of a `Dockerfile`.
457457+ If specified, the created layers will be appended to the layers defined in the base image.
458458+459459+ _Default value:_ `null`.
460460+461461+`contents` (Path or List of Paths; _optional_) []{#dockerTools-buildLayeredImage-arg-contents}
462462+463463+: Directories whose contents will be added to the generated image.
464464+ Things that coerce to paths (e.g. a derivation) can also be used.
465465+ This can be seen as an equivalent of `ADD contents/ /` in a `Dockerfile`.
466466+467467+ All the contents specified by `contents` will be added as a final layer in the generated image.
468468+ They will be added as links to the actual files (e.g. links to the store paths).
469469+ The actual files will be added in previous layers.
470470+471471+ _Default value:_ `[]`
472472+473473+`config` (Attribute Set; _optional_) []{#dockerTools-buildLayeredImage-arg-config}
474474+475475+: Used to specify the configuration of the containers that will be started off the generated image.
476476+ Must be an attribute set, with each attribute as listed in the [Docker Image Specification v1.3.0](https://github.com/moby/moby/blob/46f7ab808b9504d735d600e259ca0723f76fb164/image/spec/spec.md#image-json-field-descriptions).
477477+478478+ If any packages are used directly in `config`, they will be automatically included in the generated image.
479479+ See [](#ex-dockerTools-streamLayeredImage-configclosure) for an example.
480480+481481+ _Default value:_ `null`.
482482+483483+`architecture` (String; _optional_)
484484+485485+: Used to specify the image architecture.
486486+ This is useful for multi-architecture builds that don't need cross compiling.
487487+ If specified, its value should follow the [OCI Image Configuration Specification](https://github.com/opencontainers/image-spec/blob/main/config.md#properties), which should still be compatible with Docker.
488488+ 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`.
489489+490490+ _Default value:_ the same value from `pkgs.go.GOARCH`.
491491+492492+`created` (String; _optional_)
493493+494494+: Specifies the time of creation of the generated image.
495495+ This should be either a date and time formatted according to [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) or `"now"`, in which case the current date will be used.
496496+497497+ :::{.caution}
498498+ Using `"now"` means that the generated image will not be reproducible anymore (because the date will always change whenever it's built).
499499+ :::
500500+501501+ _Default value:_ `"1970-01-01T00:00:01Z"`.
502502+503503+`maxLayers` (Number; _optional_) []{#dockerTools-buildLayeredImage-arg-maxLayers}
504504+505505+: The maximum number of layers that will be used by the generated image.
506506+ If a `fromImage` was specified, the number of layers used by `fromImage` will be subtracted from `maxLayers` to ensure that the image generated will have at most `maxLayers`.
137507138138-`contents` _optional_
508508+ :::{.caution}
509509+ Depending on the tool/runtime where the image will be used, there might be a limit to the number of layers that an image can have.
510510+ For Docker, see [this issue on GitHub](https://github.com/docker/docs/issues/8230).
511511+ :::
512512+513513+ _Default value:_ 100.
514514+515515+`extraCommands` (String; _optional_)
516516+517517+: A bash script that will run in the context of the layer created with the contents specified by `contents`.
518518+ At the moment this script runs, only the contents directly specified by `contents` will be available as links.
519519+520520+ _Default value:_ `""`.
139521140140-: Top-level paths in the container. Either a single derivation, or a list of derivations.
522522+`fakeRootCommands` (String; _optional_)
141523142142- *Default:* `[]`
524524+: A bash script that will run in the context of the layer created with the contents specified by `contents`.
525525+ During the process to generate that layer, the script in `extraCommands` will be run first, if specified.
526526+ After that, a {manpage}`fakeroot(1)` environment will be entered.
527527+ The script specified in `fakeRootCommands` runs inside the fakeroot environment, and the layer is then generated from the view of the files inside the fakeroot environment.
143528144144-`config` _optional_
529529+ This is useful to change the owners of the files in the layer (by running `chown`, for example), or performing any other privileged operations related to file manipulation (by default, all files in the layer will be owned by root, and the build environment doesn't have enough privileges to directly perform privileged operations on these files).
145530146146-`architecture` is _optional_ and used to specify the image architecture, this is useful for multi-architecture builds that don't need cross compiling. If not specified it will default to `hostPlatform`.
531531+ For more details, see the manpage for {manpage}`fakeroot(1)`.
147532148148-: Run-time configuration of the container. A full list of the options available is in the [Docker Image Specification v1.2.0](https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions).
533533+ :::{.caution}
534534+ Due to how fakeroot works, static binaries cannot perform privileged file operations in `fakeRootCommands`, unless `enableFakechroot` is set to `true`.
535535+ :::
149536150150- *Default:* `{}`
537537+ _Default value:_ `""`.
151538152152-`created` _optional_
539539+`enableFakechroot` (Boolean; _optional_)
153540154154-: Date and time the layers were created. Follows the same `now` exception supported by `buildImage`.
541541+: By default, the script specified in `fakeRootCommands` only runs inside a fakeroot environment.
542542+ If `enableFakechroot` is `true`, a more complete chroot environment will be created using [`proot`](https://proot-me.github.io/) before running the script in `fakeRootCommands`.
543543+ Files in the Nix store will be available.
544544+ This allows scripts that perform installation in `/` to work as expected.
545545+ This can be seen as an equivalent of `RUN ...` in a `Dockerfile`.
155546156156- *Default:* `1970-01-01T00:00:01Z`
547547+ _Default value:_ `false`
157548158158-`maxLayers` _optional_
549549+`includeStorePaths` (Boolean; _optional_)
159550160160-: Maximum number of layers to create.
551551+: The files specified in `contents` are put into layers in the generated image.
552552+ If `includeStorePaths` is `false`, the actual files will not be included in the generated image, and only links to them will be added instead.
553553+ It is **not recommended** to set this to `false` unless you have other tooling to insert the store paths via other means (such as bind mounting the host store) when running containers with the generated image.
554554+ If you don't provide any extra tooling, the generated image won't run properly.
161555162162- *Default:* `100`
556556+ See [](#ex-dockerTools-streamLayeredImage-exploringlayers) to understand the impact of setting `includeStorePaths` to `false`.
163557164164- *Maximum:* `125`
558558+ _Default value:_ `true`
165559166166-`extraCommands` _optional_
560560+`passthru` (Attribute Set; _optional_)
167561168168-: 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.
562562+: Use this to pass any attributes as [passthru](#var-stdenv-passthru) for the resulting derivation.
169563170170-`fakeRootCommands` _optional_
564564+ _Default value:_ `{}`
171565172172-: Shell commands to run while creating the archive for the final layer in a fakeroot environment. Unlike `extraCommands`, you can run `chown` to change the owners of the files in the archive, changing fakeroot's state instead of the real filesystem. The latter would require privileges that the build user does not have. Static binaries do not interact with the fakeroot environment. By default all files in the archive will be owned by root.
566566+### Passthru outputs {#ssec-pkgs-dockerTools-streamLayeredImage-passthru-outputs}
173567174174-`enableFakechroot` _optional_
568568+`streamLayeredImage` also defines its own [`passthru`](#var-stdenv-passthru) attributes:
175569176176-: Whether to run in `fakeRootCommands` in `fakechroot`, making programs behave as though `/` is the root of the image being created, while files in the Nix store are available as usual. This allows scripts that perform installation in `/` to work as expected. Considering that `fakechroot` is implemented via the same mechanism as `fakeroot`, the same caveats apply.
570570+`imageTag` (String)
177571178178- *Default:* `false`
572572+: The tag of the generated image.
573573+ This is useful if no tag was specified in the attributes of the argument to the function, because an automatic tag will be used instead.
574574+ `imageTag` allows you to retrieve the value of the tag used in this case.
179575180180-### Behavior of `contents` in the final image {#dockerTools-buildLayeredImage-arg-contents}
576576+### Examples {#ssec-pkgs-dockerTools-streamLayeredImage-examples}
181577182182-Each path directly listed in `contents` will have a symlink in the root of the image.
578578+:::{.example #ex-dockerTools-streamLayeredImage-hello}
579579+# Streaming a layered Docker image
183580184184-For example:
581581+The following package builds a **script** which, when run, will stream a layered Docker image that runs the `hello` executable from the `hello` package.
582582+The Docker image will have name `hello` and tag `latest`.
185583186584```nix
187187-pkgs.dockerTools.buildLayeredImage {
585585+{ dockerTools, hello }:
586586+dockerTools.streamLayeredImage {
188587 name = "hello";
189189- contents = [ pkgs.hello ];
588588+ tag = "latest";
589589+590590+ contents = [ hello ];
591591+592592+ config.Cmd = [ "/bin/hello" ];
190593}
191594```
192595193193-will create symlinks for all the paths in the `hello` package:
596596+The result of building this package is a script.
597597+Running this script and piping it into `docker load` gives you the same image that was built in [](#ex-dockerTools-buildLayeredImage-hello).
598598+Note that in this case, the image is never added to the Nix store, but instead streamed directly into Docker.
194599195195-```ShellSession
196196-/bin/hello -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/bin/hello
197197-/share/info/hello.info -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/info/hello.info
198198-/share/locale/bg/LC_MESSAGES/hello.mo -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/locale/bg/LC_MESSAGES/hello.mo
600600+```shell
601601+$ nix-build
602602+(output removed for clarity)
603603+/nix/store/wsz2xl8ckxnlb769irvq6jv1280dfvxd-stream-hello
604604+605605+$ /nix/store/wsz2xl8ckxnlb769irvq6jv1280dfvxd-stream-hello | docker load
606606+No 'fromImage' provided
607607+Creating layer 1 from paths: ['/nix/store/i93s7xxblavsacpy82zdbn4kplsyq48l-libunistring-1.1']
608608+Creating layer 2 from paths: ['/nix/store/ji01n9vinnj22nbrb86nx8a1ssgpilx8-libidn2-2.3.4']
609609+Creating layer 3 from paths: ['/nix/store/ldrslljw4rg026nw06gyrdwl78k77vyq-xgcc-12.3.0-libgcc']
610610+Creating layer 4 from paths: ['/nix/store/9y8pmvk8gdwwznmkzxa6pwyah52xy3nk-glibc-2.38-27']
611611+Creating layer 5 from paths: ['/nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1']
612612+Creating layer 6 with customisation...
613613+Adding manifests...
614614+Done.
615615+(some output removed for clarity)
616616+Loaded image: hello:latest
199617```
618618+:::
200619201201-### Automatic inclusion of `config` references {#dockerTools-buildLayeredImage-arg-config}
202202-203203-The closure of `config` is automatically included in the closure of the final image.
620620+:::{.example #ex-dockerTools-streamLayeredImage-exploringlayers}
621621+# Exploring the layers in an image built with `streamLayeredImage`
204622205205-This allows you to make very simple Docker images with very little code. This container will start up and run `hello`:
623623+Assume the following package, which builds a layered Docker image with the `hello` package.
206624207625```nix
208208-pkgs.dockerTools.buildLayeredImage {
626626+{ dockerTools, hello }:
627627+dockerTools.streamLayeredImage {
209628 name = "hello";
210210- config.Cmd = [ "${pkgs.hello}/bin/hello" ];
629629+ contents = [ hello ];
211630}
212631```
213632214214-### Adjusting `maxLayers` {#dockerTools-buildLayeredImage-arg-maxLayers}
633633+The `hello` package depends on 4 other packages:
215634216216-Increasing the `maxLayers` increases the number of layers which have a chance to be shared between different images.
635635+```shell
636636+$ nix-store --query -R $(nix-build -A hello)
637637+/nix/store/i93s7xxblavsacpy82zdbn4kplsyq48l-libunistring-1.1
638638+/nix/store/ji01n9vinnj22nbrb86nx8a1ssgpilx8-libidn2-2.3.4
639639+/nix/store/ldrslljw4rg026nw06gyrdwl78k77vyq-xgcc-12.3.0-libgcc
640640+/nix/store/9y8pmvk8gdwwznmkzxa6pwyah52xy3nk-glibc-2.38-27
641641+/nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1
642642+```
217643218218-Modern Docker installations support up to 128 layers, but older versions support as few as 42.
644644+This means that all these packages will be included in the image generated by `streamLayeredImage`.
645645+It will put each package in its own layer, for a total of 5 layers with actual files in them.
646646+A final layer will be created only with symlinks for the `hello` package.
219647220220-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.
648648+The image generated will have the following directory structure (some directories were collapsed for readability):
221649222222-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.
650650+```
651651+├── bin
652652+│ └── hello → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/bin/hello
653653+├── nix
654654+│ └── store
655655+│ ├─⊕ 9y8pmvk8gdwwznmkzxa6pwyah52xy3nk-glibc-2.38-27
656656+│ ├─⊕ i93s7xxblavsacpy82zdbn4kplsyq48l-libunistring-1.1
657657+│ ├─⊕ ji01n9vinnj22nbrb86nx8a1ssgpilx8-libidn2-2.3.4
658658+│ ├─⊕ ldrslljw4rg026nw06gyrdwl78k77vyq-xgcc-12.3.0-libgcc
659659+│ └─⊕ zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1
660660+└── share
661661+ ├── info
662662+ │ └── hello.info → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/share/info/hello.info
663663+ ├─⊕ locale
664664+ └── man
665665+ └── man1
666666+ └── hello.1.gz → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/share/man/man1/hello.1.gz
667667+```
223668224224-Docker's Layers are not inherently ordered, they are content-addressable and are not explicitly layered until they are composed in to an Image.
669669+Each of the packages in `/nix/store` comes from a layer in the image.
670670+The final layer adds the `/bin` and `/share` directories, but they only contain links to the actual files in `/nix/store`.
225671226226-## streamLayeredImage {#ssec-pkgs-dockerTools-streamLayeredImage}
672672+If our package sets `includeStorePaths` to `false`, we'll end up with only the final layer with the links, but the actual files won't exist in the image:
227673228228-Builds a script which, when run, will stream an uncompressed tarball of a Docker image to stdout. The arguments to this function are as for `buildLayeredImage`. This method of constructing an image does not realize the image into the Nix store, so it saves on IO and disk/cache space, particularly with large images.
674674+```nix
675675+{ dockerTools, hello }:
676676+dockerTools.streamLayeredImage {
677677+ name = "hello";
678678+ contents = [ hello ];
679679+}
680680+```
229681230230-The image produced by running the output script can be piped directly into `docker load`, to load it into the local docker daemon:
682682+After building this package, the image will have the following directory structure:
231683232232-```ShellSession
233233-$(nix-build) | docker load
684684+```
685685+├── bin
686686+│ └── hello → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/bin/hello
687687+└── share
688688+ ├── info
689689+ │ └── hello.info → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/share/info/hello.info
690690+ ├─⊕ locale
691691+ └── man
692692+ └── man1
693693+ └── hello.1.gz → /nix/store/zhl06z4lrfrkw5rp0hnjjfrgsclzvxpm-hello-2.12.1/share/man/man1/hello.1.gz
234694```
235695236236-Alternatively, the image be piped via `gzip` into `skopeo`, e.g., to copy it into a registry:
696696+Note how the links point to paths in `/nix/store`, but they're not included in the image itself.
697697+This is why you need extra tooling when using `includeStorePaths`:
698698+a container created from such image won't find any of the files it needs to run otherwise.
699699+:::
700700+701701+::: {.example #ex-dockerTools-streamLayeredImage-configclosure}
702702+# Building a layered Docker image with packages directly in `config`
703703+704704+The closure of `config` is automatically included in the generated image.
705705+The following package shows a more compact way to create the same output generated in [](#ex-dockerTools-streamLayeredImage-hello).
237706238238-```ShellSession
239239-$(nix-build) | gzip --fast | skopeo copy docker-archive:/dev/stdin docker://some_docker_registry/myimage:tag
707707+```nix
708708+{ dockerTools, hello, lib }:
709709+dockerTools.streamLayeredImage {
710710+ name = "hello";
711711+ tag = "latest";
712712+ config.Cmd = [ "${lib.getExe hello}" ];
713713+}
240714```
715715+:::
241716242717## pullImage {#ssec-pkgs-dockerTools-fetchFromRegistry}
243718
+4
doc/build-helpers/special/mkshell.section.md
···29293030... all the attributes of `stdenv.mkDerivation`.
31313232+## Variants {#sec-pkgs-mkShell-variants}
3333+3434+`pkgs.mkShellNoCC` is a variant that uses `stdenvNoCC` instead of `stdenv` as base environment. This is useful if no C compiler is needed in the shell environment.
3535+3236## Building the shell {#sec-pkgs-mkShell-building}
33373438This derivation output will contain a text file that contains a reference to
+1
doc/languages-frameworks/javascript.section.md
···354354355355 - The `echo 9` steps comes from this answer: <https://stackoverflow.com/a/49139496>
356356 - Exporting the headers in `npm_config_nodedir` comes from this issue: <https://github.com/nodejs/node-gyp/issues/1191#issuecomment-301243919>
357357+- `offlineCache` (described [above](#javascript-yarn2nix-preparation)) must be specified to avoid [Import From Derivation](#ssec-import-from-derivation) (IFD) when used inside Nixpkgs.
357358358359## Outside Nixpkgs {#javascript-outside-nixpkgs}
359360
+11
doc/languages-frameworks/qt.section.md
···26262727Additionally all Qt packages must include `wrapQtAppsHook` in `nativeBuildInputs`, or you must explicitly set `dontWrapQtApps`.
28282929+`pkgs.callPackage` does not provide injections for `qtbase` or the like.
3030+Instead you want to either use `pkgs.libsForQt5.callPackage`, or `pkgs.qt6Packages.callPackage`, depending on the Qt version you want to use.
3131+3232+For example (from [here](https://github.com/NixOS/nixpkgs/blob/2f9286912cb215969ece465147badf6d07aa43fe/pkgs/top-level/all-packages.nix#L30106))
3333+3434+```nix
3535+ zeal-qt5 = libsForQt5.callPackage ../data/documentation/zeal { };
3636+ zeal-qt6 = qt6Packages.callPackage ../data/documentation/zeal { };
3737+ zeal = zeal-qt5;
3838+```
3939+2940## Locating runtime dependencies {#qt-runtime-dependencies}
30413142Qt applications must be wrapped to find runtime dependencies.
+10-9
doc/languages-frameworks/rust.section.md
···4444}
4545```
46464747-`buildRustPackage` requires either the `cargoSha256` or the
4848-`cargoHash` attribute which is computed over all crate sources of this
4949-package. `cargoHash256` is used for traditional Nix SHA-256 hashes,
5050-such as the one in the example above. `cargoHash` should instead be
5151-used for [SRI](https://www.w3.org/TR/SRI/) hashes. For example:
4747+`buildRustPackage` requires either the `cargoHash` or the `cargoSha256`
4848+attribute which is computed over all crate sources of this package.
4949+`cargoSha256` is used for traditional Nix SHA-256 hashes. `cargoHash` should
5050+instead be used for [SRI](https://www.w3.org/TR/SRI/) hashes and should be
5151+preferred. For example:
5252+5353+```nix
5454+ cargoHash = "sha256-l1vL2ZdtDRxSGvP0X/l3nMw8+6WF67KPutJEzUROjg8=";
5555+```
52565357Exception: If the application has cargo `git` dependencies, the `cargoHash`/`cargoSha256`
5458approach will not work, and you will need to copy the `Cargo.lock` file of the application
5555-to nixpkgs and continue with the next section for specifying the options of the`cargoLock`
5959+to nixpkgs and continue with the next section for specifying the options of the `cargoLock`
5660section.
57615858-```nix
5959- cargoHash = "sha256-l1vL2ZdtDRxSGvP0X/l3nMw8+6WF67KPutJEzUROjg8=";
6060-```
61626263Both types of hashes are permitted when contributing to nixpkgs. The
6364Cargo hash is obtained by inserting a fake checksum into the
···77777878There are several ways to change the configuration of the container.
7979First, on the host, you can edit
8080-`/var/lib/container/name/etc/nixos/configuration.nix`, and run
8080+`/var/lib/nixos-containers/foo/etc/nixos/configuration.nix`, and run
81818282```ShellSession
8383# nixos-container update foo
···6363 is **restart**ed with the others. If it is set, both the service and the
6464 socket are **stop**ped and the socket is **start**ed, leaving socket
6565 activation to start the service when it's needed.
6666+6767+## Sysinit reactivation {#sec-sysinit-reactivation}
6868+6969+[`sysinit.target`](https://www.freedesktop.org/software/systemd/man/latest/systemd.special.html#sysinit.target)
7070+is a systemd target that encodes system initialization (i.e. early startup). A
7171+few units that need to run very early in the bootup process are ordered to
7272+finish before this target is reached. Probably the most notable one of these is
7373+`systemd-tmpfiles-setup.service`. We will refer to these units as "sysinit
7474+units".
7575+7676+"Normal" systemd units, by default, are ordered AFTER `sysinit.target`. In
7777+other words, these "normal" units expect all services ordered before
7878+`sysinit.target` to have finished without explicity declaring this dependency
7979+relationship for each dependency. See the [systemd
8080+bootup](https://www.freedesktop.org/software/systemd/man/latest/bootup.html)
8181+for more details on the bootup process.
8282+8383+When restarting both a unit ordered before `sysinit.target` as well as one
8484+after, this presents a problem because they would be started at the same time
8585+as they do not explicitly declare their dependency relations.
8686+8787+To solve this, NixOS has an artificial `sysinit-reactivation.target` which
8888+allows you to ensure that services ordered before `sysinit.target` are
8989+restarted correctly. This applies both to the ordering between these sysinit
9090+services as well as ensuring that sysinit units are restarted before "normal"
9191+units.
9292+9393+To make an existing sysinit service restart correctly during system switch, you
9494+have to declare:
9595+9696+```nix
9797+systemd.services.my-sysinit = {
9898+ requiredBy = [ "sysinit-reactivation.target" ];
9999+ before = [ "sysinit-reactivation.target" ];
100100+ restartTriggers = [ config.environment.etc."my-sysinit.d".source ];
101101+};
102102+```
103103+104104+You need to configure appropriate `restartTriggers` specific to your service.
···3737- Forget about the failed state of units (`systemctl reset-failed`)
3838- Reload systemd (`systemctl daemon-reload`)
3939- Reload systemd user instances (`systemctl --user daemon-reload`)
4040-- Set up tmpfiles (`systemd-tmpfiles --create`)
4040+- Reactivate sysinit (`systemctl restart sysinit-reactivation.target`)
4141- Reload units (`systemctl reload`)
4242- Restart units (`systemctl restart`)
4343- Start units (`systemctl start`)
+5
nixos/doc/manual/release-notes/rl-2405.section.md
···116116117117- The executable file names for `firefox-devedition`, `firefox-beta`, `firefox-esr` now matches their package names, which is consistent with the `firefox-*-bin` packages. The desktop entries are also updated so that you can have multiple editions of firefox in your app launcher.
118118119119+- switch-to-configuration does not directly call systemd-tmpfiles anymore.
120120+ Instead, the new artificial sysinit-reactivation.target is introduced which
121121+ allows to restart multiple services that are ordered before sysinit.target
122122+ and respect the ordering between the services.
123123+119124- The `systemd.oomd` module behavior is changed as:
120125121126 - Raise ManagedOOMMemoryPressureLimit from 50% to 80%. This should make systemd-oomd kill things less often, and fix issues like [this](https://pagure.io/fedora-workstation/issue/358).
+3-3
nixos/maintainers/option-usages.nix
···991010# This file is made to be used as follow:
1111#
1212-# $ nix-instantiate ./option-usage.nix --argstr testOption service.xserver.enable -A txtContent --eval
1212+# $ nix-instantiate ./option-usages.nix --argstr testOption service.xserver.enable -A txtContent --eval
1313#
1414# or
1515#
1616-# $ nix-build ./option-usage.nix --argstr testOption service.xserver.enable -A txt -o service.xserver.enable._txt
1616+# $ nix-build ./option-usages.nix --argstr testOption service.xserver.enable -A txt -o service.xserver.enable._txt
1717#
1818# Other targets exists such as `dotContent`, `dot`, and `pdf`. If you are
1919# looking for the option usage of multiple options, you can provide a list
2020# as argument.
2121#
2222-# $ nix-build ./option-usage.nix --arg testOptions \
2222+# $ nix-build ./option-usages.nix --arg testOptions \
2323# '["boot.loader.gummiboot.enable" "boot.loader.gummiboot.timeout"]' \
2424# -A txt -o gummiboot.list
2525#
···889889890890close($list_active_users) || die("Unable to close the file handle to loginctl");
891891892892-# Set the new tmpfiles
893893-print STDERR "setting up tmpfiles\n";
894894-system("$new_systemd/bin/systemd-tmpfiles", "--create", "--remove", "--exclude-prefix=/dev") == 0 or $res = 3;
892892+# Restart sysinit-reactivation.target.
893893+# This target only exists to restart services ordered before sysinit.target. We
894894+# cannot use X-StopOnReconfiguration to restart sysinit.target because then ALL
895895+# services of the system would be restarted since all normal services have a
896896+# default dependency on sysinit.target. sysinit-reactivation.target ensures
897897+# that services ordered BEFORE sysinit.target get re-started in the correct
898898+# order. Ordering between these services is respected.
899899+print STDERR "restarting sysinit-reactivation.target\n";
900900+system("$new_systemd/bin/systemctl", "restart", "sysinit-reactivation.target") == 0 or $res = 4;
895901896902# Before reloading we need to ensure that the units are still active. They may have been
897903# deactivated because one of their requirements got stopped. If they are inactive
+7
nixos/modules/system/boot/systemd.nix
···569569 unitConfig.X-StopOnReconfiguration = true;
570570 };
571571572572+ # This target only exists so that services ordered before sysinit.target
573573+ # are restarted in the correct order, notably BEFORE the other services,
574574+ # when switching configurations.
575575+ systemd.targets.sysinit-reactivation = {
576576+ description = "Reactivate sysinit units";
577577+ };
578578+572579 systemd.units =
573580 mapAttrs' (n: v: nameValuePair "${n}.path" (pathToUnit n v)) cfg.paths
574581 // mapAttrs' (n: v: nameValuePair "${n}.service" (serviceToUnit n v)) cfg.services
+35
nixos/modules/system/boot/systemd/tmpfiles.nix
···150150 "systemd-tmpfiles-setup.service"
151151 ];
152152153153+ # Allow systemd-tmpfiles to be restarted by switch-to-configuration. This
154154+ # service is not pulled into the normal boot process. It only exists for
155155+ # switch-to-configuration.
156156+ #
157157+ # This needs to be a separate unit because it does not execute
158158+ # systemd-tmpfiles with `--boot` as that is supposed to only be executed
159159+ # once at boot time.
160160+ #
161161+ # Keep this aligned with the upstream `systemd-tmpfiles-setup.service` unit.
162162+ systemd.services."systemd-tmpfiles-resetup" = {
163163+ description = "Re-setup tmpfiles on a system that is already running.";
164164+165165+ requiredBy = [ "sysinit-reactivation.target" ];
166166+ after = [ "local-fs.target" "systemd-sysusers.service" "systemd-journald.service" ];
167167+ before = [ "sysinit-reactivation.target" "shutdown.target" ];
168168+ conflicts = [ "shutdown.target" ];
169169+ restartTriggers = [ config.environment.etc."tmpfiles.d".source ];
170170+171171+ unitConfig.DefaultDependencies = false;
172172+173173+ serviceConfig = {
174174+ Type = "oneshot";
175175+ RemainAfterExit = true;
176176+ ExecStart = "systemd-tmpfiles --create --remove --exclude-prefix=/dev";
177177+ SuccessExitStatus = "DATAERR CANTCREAT";
178178+ ImportCredential = [
179179+ "tmpfiles.*"
180180+ "loging.motd"
181181+ "login.issue"
182182+ "network.hosts"
183183+ "ssh.authorized_keys.root"
184184+ ];
185185+ };
186186+ };
187187+153188 environment.etc = {
154189 "tmpfiles.d".source = (pkgs.symlinkJoin {
155190 name = "tmpfiles.d";
···3939 aioh2 = throw "aioh2 has been removed because it is abandoned and broken."; # Added 2022-03-30
4040 aionotify = throw "aionotify has been removed because is unmaintained and incompatible with python3.11."; # Added 2023-10-27
4141 aiosenseme = throw "aiosenseme has been removed, because it does no longer work with the latest firmware and has become unmaintained"; # Added 2023-07-05
4242+ aioquic-mitmproxy = throw "aioquic-mitmproxy has been removed because mitmproxy no longer uses it"; # Added 2024-01-16
4243 amazon_kclpy = amazon-kclpy; # added 2023-08-08
4344 ansible-base = throw "ansible-base has been removed, because it is end of life"; # added 2022-03-30
4445 ansible-doctor = throw "ansible-doctor has been promoted to a top-level attribute"; # Added 2023-05-16