From 15605e3c728feedec5c3568dd2a9458d0b120c44 Mon Sep 17 00:00:00 2001 From: Malte Voos Date: Fri, 13 May 2022 17:38:39 +0200 Subject: own assertions implementation --- module.nix | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'module.nix') diff --git a/module.nix b/module.nix index 574ae9b..f09fe3f 100644 --- a/module.nix +++ b/module.nix @@ -6,6 +6,8 @@ let ${text} ''; + # -------- implementation -------- # + cfg = config; logNames = flatten (mapAttrsToList (_: sv: optional (sv.log != null) sv.log) cfg.longruns); @@ -137,8 +139,35 @@ let compiledDatabase = pkgs.runCommand "s6-rc-compiled-database" { } '' ${pkgs.s6-rc}/bin/s6-rc-compile $out ${serviceSourceDir} ''; + + # -------- assertions -------- # + + assertions = [ + { + assertion = length (attrNames allDefinitionDirs) + == length (attrNames cfg.bundles) + + length (attrNames cfg.oneshots) + + length (attrNames cfg.longruns) + + length (attrNames logLongruns); + message = "no two services can have the same name, even if they are of different types"; + } + ] ++ mapAttrsToList + (name: def: { + assertion = def.producerFor == null || def.log == null; + message = "in `longruns.${name}`: `producerFor` and `log` are mutually exclusive"; + }) + cfg.longruns; + + failedAssertions = map (x: x.message) (filter (x: !x.assertion) assertions); + + checkAssertions = + if failedAssertions != [ ] + then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" + else true; in { + # -------- interface -------- # + options = with types; let mkNullableOption = { type, ... } @ o: @@ -271,23 +300,7 @@ in }; }; - config = { - assertions = [ - { - assertion = length (attrNames allDefinitionDirs) - == length (attrNames cfg.bundles) - + length (attrNames cfg.oneshots) - + length (attrNames cfg.longruns) - + length (attrNames logLongruns); - message = "no two services can have the same name, even if they are of different types"; - } - ] ++ mapAttrsToList - (name: def: { - assertion = def.producerFor == null || def.log == null; - message = "in `longruns.${name}`: `producerFor` and `log` are mutually exclusive"; - }) - cfg.longruns; - + config = lib.mkIf checkAssertions { inherit serviceSourceDir compiledDatabase; }; } -- cgit 1.4.1