Skip to content

Commit

Permalink
nixos-containers: add networkNamespace option
Browse files Browse the repository at this point in the history
  • Loading branch information
felbinger committed Nov 26, 2024
1 parent 470e6e6 commit ef844ef
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nixos/modules/virtualisation/nixos-containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ let
--setenv HOST_PORT="$HOST_PORT" \
--setenv PATH="$PATH" \
${optionalString cfg.ephemeral "--ephemeral"} \
${optionalString (cfg.networkNamespace != null) "--network-namespace-path ${cfg.networkNamespace}"} \
${optionalString (cfg.additionalCapabilities != null && cfg.additionalCapabilities != [])
''--capability="${concatStringsSep "," cfg.additionalCapabilities}"''
} \
Expand Down Expand Up @@ -632,6 +633,20 @@ in
'';
};

networkNamespace = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Takes the path to a file representing a kernel network namespace that the container
shall run in. The specified path should refer to a (possibly bind-mounted) network
namespace file, as exposed by the kernel below /proc/<PID>/ns/net. This makes the
container enter the given network namespace. One of the typical use cases is to give
a network namespace under /run/netns created by ip-netns(8).
Note that this option cannot be used together with other network-related options,
such as --private-network or --network-interface=.
'';
};

interfaces = mkOption {
type = types.listOf types.str;
default = [];
Expand Down Expand Up @@ -793,6 +808,12 @@ in
{
warnings = optional (!config.boot.enableContainers && config.containers != {})
"containers.<name> is used, but boot.enableContainers is false. To use containers.<name>, set boot.enableContainers to true.";

assertions = let
mapper = name: cfg: optionals (cfg.networkNamespace != null) && (cfg.privateNetwork || (cfg.interfaces != [])))
"containers.${name}.networkNamespace is mutally exclusive to containers.${name}.privateNetwork and containers.${name}.interfaces.";
)
in mkMerge (mapAttrsToList mapper config.containers);
}

(mkIf (config.boot.enableContainers) (let
Expand Down

0 comments on commit ef844ef

Please sign in to comment.