nixos/oci-containers: add autoRemoveOnStop option

Adds the autoRemoveOnStop option to allow stopped or killed containers to stay around after stop. Default behaviour remains the same.
This commit is contained in:
Emma Miler 2025-03-04 18:14:42 +01:00 committed by Emma Miler
parent 417b73156f
commit 542a053b32

View file

@ -381,6 +381,14 @@ let
''; '';
}; };
autoRemoveOnStop = mkOption {
type = types.bool;
default = true;
description = ''
Automatically remove the container when it is stopped or killed
'';
};
networks = mkOption { networks = mkOption {
type = with types; listOf str; type = with types; listOf str;
default = [ ]; default = [ ];
@ -468,7 +476,6 @@ let
++ map escapeShellArg container.preRunExtraOptions ++ map escapeShellArg container.preRunExtraOptions
++ [ ++ [
"run" "run"
"--rm"
"--name=${escapedName}" "--name=${escapedName}"
"--log-driver=${container.log-driver}" "--log-driver=${container.log-driver}"
] ]
@ -489,6 +496,7 @@ let
++ (mapAttrsToList (k: v: "-l ${escapeShellArg k}=${escapeShellArg v}") container.labels) ++ (mapAttrsToList (k: v: "-l ${escapeShellArg k}=${escapeShellArg v}") container.labels)
++ optional (container.workdir != null) "-w ${escapeShellArg container.workdir}" ++ optional (container.workdir != null) "-w ${escapeShellArg container.workdir}"
++ optional (container.privileged) "--privileged" ++ optional (container.privileged) "--privileged"
++ optional (container.autoRemoveOnStop) "--rm"
++ mapAttrsToList (k: _: "--cap-add=${escapeShellArg k}") ( ++ mapAttrsToList (k: _: "--cap-add=${escapeShellArg k}") (
filterAttrs (_: v: v == true) container.capabilities filterAttrs (_: v: v == true) container.capabilities
) )