summary refs log tree commit diff
path: root/modules/misc/assertions.nix
blob: 2b06bfbbc0349e92a74bca34f4c5e03db33b3441 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{ config, pkgs, ... }:

with pkgs.lib;

let

  failed = map (x: x.message) (filter (x: !x.assertion) config.assertions);

in

{

  options = {

    assertions = mkOption {
      default = [];
      example = [ { assertion = false; msg = "you can't enable this for that reason"; } ];
      merge = pkgs.lib.mergeListOption;
      description = ''
        This option allows modules to express conditions that must
        hold for the evaluation of the system configuration to
        succeed, along with associated error messages for the user.
      '';
    };

  };

  config = {

    # This option is evaluated always. Thus the assertions are checked as well. hacky!
    environment.systemPackages =
      if [] == failed then []
      else throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failed)}";

  };

}