Improve service.networks

This commit is contained in:
Nikita Pedorich 2023-07-19 18:12:42 +09:00
parent f8359746cc
commit b175f45613
No known key found for this signature in database
GPG Key ID: ADC7FB37D4DF4CE2
2 changed files with 52 additions and 10 deletions

View File

@ -10,6 +10,17 @@
*/ */
{ lib, pkgs, ... }: { { lib, pkgs, ... }: {
config.project.name = "traefik"; config.project.name = "traefik";
config.networks = {
traefik-custom = {
name = "traefik-custom";
ipam = {
config = [{
subnet = "172.32.0.0/16";
gateway = "172.32.0.1";
}];
};
};
};
config.services = { config.services = {
traefik = { traefik = {
image.command = [ image.command = [
@ -24,6 +35,7 @@
stop_signal = "SIGINT"; stop_signal = "SIGINT";
ports = [ "80:80" "8080:8080" ]; ports = [ "80:80" "8080:8080" ];
volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ]; volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ];
networks = [ "traefik-custom" ];
}; };
}; };
@ -34,14 +46,17 @@
${pkgs.python3}/bin/python -m http.server ${pkgs.python3}/bin/python -m http.server
''}"]; ''}"];
service.container_name = "simple-service"; service.container_name = "simple-service";
service.ports = [
"8000:8000" # host:container
];
service.stop_signal = "SIGINT"; service.stop_signal = "SIGINT";
service.labels = { service.labels = {
"traefik.enable" = "true"; "traefik.enable" = "true";
"traefik.http.routers.nix-docs.rule" = "Host(`nix-docs.localhost`)"; "traefik.http.routers.nix-docs.rule" = "Host(`nix-docs.localhost`)";
"traefik.http.routers.nix-docs.entrypoints" = "web"; "traefik.http.routers.nix-docs.entrypoints" = "web";
"traefik.http.services.nix-docs.loadBalancer.server.port" = "8000";
};
service.networks = {
traefik-custom = {
ipv4_address = "172.32.0.5";
};
}; };
}; };
}; };

View File

@ -253,9 +253,34 @@ in
default = null; default = null;
description = serviceRef "network_mode"; description = serviceRef "network_mode";
}; };
service.networks = mkOption { service.networks =
type = nullOr (listOf types.str); let
default = null; networksModule = submodule ({ config, options, ...}: {
options = {
_out = mkOption {
internal = true;
readOnly = true;
default = lib.mapAttrs (k: opt: opt.value) (lib.filterAttrs (_: opt: opt.isDefined) { inherit (options) aliases ipv4_address ipv6_address; });
};
aliases = mkOption {
type = listOf str;
description = serviceRef "aliases";
default = [ ];
};
ipv4_address = mkOption {
type = str;
description = serviceRef "ipv4_address-ipv6_address";
};
ipv6_address = mkOption {
type = str;
description = serviceRef "ipv4_address-ipv6_address";
};
};
});
in
mkOption {
type = either (listOf str) (attrsOf networksModule);
default = [];
description = serviceRef "networks"; description = serviceRef "networks";
}; };
service.stop_signal = mkOption { service.stop_signal = mkOption {
@ -337,8 +362,10 @@ in
inherit (config.service) privileged; inherit (config.service) privileged;
} // lib.optionalAttrs (config.service.network_mode != null) { } // lib.optionalAttrs (config.service.network_mode != null) {
inherit (config.service) network_mode; inherit (config.service) network_mode;
} // lib.optionalAttrs (config.service.networks != null) { } // lib.optionalAttrs (config.service.networks != [] && config.service.networks != {}) {
inherit (config.service) networks; networks =
if (builtins.isAttrs config.service.networks) then builtins.mapAttrs (_: v: v._out) config.service.networks
else config.service.networks;
} // lib.optionalAttrs (config.service.restart != null) { } // lib.optionalAttrs (config.service.restart != null) {
inherit (config.service) restart; inherit (config.service) restart;
} // lib.optionalAttrs (config.service.stop_signal != null) { } // lib.optionalAttrs (config.service.stop_signal != null) {