about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2023-12-10 06:03:50 +0100
committerGitHub <noreply@github.com>2023-12-10 06:03:50 +0100
commit584463c7449a116f095eb456116f728c2b31497b (patch)
treece404cce204858d19763089cbddd98002c677d5f /lib
parentb9cb1d8d5f87cbd4c9316ca5a513d107fb6ea593 (diff)
parent8d3978c149352de6b7e8b72946b58a16427eda2c (diff)
downloadnixlib-584463c7449a116f095eb456116f728c2b31497b.tar
nixlib-584463c7449a116f095eb456116f728c2b31497b.tar.gz
nixlib-584463c7449a116f095eb456116f728c2b31497b.tar.bz2
nixlib-584463c7449a116f095eb456116f728c2b31497b.tar.lz
nixlib-584463c7449a116f095eb456116f728c2b31497b.tar.xz
nixlib-584463c7449a116f095eb456116f728c2b31497b.tar.zst
nixlib-584463c7449a116f095eb456116f728c2b31497b.zip
Merge pull request #272764 from tweag/anyBool
lib.types.anyBool: init
Diffstat (limited to 'lib')
-rwxr-xr-xlib/tests/modules.sh6
-rw-r--r--lib/tests/modules/boolByOr.nix14
-rw-r--r--lib/types.nix16
3 files changed, 36 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index 21d4978a1160..0eb976c1f497 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -111,6 +111,12 @@ checkConfigError 'The option .* does not exist. Definition values:\n\s*- In .*'
 checkConfigError 'while evaluating a definition from `.*/define-enable-abort.nix' config.enable ./define-enable-abort.nix
 checkConfigError 'while evaluating the error message for definitions for .enable., which is an option that does not exist' config.enable ./define-enable-abort.nix
 
+# Check boolByOr type.
+checkConfigOutput '^false$' config.value.falseFalse ./boolByOr.nix
+checkConfigOutput '^true$' config.value.trueFalse ./boolByOr.nix
+checkConfigOutput '^true$' config.value.falseTrue ./boolByOr.nix
+checkConfigOutput '^true$' config.value.trueTrue ./boolByOr.nix
+
 checkConfigOutput '^1$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix
 checkConfigOutput '^2$' config.bare-submodule.deep ./declare-bare-submodule.nix ./declare-bare-submodule-deep-option.nix
 checkConfigOutput '^42$' config.bare-submodule.nested ./declare-bare-submodule.nix ./declare-bare-submodule-nested-option.nix ./declare-bare-submodule-deep-option.nix ./define-bare-submodule-values.nix
diff --git a/lib/tests/modules/boolByOr.nix b/lib/tests/modules/boolByOr.nix
new file mode 100644
index 000000000000..ff86e2dfc859
--- /dev/null
+++ b/lib/tests/modules/boolByOr.nix
@@ -0,0 +1,14 @@
+{ lib, ... }: {
+
+  options.value = lib.mkOption {
+    type = lib.types.lazyAttrsOf lib.types.boolByOr;
+  };
+
+  config.value = {
+    falseFalse = lib.mkMerge [ false false ];
+    trueFalse = lib.mkMerge [ true false ];
+    falseTrue = lib.mkMerge [ false true ];
+    trueTrue = lib.mkMerge [ true true ];
+  };
+}
+
diff --git a/lib/types.nix b/lib/types.nix
index 5ffbecda5db3..51e58eaa8ab5 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -275,6 +275,22 @@ rec {
       merge = mergeEqualOption;
     };
 
+    boolByOr = mkOptionType {
+      name = "boolByOr";
+      description = "boolean (merged using or)";
+      descriptionClass = "noun";
+      check = isBool;
+      merge = loc: defs:
+        foldl'
+          (result: def:
+            # Under the assumption that .check always runs before merge, we can assume that all defs.*.value
+            # have been forced, and therefore we assume we don't introduce order-dependent strictness here
+            result || def.value
+          )
+          false
+          defs;
+    };
+
     int = mkOptionType {
       name = "int";
       description = "signed integer";