From 9523df7eb600e7fc2a88bc5227d9dfe12055a9bd Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Wed, 2 Sep 2020 00:17:26 +0200 Subject: [PATCH] nixos/assertions: Use module-builtin assertion implementation --- lib/modules.nix | 12 +++++------ nixos/modules/misc/assertions.nix | 21 ++++++++++++++++++- nixos/modules/system/activation/top-level.nix | 10 +-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index 0d761c632d02..31200ae0b035 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -254,11 +254,11 @@ rec { */ injectAssertions = assertions: config: let # Partition into assertions that are triggered on this level and ones that aren't - parted = partition (a: length a.triggerPath == 0) assertions; + parted = lib.partition (a: length a.triggerPath == 0) assertions; # From the ones that are triggered, filter out ones that aren't enabled # and group into warnings/errors - byType = groupBy (a: a.type) (filter (a: a.enable) parted.right); + byType = lib.groupBy (a: a.type) (filter (a: a.enable) parted.right); # Triggers semantically are just lib.id, but they print warning cause errors in addition warningTrigger = value: lib.foldr (w: warn w.show) value (byType.warning or []); @@ -266,16 +266,16 @@ rec { if byType.error or [] == [] then value else throw '' Failed assertions: - ${concatMapStringsSep "\n" (a: "- ${a.show}") byType.error} + ${lib.concatMapStringsSep "\n" (a: "- ${a.show}") byType.error} ''; # Trigger for both warnings and errors trigger = value: warningTrigger (errorTrigger value); # From the non-triggered assertions, split off the first element of triggerPath # to get a mapping from nested attributes to a list of assertions for that attribute - nested = zipAttrs (map (a: { + nested = lib.zipAttrs (map (a: { ${head a.triggerPath} = a // { - triggerPath = tail a.triggerPath; + triggerPath = lib.tail a.triggerPath; }; }) parted.wrong); @@ -296,7 +296,7 @@ rec { # has a `show` attribute for how to show it if triggered assertions = mapAttrsToList (name: value: let id = - if hasPrefix "_" name then "" + if lib.hasPrefix "_" name then "" else "[${showOption prefix}${optionalString (prefix != []) "/"}${name}] "; in value // { show = "${id}${value.message}"; diff --git a/nixos/modules/misc/assertions.nix b/nixos/modules/misc/assertions.nix index 550b3ac97f6a..e931611247f2 100644 --- a/nixos/modules/misc/assertions.nix +++ b/nixos/modules/misc/assertions.nix @@ -1,4 +1,4 @@ -{ lib, ... }: +{ lib, config, ... }: with lib; @@ -29,6 +29,25 @@ with lib; ''; }; + _module.assertions = mkOption { + type = types.attrsOf (types.submodule { + triggerPath = mkDefault [ "system" "build" "toplevel" ]; + }); + }; + }; + + config._module.assertions = lib.listToAttrs (lib.imap1 (n: value: + let + name = "_${toString n}"; + isWarning = lib.isString value; + result = { + enable = if isWarning then true else ! value.assertion; + type = if isWarning then "warning" else "error"; + message = if isWarning then value else value.message; + }; + in nameValuePair name result + ) (config.assertions ++ config.warnings)); + # impl of assertions is in } diff --git a/nixos/modules/system/activation/top-level.nix b/nixos/modules/system/activation/top-level.nix index 03d7e7493230..17b62ad9569b 100644 --- a/nixos/modules/system/activation/top-level.nix +++ b/nixos/modules/system/activation/top-level.nix @@ -117,18 +117,10 @@ let perl = "${pkgs.perl}/bin/perl " + (concatMapStringsSep " " (lib: "-I${lib}/${pkgs.perl.libPrefix}") (with pkgs.perlPackages; [ FileSlurp NetDBus XMLParser XMLTwig ])); }; - # Handle assertions and warnings - - failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions); - - baseSystemAssertWarn = if failedAssertions != [] - then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" - else showWarnings config.warnings baseSystem; - # Replace runtime dependencies system = fold ({ oldDependency, newDependency }: drv: pkgs.replaceDependency { inherit oldDependency newDependency drv; } - ) baseSystemAssertWarn config.system.replaceRuntimeDependencies; + ) baseSystem config.system.replaceRuntimeDependencies; in