From 38048ada2c4bcc70edb811992c879ee46de88068 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Jan 2021 18:11:52 +0100 Subject: [PATCH 01/10] Add service.labels --- src/nix/modules/service/docker-compose-service.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/nix/modules/service/docker-compose-service.nix b/src/nix/modules/service/docker-compose-service.nix index 42cd89f..5082eaf 100644 --- a/src/nix/modules/service/docker-compose-service.nix +++ b/src/nix/modules/service/docker-compose-service.nix @@ -115,6 +115,11 @@ in ${dockerComposeRef "devices"} ''; }; + service.labels = mkOption { + type = attrsOf str; + default = {}; + description = dockerComposeRef "labels"; + }; service.links = mkOption { type = listOf str; default = []; @@ -247,6 +252,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 != []) { From 35cb7adfb50f1cd09c70cb234632228861856f17 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Jan 2021 18:16:09 +0100 Subject: [PATCH 02/10] service.labels: Bad example --- src/nix/modules/service/docker-compose-service.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nix/modules/service/docker-compose-service.nix b/src/nix/modules/service/docker-compose-service.nix index 5082eaf..ce20bcb 100644 --- a/src/nix/modules/service/docker-compose-service.nix +++ b/src/nix/modules/service/docker-compose-service.nix @@ -118,6 +118,7 @@ in service.labels = mkOption { type = attrsOf str; default = {}; + example = { "com.example.foo" = "bar"; }; description = dockerComposeRef "labels"; }; service.links = mkOption { From 9a523b45d79310ac47550a96747a05c0f4818da3 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 20 Jan 2021 18:14:37 +0100 Subject: [PATCH 03/10] ./update-options --- docs/modules/ROOT/partials/NixOSOptions.adoc | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/modules/ROOT/partials/NixOSOptions.adoc b/docs/modules/ROOT/partials/NixOSOptions.adoc index 34f4bfb..f566cbd 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"} +---- + + == services..service.links See link:https://docs.docker.com/compose/compose-file/#links[Docker Compose#links] From ee2f71327d05993137cf14ef1a52a0797554769d Mon Sep 17 00:00:00 2001 From: lunik1 Date: Thu, 21 Jan 2021 23:07:52 +0100 Subject: [PATCH 04/10] Add traefik example --- examples/traefik/arion-compose.nix | 28 ++++++++++++++++++++++++++++ examples/traefik/arion-pkgs.nix | 6 ++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/traefik/arion-compose.nix create mode 100644 examples/traefik/arion-pkgs.nix diff --git a/examples/traefik/arion-compose.nix b/examples/traefik/arion-compose.nix new file mode 100644 index 0000000..03cacb6 --- /dev/null +++ b/examples/traefik/arion-compose.nix @@ -0,0 +1,28 @@ +{ pkgs, ... }: { + + config.services = { + traefik.service = { + image = "traefik:v2.4"; + container_name = "traefik"; + command = [ + "--api.insecure=true" + "--providers.docker=true" + "--providers.docker.exposedbydefault=false" + "--entrypoints.web.address=:80" + ]; + ports = [ "80:80" "8080:8080" ]; + volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ]; + }; + + whoami.service = { + image = "traefik/whoami"; + container_name = "simple-service"; + labels = { + "traefik.enable" = "true"; + "traefik.http.routers.whoami.rule" = "Host(`whoami.localhost`)"; + "traefik.http.routers.whoami.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"; +} From 32b00781b43f000db745ab14e7c7ead616bd123c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 21 Jan 2021 23:43:03 +0100 Subject: [PATCH 05/10] examples/traefik: Make pure --- examples/traefik/arion-compose.nix | 47 +++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/examples/traefik/arion-compose.nix b/examples/traefik/arion-compose.nix index 03cacb6..b6c6f7d 100644 --- a/examples/traefik/arion-compose.nix +++ b/examples/traefik/arion-compose.nix @@ -1,26 +1,47 @@ -{ pkgs, ... }: { +/* + + 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.service = { - image = "traefik:v2.4"; - container_name = "traefik"; - command = [ + traefik = { + image.command = [ + "${pkgs.traefik}/bin/traefik" "--api.insecure=true" "--providers.docker=true" "--providers.docker.exposedbydefault=false" "--entrypoints.web.address=:80" ]; - ports = [ "80:80" "8080:8080" ]; - volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ]; + service = { + container_name = "traefik"; + stop_signal = "SIGINT"; + ports = [ "80:80" "8080:8080" ]; + volumes = [ "/var/run/docker.sock:/var/run/docker.sock:ro" ]; + }; }; - whoami.service = { - image = "traefik/whoami"; - container_name = "simple-service"; - labels = { + 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.whoami.rule" = "Host(`whoami.localhost`)"; - "traefik.http.routers.whoami.entrypoints" = "web"; + "traefik.http.routers.nix-docs.rule" = "Host(`nix-docs.localhost`)"; + "traefik.http.routers.nix-docs.entrypoints" = "web"; }; }; }; From cfa65c56a6e530625acc55cd2b780d228dc4f0e1 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 21 Jan 2021 23:53:37 +0100 Subject: [PATCH 06/10] Test examples/traefik --- tests/arion-test/default.nix | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/arion-test/default.nix b/tests/arion-test/default.nix index ad4eebc..5aeb65f 100644 --- a/tests/arion-test/default.nix +++ b/tests/arion-test/default.nix @@ -33,6 +33,7 @@ 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 ]; @@ -92,5 +93,19 @@ in "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" ) machine.wait_until_fails("curl 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 nix-docs.localhost") + machine.succeed( + "cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down" + ) + machine.wait_until_fails("curl nix-docs.localhost") + ''; } From 3bad85064bd1ec0f1c2b1a37d82a96513b89efc2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 21 Jan 2021 23:54:53 +0100 Subject: [PATCH 07/10] Tests: curl --fail --- tests/arion-test/default.nix | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/arion-test/default.nix b/tests/arion-test/default.nix index 5aeb65f..cd32b35 100644 --- a/tests/arion-test/default.nix +++ b/tests/arion-test/default.nix @@ -40,7 +40,7 @@ in virtualisation.memorySize = 1024; }; testScript = '' - machine.fail("curl localhost:8000") + machine.fail("curl --fail localhost:8000") machine.succeed("docker --version") # Tests @@ -51,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 @@ -64,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( """ @@ -80,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 @@ -88,11 +88,11 @@ 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 @@ -101,11 +101,10 @@ in 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 nix-docs.localhost") + 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 nix-docs.localhost") - + machine.wait_until_fails("curl --fail nix-docs.localhost") ''; } From 648230492d79777b192af716c2b5d9e0fb5b44c2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 22 Jan 2021 10:24:36 +0100 Subject: [PATCH 08/10] Remove obsolete Nixpkgs versions Traefik test fails on NixOS 20.03, which is end-of-life. --- nix/ci.nix | 12 +----------- nix/sources.json | 24 ------------------------ 2 files changed, 1 insertion(+), 35 deletions(-) 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", From 3d9f19b630635803482dded083243461b2d486bb Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 22 Jan 2021 10:32:14 +0100 Subject: [PATCH 09/10] Update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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`. From bb23a55c8ac3016a9e9f2c47192590937c76d2b8 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 22 Jan 2021 10:35:57 +0100 Subject: [PATCH 10/10] Improve labels example --- docs/modules/ROOT/partials/NixOSOptions.adoc | 2 +- src/nix/modules/service/docker-compose-service.nix | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/modules/ROOT/partials/NixOSOptions.adoc b/docs/modules/ROOT/partials/NixOSOptions.adoc index f566cbd..2a97cbb 100644 --- a/docs/modules/ROOT/partials/NixOSOptions.adoc +++ b/docs/modules/ROOT/partials/NixOSOptions.adoc @@ -694,7 +694,7 @@ Default:: Example:: + ---- -{"com.example.foo":"bar"} +{"com.example.foo":"bar","traefik.enable":"true","traefik.http.routers.my-service.entrypoints":"web","traefik.http.routers.my-service.rule":"Host(`my-service.localhost`)"} ---- diff --git a/src/nix/modules/service/docker-compose-service.nix b/src/nix/modules/service/docker-compose-service.nix index ce20bcb..4da0ab3 100644 --- a/src/nix/modules/service/docker-compose-service.nix +++ b/src/nix/modules/service/docker-compose-service.nix @@ -118,7 +118,12 @@ in service.labels = mkOption { type = attrsOf str; default = {}; - example = { "com.example.foo" = "bar"; }; + 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 {