fixing ip routing for tailscale outside
This commit is contained in:
1 parent
060d427d04
commit
cbba7075a7
4 files changed
+254
-113
No files matched your search
@@ -45,38 +45,57 @@ skyg.nixos.common.containers.enableOnBoot
|
||||
skyg.nixos.common.containers.localDockerRegistries
|
||||
skyg.nixos.common.containers.metricsPort
|
||||
skyg.nixos.common.containers.openMetricsPort
|
||||
skyg.nixos.common.containers.networks.<name>.* → networks.nix (host-created networks)
|
||||
skyg.nixos.common.containers.networks.macvlanLab.* → predefined macvlan lab network
|
||||
skyg.nixos.common.containers.networks.ipvlanLab.* → predefined ipvlan lab network
|
||||
skyg.nixos.common.containers.networks.extra.<name>.* → custom networks (networks.nix)
|
||||
```
|
||||
|
||||
## Host-created networks (`skyg.nixos.common.containers.networks`)
|
||||
|
||||
Creates container networks once on the host (one systemd oneshot each, ordered before
|
||||
`container-networks.target`), independent of any compose project. This is how a _single_
|
||||
network — e.g. one macvlan network — is shared by several `container-services` groups
|
||||
network — e.g. one ipvlan network — is shared by several `container-services` groups
|
||||
instead of being re-declared (and re-created) per stack.
|
||||
|
||||
Two predefined lab networks are available:
|
||||
|
||||
```nix
|
||||
skyg.nixos.common.containers.networks.lab = {
|
||||
driver = "macvlan";
|
||||
driverOpts.parent = "eno1";
|
||||
subnet = "10.0.0.0/16";
|
||||
gateway = "10.0.0.1";
|
||||
# macvlan (host can't reach its own containers)
|
||||
skyg.nixos.common.containers.networks.macvlanLab = {
|
||||
enable = true;
|
||||
parent = "eno1";
|
||||
ipRange = "10.0.101.16/28";
|
||||
};
|
||||
|
||||
# ipvlan L2 mode (host can reach its own containers — preferred)
|
||||
skyg.nixos.common.containers.networks.ipvlanLab = {
|
||||
enable = true;
|
||||
parent = "eno1";
|
||||
ipRange = "10.0.101.16/28";
|
||||
};
|
||||
```
|
||||
|
||||
Each network exposes a computed `compose` attribute (an `external: true` entry) for use in a
|
||||
group's `networks` option:
|
||||
Each exposes a computed `compose` attribute for use in a group's `networks` option:
|
||||
|
||||
```nix
|
||||
skyg.nixos.common.container-services.my-stack = {
|
||||
networks.lan = config.skyg.nixos.common.containers.networks.lab.compose;
|
||||
networks.lan = config.skyg.nixos.common.containers.networks.ipvlanLab.compose;
|
||||
services.app.networks.lan = { ipv4_address = "10.0.101.3"; };
|
||||
};
|
||||
```
|
||||
|
||||
Fields: `enable`, `name` (default = attr name), `driver`, `driverOpts`, `subnet`, `gateway`,
|
||||
`ipRange`, `internal`. Works with both docker and podman.
|
||||
For ad-hoc networks not covered by the predefined ones, use `networks.extra`:
|
||||
|
||||
```nix
|
||||
skyg.nixos.common.containers.networks.extra.myNet = {
|
||||
driver = "bridge";
|
||||
subnet = "172.20.0.0/16";
|
||||
};
|
||||
```
|
||||
|
||||
Predefined fields: `enable`, `parent`, `subnet` (default `10.0.0.0/16`), `gateway` (default
|
||||
`10.0.0.1`), `ipRange`, `internal`. Extra networks also have `name`, `driver`, `driverOpts`.
|
||||
Works with both docker and podman.
|
||||
|
||||
## Container service groups (`skyg.nixos.common.container-services`)
|
||||
|
||||
|
||||
@@ -7,7 +7,40 @@ let
|
||||
runtimeBin = if isDocker then "${pkgs.docker}/bin/docker" else "${pkgs.podman}/bin/podman";
|
||||
runtimeService = if isDocker then "docker.service" else "podman.service";
|
||||
|
||||
enabledNetworks = lib.filterAttrs (_: net: net.enable) cfg.networks;
|
||||
# Convert a lab network config to the generic network shape for createArgs
|
||||
mkNetwork = { driver, mode, parent, subnet, gateway, ipRange, internal, name }: {
|
||||
inherit driver subnet gateway ipRange internal name;
|
||||
driverOpts = { inherit parent; } // lib.optionalAttrs (mode != null) { inherit mode; };
|
||||
};
|
||||
|
||||
# Collect all enabled networks (predefined + extra)
|
||||
labNetworks =
|
||||
lib.optional cfg.networks.macvlanLab.enable
|
||||
(mkNetwork {
|
||||
driver = "macvlan";
|
||||
mode = null;
|
||||
parent = cfg.networks.macvlanLab.parent;
|
||||
subnet = cfg.networks.macvlanLab.subnet;
|
||||
gateway = cfg.networks.macvlanLab.gateway;
|
||||
ipRange = cfg.networks.macvlanLab.ipRange;
|
||||
internal = cfg.networks.macvlanLab.internal;
|
||||
name = "macvlan-lab";
|
||||
})
|
||||
++ lib.optional cfg.networks.ipvlanLab.enable
|
||||
(mkNetwork {
|
||||
driver = "ipvlan";
|
||||
mode = "l2";
|
||||
parent = cfg.networks.ipvlanLab.parent;
|
||||
subnet = cfg.networks.ipvlanLab.subnet;
|
||||
gateway = cfg.networks.ipvlanLab.gateway;
|
||||
ipRange = cfg.networks.ipvlanLab.ipRange;
|
||||
internal = cfg.networks.ipvlanLab.internal;
|
||||
name = "ipvlan-lab";
|
||||
});
|
||||
|
||||
extraNetworks = lib.attrValues (lib.filterAttrs (_: net: net.enable) cfg.networks.extra);
|
||||
|
||||
allNetworks = labNetworks ++ extraNetworks;
|
||||
|
||||
createArgs = net:
|
||||
lib.concatStringsSep " " (
|
||||
@@ -21,82 +54,182 @@ let
|
||||
);
|
||||
in
|
||||
{
|
||||
options.skyg.nixos.common.containers.networks = lib.mkOption {
|
||||
default = { };
|
||||
description = ''
|
||||
Container networks created on the host at boot, one systemd oneshot per
|
||||
network, independent of any compose project. This gives a single network
|
||||
that several container-services groups can share.
|
||||
options.skyg.nixos.common.containers.networks = {
|
||||
macvlanLab = lib.mkOption {
|
||||
default = { };
|
||||
description = "Predefined macvlan lab network (driver = macvlan).";
|
||||
type = lib.types.submodule ({
|
||||
options = {
|
||||
enable = lib.mkEnableOption "macvlan lab network" // { default = false; };
|
||||
|
||||
Reference one from a container-services group through its computed
|
||||
`compose` attribute:
|
||||
parent = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Parent interface (e.g. eno1).";
|
||||
example = "eno1";
|
||||
};
|
||||
|
||||
networks.lan = config.skyg.nixos.common.containers.networks.lab.compose;
|
||||
subnet = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "10.0.0.0/16";
|
||||
description = "IPv4 subnet.";
|
||||
};
|
||||
|
||||
Works with both docker and podman (see runtime).
|
||||
'';
|
||||
type = lib.types.attrsOf (lib.types.submodule ({ name, config, ... }: {
|
||||
options = {
|
||||
enable = lib.mkEnableOption "creating this container network" // { default = true; };
|
||||
gateway = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "10.0.0.1";
|
||||
description = "IPv4 gateway.";
|
||||
};
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = name;
|
||||
description = "Runtime name of the network.";
|
||||
ipRange = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 range for dynamic address allocation (--ip-range).";
|
||||
};
|
||||
|
||||
internal = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Restrict the network to internal traffic (--internal).";
|
||||
};
|
||||
|
||||
compose = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.anything;
|
||||
readOnly = true;
|
||||
description = ''
|
||||
Computed compose `networks:` entry (external) for referencing this
|
||||
network from a container-services group.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
driver = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "bridge";
|
||||
example = "macvlan";
|
||||
description = "Network driver.";
|
||||
config.compose = {
|
||||
name = "macvlan-lab";
|
||||
external = true;
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
ipvlanLab = lib.mkOption {
|
||||
default = { };
|
||||
description = "Predefined ipvlan lab network (driver = ipvlan, mode = l2).";
|
||||
type = lib.types.submodule ({
|
||||
options = {
|
||||
enable = lib.mkEnableOption "ipvlan lab network" // { default = false; };
|
||||
|
||||
parent = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Parent interface (e.g. eno1).";
|
||||
example = "eno1";
|
||||
};
|
||||
|
||||
subnet = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "10.0.0.0/16";
|
||||
description = "IPv4 subnet.";
|
||||
};
|
||||
|
||||
gateway = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "10.0.0.1";
|
||||
description = "IPv4 gateway.";
|
||||
};
|
||||
|
||||
ipRange = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 range for dynamic address allocation (--ip-range).";
|
||||
};
|
||||
|
||||
internal = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Restrict the network to internal traffic (--internal).";
|
||||
};
|
||||
|
||||
compose = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.anything;
|
||||
readOnly = true;
|
||||
description = ''
|
||||
Computed compose `networks:` entry (external) for referencing this
|
||||
network from a container-services group.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
driverOpts = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
example = { parent = "eno1"; };
|
||||
description = "Driver options (--opt key=value), e.g. the macvlan parent interface.";
|
||||
config.compose = {
|
||||
name = "ipvlan-lab";
|
||||
external = true;
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
extra = lib.mkOption {
|
||||
default = { };
|
||||
description = "Additional custom container networks (generic attrset).";
|
||||
type = lib.types.attrsOf (lib.types.submodule ({ name, config, ... }: {
|
||||
options = {
|
||||
enable = lib.mkEnableOption "creating this container network" // { default = true; };
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = name;
|
||||
description = "Runtime name of the network.";
|
||||
};
|
||||
|
||||
driver = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "bridge";
|
||||
example = "macvlan";
|
||||
description = "Network driver.";
|
||||
};
|
||||
|
||||
driverOpts = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = { };
|
||||
example = { parent = "eno1"; };
|
||||
description = "Driver options (--opt key=value), e.g. the macvlan parent interface.";
|
||||
};
|
||||
|
||||
subnet = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 subnet (--subnet).";
|
||||
};
|
||||
|
||||
gateway = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 gateway (--gateway).";
|
||||
};
|
||||
|
||||
ipRange = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 range for dynamic address allocation (--ip-range).";
|
||||
};
|
||||
|
||||
internal = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Restrict the network to internal traffic (--internal).";
|
||||
};
|
||||
|
||||
compose = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.anything;
|
||||
readOnly = true;
|
||||
description = ''
|
||||
Computed compose `networks:` entry (external) for referencing this
|
||||
network from a container-services group.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
subnet = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 subnet (--subnet).";
|
||||
config.compose = {
|
||||
name = config.name;
|
||||
external = true;
|
||||
};
|
||||
|
||||
gateway = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 gateway (--gateway).";
|
||||
};
|
||||
|
||||
ipRange = lib.mkOption {
|
||||
type = lib.types.nullOr lib.types.str;
|
||||
default = null;
|
||||
description = "IPv4 range for dynamic address allocation (--ip-range).";
|
||||
};
|
||||
|
||||
internal = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Restrict the network to internal traffic (--internal).";
|
||||
};
|
||||
|
||||
compose = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.anything;
|
||||
description = ''
|
||||
Computed compose `networks:` entry (external) for referencing this
|
||||
network from a container-services group.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config.compose = {
|
||||
name = config.name;
|
||||
external = true;
|
||||
};
|
||||
}));
|
||||
}));
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
@@ -106,9 +239,9 @@ in
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
};
|
||||
|
||||
systemd.services = lib.mkIf cfg.enable (lib.mapAttrs'
|
||||
(n: net: lib.nameValuePair "container-network-${n}" {
|
||||
description = "Create container network '${net.name}'";
|
||||
systemd.services = lib.mkIf (cfg.enable && allNetworks != [ ]) {
|
||||
container-networks = {
|
||||
description = "Create configured container networks";
|
||||
after = [ runtimeService "network-online.target" ];
|
||||
requires = [ runtimeService ];
|
||||
wantedBy = [ "container-networks.target" ];
|
||||
@@ -117,12 +250,16 @@ in
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
# Idempotent: reuse the network if it already exists.
|
||||
script = ''
|
||||
${runtimeBin} network inspect ${net.name} >/dev/null 2>&1 \
|
||||
|| ${runtimeBin} ${createArgs net}
|
||||
# Prune unused networks so stale ones don't linger.
|
||||
${runtimeBin} network prune -f 2>/dev/null || true
|
||||
|
||||
${lib.concatStringsSep "\n" (map (net: ''
|
||||
${runtimeBin} network inspect ${net.name} >/dev/null 2>&1 \
|
||||
|| ${runtimeBin} ${createArgs net}
|
||||
'') allNetworks)}
|
||||
'';
|
||||
})
|
||||
enabledNetworks);
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user