200: Improve service.networks r=roberth a=pedorich-n

This PR adds more options to `service.networks`, according to the [spec](https://docs.docker.com/compose/compose-file/compose-file-v3/#networks) it exposes:
- `aliases`
- `ipv4_address`
- `ipv6_address`

A more complex example using these options is added, by modifying the existing `traefik` example.
I wasn't able to run the tests locally on my non-NixOS machine, but from what I can see, it just tests if the host is available. 
That is still true and works, I checked by running `arion up` from the `examples/traefik` folder.

Co-authored-by: Nikita Pedorich <pedorich.n@gmail.com>
This commit is contained in:
bors[bot] 2023-07-21 14:03:31 +00:00 committed by GitHub
commit 9ba47f9fbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 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,11 +253,44 @@ 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, ...}: {
description = serviceRef "networks"; 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 link_local_ips priority; });
};
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";
};
link_local_ips = mkOption {
type = listOf str;
description = serviceRef "link_local_ips";
};
priority = mkOption {
type = int;
description = serviceRef "priority";
};
};
});
in
mkOption {
type = either (listOf str) (attrsOf networksModule);
default = [];
description = serviceRef "networks";
};
service.stop_signal = mkOption { service.stop_signal = mkOption {
type = nullOr str; type = nullOr str;
default = null; default = null;
@ -337,8 +370,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) {