arion/src/nix/modules/service/docker-compose-service.nix

287 lines
9 KiB
Nix
Raw Normal View History

/*
2019-10-03 21:30:14 +02:00
This service-level module defines the out.service option, using
the user-facing options service.image, service.volumes, etc.
*/
{ pkgs, lib, config, ... }:
let
inherit (lib) mkOption types;
2019-03-04 00:12:01 +01:00
inherit (types) listOf nullOr attrsOf str either int bool;
2019-03-03 23:42:40 +01:00
2019-03-11 14:36:18 +01:00
link = url: text:
2019-10-29 12:52:00 +01:00
''link:${url}[${text}]'';
2019-03-03 23:42:40 +01:00
dockerComposeRef = fragment:
2019-10-29 12:52:00 +01:00
''See ${link "https://docs.docker.com/compose/compose-file/#${fragment}" "Docker Compose#${fragment}"}'';
2019-03-03 23:42:40 +01:00
dockerComposeKitchenSink = ''
2019-10-29 12:52:00 +01:00
Analogous to the `docker run` counterpart.
2019-03-03 23:42:40 +01:00
${dockerComposeRef "domainname-hostname-ipc-mac_address-privileged-read_only-shm_size-stdin_open-tty-user-working_dir"}
'';
2019-03-11 14:36:18 +01:00
cap_add = lib.attrNames (lib.filterAttrs (name: value: value == true) config.service.capabilities);
cap_drop = lib.attrNames (lib.filterAttrs (name: value: value == false) config.service.capabilities);
in
{
2019-10-03 21:30:14 +02:00
imports = [
(lib.mkRenamedOptionModule ["build" "service"] ["out" "service"])
];
options = {
2019-10-03 21:30:14 +02:00
out.service = mkOption {
2019-03-04 00:13:57 +01:00
type = attrsOf types.unspecified;
description = ''
2019-10-29 12:52:00 +01:00
Raw input for the service in `docker-compose.yaml`.
2019-03-04 00:13:57 +01:00
You should not need to use this option. If anything is
missing, please contribute the missing option.
This option is user accessible because it may serve as an
escape hatch for some.
'';
2019-10-03 21:30:14 +02:00
apply = config.assertWarn;
2019-03-04 00:13:57 +01:00
};
service.name = mkOption {
type = str;
description = ''
2019-10-29 12:52:00 +01:00
The name of the service - `<name>` in the composition-level `services.<name>`
'';
readOnly = true;
};
service.volumes = mkOption {
type = listOf types.unspecified;
default = [];
description = dockerComposeRef "volumes";
};
service.tmpfs = mkOption {
type = listOf types.str;
default = [];
description = dockerComposeRef "tmpfs";
};
service.build.context = mkOption {
2019-03-04 00:12:01 +01:00
type = nullOr str;
default = null;
2019-03-03 23:42:40 +01:00
description = ''
Locates a Dockerfile to use for creating an image to use in this service.
${dockerComposeRef "context"}
'';
};
service.hostname = mkOption {
2019-03-04 00:12:01 +01:00
type = nullOr str;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeKitchenSink;
};
service.tty = mkOption {
type = nullOr bool;
default = null;
description = dockerComposeKitchenSink;
};
service.environment = mkOption {
2019-03-04 00:12:01 +01:00
type = attrsOf (either str int);
default = {};
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "environment";
};
service.image = mkOption {
2019-03-04 00:12:01 +01:00
type = str;
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "image";
};
service.command = mkOption {
type = nullOr types.unspecified;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "command";
};
service.container_name = mkOption {
type = nullOr types.str;
default = null;
description = dockerComposeRef "container_name";
};
service.depends_on = mkOption {
2019-03-04 00:12:01 +01:00
type = listOf str;
default = [];
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "depends_on";
};
2019-03-11 14:36:49 +01:00
service.devices = mkOption {
type = listOf str;
default = [];
description = ''
See ${link "https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities"
2019-10-29 12:52:00 +01:00
"`docker run --device` documentation"}
2019-03-11 14:36:49 +01:00
${dockerComposeRef "devices"}
'';
};
2021-01-20 18:11:52 +01:00
service.labels = mkOption {
type = attrsOf str;
default = {};
2021-01-22 10:35:57 +01:00
example = {
"com.example.foo" = "bar";
"traefik.enable" = "true";
"traefik.http.routers.my-service.rule" = "Host(`my-service.localhost`)";
"traefik.http.routers.my-service.entrypoints" = "web";
};
2021-01-20 18:11:52 +01:00
description = dockerComposeRef "labels";
};
service.links = mkOption {
2019-03-04 00:12:01 +01:00
type = listOf str;
default = [];
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "links";
};
service.external_links = mkOption {
2019-03-04 00:12:01 +01:00
type = listOf str;
default = [];
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "external_links";
};
service.extra_hosts = mkOption {
2019-03-04 00:12:01 +01:00
type = listOf str;
default = [];
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "extra_hosts";
};
service.working_dir = mkOption {
2019-03-04 00:12:01 +01:00
type = nullOr str;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeKitchenSink;
};
service.privileged = mkOption {
type = nullOr bool;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeKitchenSink;
};
service.entrypoint = mkOption {
2019-03-04 00:12:01 +01:00
type = nullOr str;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "entrypoint";
};
service.restart = mkOption {
2019-03-04 00:12:01 +01:00
type = nullOr str;
default = null;
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "restart";
};
2019-05-07 14:14:49 +07:00
service.user = mkOption {
type = nullOr str;
default = null;
description = dockerComposeKitchenSink;
};
service.ports = mkOption {
type = listOf types.unspecified;
default = [];
description = ''
Expose ports on host. "host:container" or structured.
2019-03-03 23:42:40 +01:00
${dockerComposeRef "ports"}
'';
};
service.expose = mkOption {
2019-03-04 00:12:01 +01:00
type = listOf str;
default = [];
2019-03-03 23:42:40 +01:00
description = dockerComposeRef "expose";
};
service.env_file = mkOption {
type = listOf str;
default = [];
description = dockerComposeRef "env_file";
};
service.network_mode = mkOption {
type = nullOr str;
default = null;
description = dockerComposeRef "network_mode";
};
service.networks = mkOption {
type = nullOr (listOf types.str);
default = null;
description = dockerComposeRef "networks";
};
service.stop_signal = mkOption {
type = nullOr str;
default = null;
description = dockerComposeRef "stop_signal";
};
2019-05-07 15:33:24 +07:00
service.sysctls = mkOption {
type = attrsOf (either str int);
default = {};
description = dockerComposeRef "sysctls";
};
2019-03-11 14:36:18 +01:00
service.capabilities = mkOption {
type = attrsOf (nullOr bool);
default = {};
example = { ALL = true; SYS_ADMIN = false; NET_ADMIN = false; };
description = ''
Enable/disable linux capabilities, or pick Docker's default.
2019-10-29 12:52:00 +01:00
Setting a capability to `true` means that it will be
"added". Setting it to `false` means that it will be "dropped".
2019-03-11 14:36:18 +01:00
${dockerComposeRef "cap_add-cap_drop"}
2019-10-29 12:52:00 +01:00
Omitted and `null` capabilities will therefore be set
2019-03-11 14:36:18 +01:00
according to Docker's ${
link "https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities"
"default list of capabilities."
}
'';
};
};
2019-10-03 21:30:14 +02:00
config.out.service = {
inherit (config.service)
volumes
environment
2019-05-07 15:33:24 +07:00
sysctls
image
;
} // lib.optionalAttrs (config.service.build.context != null) {
inherit (config.service) build;
2019-03-11 14:36:18 +01:00
} // lib.optionalAttrs (cap_add != []) {
inherit cap_add;
} // lib.optionalAttrs (cap_drop != []) {
inherit cap_drop;
} // lib.optionalAttrs (config.service.command != null) {
inherit (config.service) command;
} // lib.optionalAttrs (config.service.container_name != null) {
inherit (config.service) container_name;
} // lib.optionalAttrs (config.service.depends_on != []) {
inherit (config.service) depends_on;
2019-03-11 14:36:49 +01:00
} // lib.optionalAttrs (config.service.devices != []) {
inherit (config.service) devices;
} // lib.optionalAttrs (config.service.entrypoint != null) {
inherit (config.service) entrypoint;
} // lib.optionalAttrs (config.service.env_file != []) {
inherit (config.service) env_file;
} // lib.optionalAttrs (config.service.expose != []) {
inherit (config.service) expose;
} // lib.optionalAttrs (config.service.external_links != []) {
inherit (config.service) external_links;
} // lib.optionalAttrs (config.service.extra_hosts != []) {
inherit (config.service) extra_hosts;
} // lib.optionalAttrs (config.service.hostname != null) {
inherit (config.service) hostname;
2021-01-20 18:11:52 +01:00
} // lib.optionalAttrs (config.service.labels != {}) {
inherit (config.service) labels;
} // lib.optionalAttrs (config.service.links != []) {
inherit (config.service) links;
} // lib.optionalAttrs (config.service.ports != []) {
inherit (config.service) ports;
} // lib.optionalAttrs (config.service.privileged != null) {
inherit (config.service) privileged;
} // lib.optionalAttrs (config.service.network_mode != null) {
inherit (config.service) network_mode;
} // lib.optionalAttrs (config.service.networks != null) {
inherit (config.service) networks;
} // lib.optionalAttrs (config.service.restart != null) {
inherit (config.service) restart;
} // lib.optionalAttrs (config.service.stop_signal != null) {
inherit (config.service) stop_signal;
} // lib.optionalAttrs (config.service.tmpfs != []) {
inherit (config.service) tmpfs;
} // lib.optionalAttrs (config.service.tty != null) {
inherit (config.service) tty;
} // lib.optionalAttrs (config.service.working_dir != null) {
inherit (config.service) working_dir;
2019-05-07 14:14:49 +07:00
} // lib.optionalAttrs (config.service.user != null) {
inherit (config.service) user;
};
}