diff --git a/CHANGELOG.md b/CHANGELOG.md index a0ba058..9947529 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Revision history for Arion +## Next + +* Support `service.labels`, which is useful for autodiscovery among other things. + +* Add a tested example for Traefik with label-based routing. + +* Drop obsolete NixOS 19.09 and 20.03 support. It may still be usable there. + ## 0.1.2.0 -- 2020-03-05 * Support use of prebuilt `docker-compose.yaml`. diff --git a/docs/modules/ROOT/partials/NixOSOptions.adoc b/docs/modules/ROOT/partials/NixOSOptions.adoc index 34f4bfb..2a97cbb 100644 --- a/docs/modules/ROOT/partials/NixOSOptions.adoc +++ b/docs/modules/ROOT/partials/NixOSOptions.adoc @@ -676,6 +676,28 @@ No Default:: {blank} No Example:: {blank} +== services..service.labels + +See link:https://docs.docker.com/compose/compose-file/#labels[Docker Compose#labels] + +[discrete] +=== details + +Type:: attribute set of strings +Default:: ++ +---- +{} +---- + + +Example:: ++ +---- +{"com.example.foo":"bar","traefik.enable":"true","traefik.http.routers.my-service.entrypoints":"web","traefik.http.routers.my-service.rule":"Host(`my-service.localhost`)"} +---- + + == services..service.links See link:https://docs.docker.com/compose/compose-file/#links[Docker Compose#links] diff --git a/examples/traefik/arion-compose.nix b/examples/traefik/arion-compose.nix new file mode 100644 index 0000000..b6c6f7d --- /dev/null +++ b/examples/traefik/arion-compose.nix @@ -0,0 +1,49 @@ +/* + + An example of + - traefik HTTP reverse proxy + - minimal images + - routing via docker labels + + Run `arion up -d` and open http://nix-docs.localhost/ + + */ +{ lib, pkgs, ... }: { + + config.services = { + traefik = { + image.command = [ + "${pkgs.traefik}/bin/traefik" + "--api.insecure=true" + "--providers.docker=true" + "--providers.docker.exposedbydefault=false" + "--entrypoints.web.address=:80" + ]; + service = { + container_name = "traefik"; + stop_signal = "SIGINT"; + ports = [ "80:80" "8080:8080" ]; + volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ]; + }; + }; + + nix-docs = { + image.command = ["${pkgs.writeScript "entrypoint" '' + #!${pkgs.bash}/bin/bash + cd ${pkgs.nix.doc}/share/doc/nix/manual + ${pkgs.python3}/bin/python -m http.server + ''}"]; + service.container_name = "simple-service"; + service.ports = [ + "8000:8000" # host:container + ]; + service.stop_signal = "SIGINT"; + service.labels = { + "traefik.enable" = "true"; + "traefik.http.routers.nix-docs.rule" = "Host(`nix-docs.localhost`)"; + "traefik.http.routers.nix-docs.entrypoints" = "web"; + }; + }; + }; +} + diff --git a/examples/traefik/arion-pkgs.nix b/examples/traefik/arion-pkgs.nix new file mode 100644 index 0000000..69aad13 --- /dev/null +++ b/examples/traefik/arion-pkgs.nix @@ -0,0 +1,6 @@ +# Instead of pinning Nixpkgs, we can opt to use the one in NIX_PATH +import { + # We specify the architecture explicitly. Use a Linux remote builder when + # calling arion from other platforms. + system = "x86_64-linux"; +} diff --git a/nix/ci.nix b/nix/ci.nix index 183f8b3..4726fd9 100644 --- a/nix/ci.nix +++ b/nix/ci.nix @@ -1,6 +1,6 @@ let sources = import ./sources.nix; - lib = import (sources."nixos-20.03" + "/lib"); + lib = import (sources."nixos-unstable" + "/lib"); inherit (import (sources."project.nix" + "/lib/dimension.nix") { inherit lib; }) dimension; in @@ -14,16 +14,6 @@ dimension "Nixpkgs version" { enableDoc = false; nixosTestIsPerl = true; }; - "nixos-19_09" = { - nixpkgsSource = "nixos-19.09"; - enableDoc = false; - nixosTestIsPerl = true; - }; - "nixos-20_03" = { - nixpkgsSource = "nixos-20.03"; - isReferenceNixpkgs = false; - enableDoc = true; - }; "nixos-20_09" = { nixpkgsSource = "nixos-20.09"; isReferenceNixpkgs = true; diff --git a/nix/sources.json b/nix/sources.json index 067c7b0..47e0e6c 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -23,30 +23,6 @@ "url": "https://github.com/NixOS/nixpkgs-channels/archive/34c7eb7545d155cc5b6f499b23a7cb1c96ab4d59.tar.gz", "url_template": "https://github.com///archive/.tar.gz" }, - "nixos-19.09": { - "branch": "nixos-19.09", - "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to", - "homepage": "https://github.com/NixOS/nixpkgs", - "owner": "NixOS", - "repo": "nixpkgs-channels", - "rev": "289466dd6a11c65a7de4a954d6ebf66c1ad07652", - "sha256": "0r5ja052s86fr54fm1zlhld3fwawz2w1d1gd6vbvpjrpjfyajibn", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs-channels/archive/289466dd6a11c65a7de4a954d6ebf66c1ad07652.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "nixos-20.03": { - "branch": "release-20.03", - "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to", - "homepage": "https://github.com/NixOS/nixpkgs", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "b4db68ff563895eea6aab4ff24fa04ef403dfe14", - "sha256": "1qbs7p0mmcmpg70ibd437hl57byqx5q0pc61p1dckrkazj7kq0pc", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/b4db68ff563895eea6aab4ff24fa04ef403dfe14.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, "nixos-20.09": { "branch": "nixos-20.09", "description": "Nix Packages collection", diff --git a/src/nix/modules/service/docker-compose-service.nix b/src/nix/modules/service/docker-compose-service.nix index 42cd89f..4da0ab3 100644 --- a/src/nix/modules/service/docker-compose-service.nix +++ b/src/nix/modules/service/docker-compose-service.nix @@ -115,6 +115,17 @@ in ${dockerComposeRef "devices"} ''; }; + service.labels = mkOption { + type = attrsOf str; + default = {}; + 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"; + }; + description = dockerComposeRef "labels"; + }; service.links = mkOption { type = listOf str; default = []; @@ -247,6 +258,8 @@ in inherit (config.service) extra_hosts; } // lib.optionalAttrs (config.service.hostname != null) { inherit (config.service) hostname; + } // lib.optionalAttrs (config.service.labels != {}) { + inherit (config.service) labels; } // lib.optionalAttrs (config.service.links != []) { inherit (config.service) links; } // lib.optionalAttrs (config.service.ports != []) { diff --git a/tests/arion-test/default.nix b/tests/arion-test/default.nix index ad4eebc..cd32b35 100644 --- a/tests/arion-test/default.nix +++ b/tests/arion-test/default.nix @@ -33,13 +33,14 @@ in (preEval [ ../../examples/minimal/arion-compose.nix ]).config.out.dockerComposeYaml (preEval [ ../../examples/full-nixos/arion-compose.nix ]).config.out.dockerComposeYaml (preEval [ ../../examples/nixos-unit/arion-compose.nix ]).config.out.dockerComposeYaml + (preEval [ ../../examples/traefik/arion-compose.nix ]).config.out.dockerComposeYaml pkgs.stdenv ]; virtualisation.memorySize = 1024; }; testScript = '' - machine.fail("curl localhost:8000") + machine.fail("curl --fail localhost:8000") machine.succeed("docker --version") # Tests @@ -50,11 +51,11 @@ in machine.succeed( "rm -rf work && cp -frT ${../../examples/minimal} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d" ) - machine.wait_until_succeeds("curl localhost:8000") + machine.wait_until_succeeds("curl --fail localhost:8000") machine.succeed( "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" ) - machine.wait_until_fails("curl localhost:8000") + machine.wait_until_fails("curl --fail localhost:8000") # Tests # - arion exec @@ -63,7 +64,7 @@ in machine.succeed( "rm -rf work && cp -frT ${../../examples/full-nixos} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d" ) - machine.wait_until_succeeds("curl localhost:8000") + machine.wait_until_succeeds("curl --fail localhost:8000") machine.succeed( """ @@ -79,7 +80,7 @@ in machine.succeed( "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" ) - machine.wait_until_fails("curl localhost:8000") + machine.wait_until_fails("curl --fail localhost:8000") # Tests # - examples/nixos-unit @@ -87,10 +88,23 @@ in machine.succeed( "rm -rf work && cp -frT ${../../examples/nixos-unit} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d" ) - machine.wait_until_succeeds("curl localhost:8000") + machine.wait_until_succeeds("curl --fail localhost:8000") machine.succeed( "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" ) - machine.wait_until_fails("curl localhost:8000") + machine.wait_until_fails("curl --fail localhost:8000") + + # Tests + # - examples/traefik + # - labels + with subtest("traefik"): + machine.succeed( + "rm -rf work && cp -frT ${../../examples/traefik} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d" + ) + machine.wait_until_succeeds("curl --fail nix-docs.localhost") + machine.succeed( + "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" + ) + machine.wait_until_fails("curl --fail nix-docs.localhost") ''; }