1 { pkgs, lib, config, ... }:
4 cfg = config.services.netns;
5 # Escape as required by: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
7 lib.concatMapStrings (s: if lib.isList s then "-" else s)
8 (builtins.split "[^a-zA-Z0-9_.\\-]+" name);
11 options.services.netns = {
12 namespaces = mkOption {
13 description = "netns namespaces to create";
14 type = types.attrsOf (types.submodule ({ name, ... }: {
24 type = with types; attrsOf (nullOr (oneOf [bool str int]));
34 Systemd configuration specific to this netns service.
43 systemd.services = mapAttrs' (name: c:
44 nameValuePair "netns-${escapeUnitName name}" (mkMerge [
45 { description = "${name} network namespace";
46 before = [ "network.target" ];
49 RemainAfterExit = true;
50 PrivateNetwork = true;
51 ExecStart = "${pkgs.iproute}/bin/ip netns add ${escapeShellArg name}";
52 ExecStartPost = optional config.networking.nftables.enable
53 "${pkgs.iproute}/bin/ip netns exec ${escapeShellArg name} ${pkgs.writeScript "nftables-ruleset" ''
54 #!${pkgs.nftables}/bin/nft -f
58 ExecStop = "${pkgs.iproute}/bin/ip netns del ${escapeShellArg name}";
64 meta.maintainers = with lib.maintainers; [ julm ];