From 83a9d4668f978b910454f1efe4aa4a262c7dee40 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 3 Oct 2019 17:53:13 +0200 Subject: [PATCH] Use proper submodule for services --- doc/manual/.gitignore | 1 - doc/manual/Makefile | 6 ++-- doc/manual/default.nix | 22 ------------ doc/manual/manual.xml | 1 - src/haskell/exe/Main.hs | 4 +-- src/nix/eval-service.nix | 32 ----------------- .../modules/composition/arion-base-image.nix | 2 +- .../modules/composition/docker-compose.nix | 35 +++++++++++-------- src/nix/modules/composition/images.nix | 4 +-- src/nix/modules/composition/service-info.nix | 6 ++-- src/nix/modules/service/all-modules.nix | 10 ++++++ 11 files changed, 40 insertions(+), 83 deletions(-) delete mode 100644 src/nix/eval-service.nix create mode 100644 src/nix/modules/service/all-modules.nix diff --git a/doc/manual/.gitignore b/doc/manual/.gitignore index cfc155d..0c15756 100644 --- a/doc/manual/.gitignore +++ b/doc/manual/.gitignore @@ -1,3 +1,2 @@ manual.html options-composition.xml -options-service.xml diff --git a/doc/manual/Makefile b/doc/manual/Makefile index cc31da1..656c0ab 100644 --- a/doc/manual/Makefile +++ b/doc/manual/Makefile @@ -17,7 +17,7 @@ docbookxsl = http://docbook.sourceforge.net/release/xsl/current all: manual.html -manual.html: manual.xml options-composition.xml options-service.xml +manual.html: manual.xml options-composition.xml $(xsltproc) --xinclude --stringparam profile.condition manual \ $(docbookxsl)/profiling/profile.xsl manual.xml | \ $(xsltproc) --output manual.html $(docbookxsl)/xhtml/docbook.xsl - @@ -27,8 +27,8 @@ manual.html: manual.xml options-composition.xml options-service.xml asciidoctor --backend docbook45 --doctype article $< sed -e 's///' -i $@ -options-composition.xml options-service.xml: - echo "options-composition.xml and options-service.xml should be written by the derivation. Are you running in 'nix-shell -A manual'?"; exit 1; fi +options-composition.xml: + echo "options-composition.xml should be written by the derivation. Are you running in 'nix-shell -A manual'?"; exit 1; fi install: all mkdir -p $(docdir) diff --git a/doc/manual/default.nix b/doc/manual/default.nix index 03522ab..64b352b 100644 --- a/doc/manual/default.nix +++ b/doc/manual/default.nix @@ -58,26 +58,9 @@ let ''; }; - serviceOptions = options { - moduleType = "service"; - description = "List of Arion service-level options in JSON format"; - optionsExpr = let - src = ../../src/nix; - in '' - let pkgs = import ${pkgs.path} {}; - fixPaths = opt: opt // { - declarations = map (d: "src/nix" + (lib.strings.removePrefix (toString ${src}) (toString d))) opt.declarations; - }; - inherit (pkgs) lib; - composition = pkgs.callPackage ${src}/eval-service.nix {} { modules = []; host = {}; name = abort "The manual's service options section must not depend on the service name."; composition = abort "The manual's service options must not depend on the composition."; }; - in map fixPaths (lib.filter (opt: opt.visible && !opt.internal) (lib.optionAttrSetToDocList composition.options)) - ''; - }; - generatedDocBook = runCommand "generated-docbook" {} '' mkdir $out ln -s ${compositionOptions.optionsDocBook} $out/options-composition.xml - ln -s ${serviceOptions.optionsDocBook} $out/options-service.xml ''; manual = stdenv.mkDerivation { @@ -105,11 +88,6 @@ let ''; prePatch = '' cp ${generatedDocBook}/* . - substituteInPlace options-service.xml \ - --replace 'xml:id="appendix-configuration-options"' 'xml:id="appendix-service-options"' \ - --replace 'Configuration Options' 'Service Options' \ - --replace 'xml:id="configuration-variable-list"' 'xml:id="service-variable-list"' \ - ; substituteInPlace options-composition.xml \ --replace 'xml:id="appendix-configuration-options"' 'xml:id="appendix-composition-options"' \ --replace 'Configuration Options' 'Composition Options' \ diff --git a/doc/manual/manual.xml b/doc/manual/manual.xml index bce99c2..87d9616 100644 --- a/doc/manual/manual.xml +++ b/doc/manual/manual.xml @@ -17,6 +17,5 @@ - diff --git a/src/haskell/exe/Main.hs b/src/haskell/exe/Main.hs index 21ec3d3..57fcada 100644 --- a/src/haskell/exe/Main.hs +++ b/src/haskell/exe/Main.hs @@ -183,9 +183,7 @@ runRepl co = do "Launching a repl for you. To get started:\n\ \\n\ \To see deployment-wide configuration\n\ - \ type config. and hit TAB\n\ - \To see the services\n\ - \ type config.docker-compose.evaluatedServices TAB or ENTER\n\ + \ type config. and use tab completion\n\ \To bring the top-level Nixpkgs attributes into scope\n\ \ type :a (config._module.args.pkgs) // { inherit config; }\n\ \" diff --git a/src/nix/eval-service.nix b/src/nix/eval-service.nix deleted file mode 100644 index 2ab731b..0000000 --- a/src/nix/eval-service.nix +++ /dev/null @@ -1,32 +0,0 @@ -{ lib, pkgs, ... }: - -{ modules, host, name, composition }: -let - composite = lib.evalModules { - check = true; - modules = builtinModules ++ modules; - }; - - builtinModules = [ - argsModule - ./modules/service/default-exec.nix - ./modules/service/docker-compose-service.nix - ./modules/service/extended-info.nix - ./modules/service/host-store.nix - ./modules/service/context.nix - ./modules/service/image.nix - ./modules/service/nixos.nix - ./modules/service/nixos-init.nix - ]; - - argsModule = { - _file = ./eval-service.nix; - key = ./eval-service.nix; - config._module.args.pkgs = lib.mkForce pkgs; - config.host = host; - config.service.name = name; - config.composition = composition; - }; - -in - composite diff --git a/src/nix/modules/composition/arion-base-image.nix b/src/nix/modules/composition/arion-base-image.nix index fad7b6c..2be1d97 100644 --- a/src/nix/modules/composition/arion-base-image.nix +++ b/src/nix/modules/composition/arion-base-image.nix @@ -34,7 +34,7 @@ in config = { arionBaseImage = "${name}:${tag}"; - build.imagesToLoad = lib.mkIf (lib.any (s: s.config.service.useHostStore) (lib.attrValues config.docker-compose.evaluatedServices)) [ + build.imagesToLoad = lib.mkIf (lib.any (s: s.service.useHostStore) (lib.attrValues config.docker-compose.services)) [ { image = builtImage; imageName = name; imageTag = tag; } ]; }; diff --git a/src/nix/modules/composition/docker-compose.nix b/src/nix/modules/composition/docker-compose.nix index 1891bbf..577fbb8 100644 --- a/src/nix/modules/composition/docker-compose.nix +++ b/src/nix/modules/composition/docker-compose.nix @@ -9,13 +9,25 @@ - docker-compose.services */ -{ pkgs, lib, config, ... }: +compositionArgs@{ lib, config, options, pkgs, ... }: let - evalService = name: modules: pkgs.callPackage ../../eval-service.nix {} { - inherit name modules; - inherit (config) host; - composition = config; + inherit (lib) types; + + service = { + imports = [ argsModule ] ++ import ../service/all-modules.nix; }; + argsModule = + { name, # injected by types.submodule + ... + }: { + _file = ./docker-compose.nix; + key = ./docker-compose.nix; + + config._module.args.pkgs = lib.mkDefault compositionArgs.pkgs; + config.host = compositionArgs.config.host; + config.composition = compositionArgs.config; + config.service.name = name; + }; in { @@ -44,14 +56,8 @@ in description = "Attribute set that will be turned into the x-arion section of the docker-compose.yaml file."; }; docker-compose.services = lib.mkOption { - default = {}; - type = with lib.types; attrsOf (coercedTo unspecified (a: [a]) (listOf unspecified)); - description = "A attribute set of service configurations. A service specifies how to run an image. Each of these service configurations is specified using modules whose options are described in the Service Options section."; - }; - docker-compose.evaluatedServices = lib.mkOption { - type = lib.types.attrsOf lib.types.attrs; - description = "Attribute set of evaluated service configurations."; - readOnly = true; + type = lib.types.attrsOf (lib.types.submodule service); + description = "An attribute set of service configurations. A service specifies how to run an image as a container."; }; }; config = { @@ -59,10 +65,9 @@ in build.dockerComposeYamlText = builtins.toJSON (config.build.dockerComposeYamlAttrs); build.dockerComposeYamlAttrs = config.docker-compose.raw; - docker-compose.evaluatedServices = lib.mapAttrs evalService config.docker-compose.services; docker-compose.raw = { version = "3.4"; - services = lib.mapAttrs (k: c: c.config.build.service) config.docker-compose.evaluatedServices; + services = lib.mapAttrs (k: c: c.build.service) config.docker-compose.services; x-arion = config.docker-compose.extended; }; }; diff --git a/src/nix/modules/composition/images.nix b/src/nix/modules/composition/images.nix index bedf8e8..bb505da 100644 --- a/src/nix/modules/composition/images.nix +++ b/src/nix/modules/composition/images.nix @@ -4,12 +4,12 @@ let serviceImages = lib.mapAttrs addDetails ( - lib.filterAttrs filterFunction config.docker-compose.evaluatedServices + lib.filterAttrs filterFunction config.docker-compose.services ); filterFunction = serviceName: service: builtins.addErrorContext "while evaluating whether the service ${serviceName} defines an image" - service.config.image.nixBuild; + service.image.nixBuild; addDetails = serviceName: service: builtins.addErrorContext "while evaluating the image for service ${serviceName}" diff --git a/src/nix/modules/composition/service-info.nix b/src/nix/modules/composition/service-info.nix index 1ed2989..10acb06 100644 --- a/src/nix/modules/composition/service-info.nix +++ b/src/nix/modules/composition/service-info.nix @@ -7,14 +7,14 @@ let serviceInfo = lib.mapAttrs getInfo ( - lib.filterAttrs filterFunction config.docker-compose.evaluatedServices + lib.filterAttrs filterFunction config.docker-compose.services ); filterFunction = _serviceName: service: # shallow equality suffices for emptiness test - builtins.attrNames service.config.build.extendedInfo != []; + builtins.attrNames service.build.extendedInfo != []; - getInfo = _serviceName: service: service.config.build.extendedInfo; + getInfo = _serviceName: service: service.build.extendedInfo; in { diff --git a/src/nix/modules/service/all-modules.nix b/src/nix/modules/service/all-modules.nix new file mode 100644 index 0000000..bfbb13c --- /dev/null +++ b/src/nix/modules/service/all-modules.nix @@ -0,0 +1,10 @@ +[ + ./default-exec.nix + ./docker-compose-service.nix + ./extended-info.nix + ./host-store.nix + ./context.nix + ./image.nix + ./nixos.nix + ./nixos-init.nix +]