From 6505abb2249d0e2cad365cf0eb9668407758a9f0 Mon Sep 17 00:00:00 2001 From: Jade Wilk Date: Sat, 10 Oct 2026 12:30:13 +0100 Subject: [PATCH] nixos/oci-containers: add tmpFs option Signed-off-by: Sefa Eyeoglu --- .../manual/release-notes/rl-2611.section.md | 2 + .../modules/virtualisation/oci-containers.nix | 107 +++++++++++------- nixos/tests/oci-containers.nix | 8 ++ 3 files changed, 76 insertions(+), 41 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2611.section.md b/nixos/doc/manual/release-notes/rl-2611.section.md index 246dcbd560e6..69c616b52e48 100644 --- a/nixos/doc/manual/release-notes/rl-2611.section.md +++ b/nixos/doc/manual/release-notes/rl-2611.section.md @@ -479,6 +479,8 @@ The above example can be migrated via the following structured code block: - The `newuidmap` and `newgidmap` security wrappers are now installed with `cap_setuid`/`cap_setgid` file capabilities instead of the setuid-root bit, matching shadow's `--with-fcaps` install mode and other major distributions. Rootless containers (podman, docker-rootless, unprivileged user namespaces) are unaffected. The only behavioural change is that mapping host uid 0 via `/etc/subuid` (which NixOS never configures by default) additionally requires `cap_setfcap`; users who explicitly grant uid 0 in a subuid range can restore the previous behaviour with `security.wrappers.newuidmap.capabilities = lib.mkForce "cap_setuid,cap_setfcap+ep";`. +- Added `virtualisation.oci-containers.containers..tmpFs` to configure `tmpfs` mounts on OCI containers. Refer to the [Docker engine documentation](https://docs.docker.com/engine/storage/tmpfs/) for the full list of mount options. + - The `authelia` module now uses systemd's `LoadCredential` to load all files defined in `secrets`. As such, these files no longer need to be readable by the authelia user and group: they can for example be set to be only readable by the root user. - `zoneminder` has been updated to 1.38.x release. See [upstream release note](https://github.com/ZoneMinder/zoneminder/releases/tag/1.38.0). While database migration should happen automatically, it's recommended that you make a backup of the database before upgrading your system. diff --git a/nixos/modules/virtualisation/oci-containers.nix b/nixos/modules/virtualisation/oci-containers.nix index d4033df032e2..2cb0bf427807 100644 --- a/nixos/modules/virtualisation/oci-containers.nix +++ b/nixos/modules/virtualisation/oci-containers.nix @@ -219,6 +219,26 @@ let ]; }; + tmpFs = mkOption { + type = types.attrsOf (types.listOf types.str); + default = { }; + description = '' + Attribute set of container paths to mount into a tmpfs. + + Mount options for tmpfs may be passed in as a list for each path. + For the full list of options, refer to the + [Docker engine documentation](https://docs.docker.com/engine/storage/tmpfs/). + ''; + example = { + "/data" = [ + "noexec" + "size=1024" + "mode=1777" + ]; + "/defaultData" = [ ]; + }; + }; + workdir = mkOption { type = with types; nullOr str; default = null; @@ -459,47 +479,52 @@ let else throw "Unhandled backend: ${cfg.backend}"; - script = concatStringsSep " \\\n " ( - [ - "exec ${cfg.backend} " - ] - ++ map escapeShellArg container.preRunExtraOptions - ++ [ - "run" - "--name=${escapedName}" - "--log-driver=${container.log-driver}" - ] - ++ optional (container.entrypoint != null) "--entrypoint=${escapeShellArg container.entrypoint}" - ++ optional (container.hostname != null) "--hostname=${escapeShellArg container.hostname}" - ++ lib.optionals (cfg.backend == "podman") [ - "--cidfile=/run/${escapedName}/ctr-id" - "--cgroups=enabled" - "--sdnotify=${container.podman.sdnotify}" - "-d" - "--replace" - ] - ++ (mapAttrsToList (k: v: "-e ${escapeShellArg k}=${escapeShellArg v}") container.environment) - ++ map (f: "--env-file ${escapeShellArg f}") container.environmentFiles - ++ map (p: "-p ${escapeShellArg p}") container.ports - ++ optional (container.user != null) "-u ${escapeShellArg container.user}" - ++ map (v: "-v ${escapeShellArg v}") container.volumes - ++ (mapAttrsToList (k: v: "-l ${escapeShellArg k}=${escapeShellArg v}") container.labels) - ++ optional (container.workdir != null) "-w ${escapeShellArg container.workdir}" - ++ optional (container.privileged) "--privileged" - ++ optional (container.autoRemoveOnStop) "--rm" - ++ mapAttrsToList (k: _: "--cap-add=${escapeShellArg k}") ( - filterAttrs (_: v: v == true) container.capabilities - ) - ++ mapAttrsToList (k: _: "--cap-drop=${escapeShellArg k}") ( - filterAttrs (_: v: v == false) container.capabilities - ) - ++ map (d: "--device=${escapeShellArg d}") container.devices - ++ map (n: "--network=${escapeShellArg n}") (lib.lists.unique container.networks) - ++ [ "--pull ${escapeShellArg container.pull}" ] - ++ map escapeShellArg container.extraOptions - ++ [ container.image ] - ++ map escapeShellArg container.cmd - ); + script = + let + concatOptions = options: optionalString (options != [ ]) ":${concatStringsSep "," options}"; + in + concatStringsSep " \\\n " ( + [ + "exec ${cfg.backend} " + ] + ++ map escapeShellArg container.preRunExtraOptions + ++ [ + "run" + "--name=${escapedName}" + "--log-driver=${container.log-driver}" + ] + ++ optional (container.entrypoint != null) "--entrypoint=${escapeShellArg container.entrypoint}" + ++ optional (container.hostname != null) "--hostname=${escapeShellArg container.hostname}" + ++ lib.optionals (cfg.backend == "podman") [ + "--cidfile=/run/${escapedName}/ctr-id" + "--cgroups=enabled" + "--sdnotify=${container.podman.sdnotify}" + "-d" + "--replace" + ] + ++ (mapAttrsToList (k: v: "-e ${escapeShellArg k}=${escapeShellArg v}") container.environment) + ++ map (f: "--env-file ${escapeShellArg f}") container.environmentFiles + ++ map (p: "-p ${escapeShellArg p}") container.ports + ++ optional (container.user != null) "-u ${escapeShellArg container.user}" + ++ map (v: "-v ${escapeShellArg v}") container.volumes + ++ (mapAttrsToList (k: v: "--tmpfs ${escapeShellArg k}${concatOptions v}") container.tmpFs) + ++ (mapAttrsToList (k: v: "-l ${escapeShellArg k}=${escapeShellArg v}") container.labels) + ++ optional (container.workdir != null) "-w ${escapeShellArg container.workdir}" + ++ optional (container.privileged) "--privileged" + ++ optional (container.autoRemoveOnStop) "--rm" + ++ mapAttrsToList (k: _: "--cap-add=${escapeShellArg k}") ( + filterAttrs (_: v: v == true) container.capabilities + ) + ++ mapAttrsToList (k: _: "--cap-drop=${escapeShellArg k}") ( + filterAttrs (_: v: v == false) container.capabilities + ) + ++ map (d: "--device=${escapeShellArg d}") container.devices + ++ map (n: "--network=${escapeShellArg n}") (lib.lists.unique container.networks) + ++ [ "--pull ${escapeShellArg container.pull}" ] + ++ map escapeShellArg container.extraOptions + ++ [ container.image ] + ++ map escapeShellArg container.cmd + ); preStop = if cfg.backend == "podman" then diff --git a/nixos/tests/oci-containers.nix b/nixos/tests/oci-containers.nix index c31aa20f7c81..73856164617d 100644 --- a/nixos/tests/oci-containers.nix +++ b/nixos/tests/oci-containers.nix @@ -37,6 +37,11 @@ let devices = [ "/dev/random:/dev/random" ]; + tmpFs."/custom-tmp" = [ + "noexec" + "size=1024" + "mode=1777" + ]; }; }; @@ -63,6 +68,9 @@ let assert output['HostConfig']['Devices'] == [{'PathOnHost': '/dev/random', 'PathInContainer': '/dev/random', 'CgroupPermissions': '${ if backend == "docker" then "rwm" else "" }'}] + assert output['HostConfig']['Tmpfs'] == {'/custom-tmp': 'noexec,size=1024,mode=1777${ + lib.optionalString (backend == "podman") ",rprivate,nosuid,nodev,tmpcopyup" + }'} '' + lib.strings.optionalString (backend == "podman") '' assert output['Config']['Labels']['PODMAN_SYSTEMD_UNIT'] == '${serviceName}.service'