From 7c923dbf4cc43db6f171bd21f435fbec7b0f2684 Mon Sep 17 00:00:00 2001 From: edef Date: Thu, 4 Apr 2024 14:05:18 +0000 Subject: [PATCH] lib: add xor This gets clumsily reimplemented in various places, to no useful end. --- lib/default.nix | 2 +- lib/tests/misc.nix | 11 +++++++++++ lib/trivial.nix | 18 ++++++++++++++++++ nixos/modules/config/users-groups.nix | 1 - nixos/modules/services/networking/kea.nix | 1 - pkgs/development/compilers/llvm/13/default.nix | 7 ++----- pkgs/development/compilers/llvm/14/default.nix | 7 ++----- pkgs/development/compilers/llvm/15/default.nix | 7 ++----- pkgs/development/compilers/llvm/16/default.nix | 7 ++----- pkgs/development/compilers/llvm/17/default.nix | 7 ++----- pkgs/development/compilers/llvm/18/default.nix | 7 ++----- .../development/compilers/llvm/git/default.nix | 7 ++----- 12 files changed, 44 insertions(+), 38 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index f6cb7932507a9c9..b442ddf5fa0f05b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -69,7 +69,7 @@ let hasAttr head isAttrs isBool isInt isList isPath isString length lessThan listToAttrs pathExists readFile replaceStrings seq stringLength sub substring tail trace; - inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor + inherit (self.trivial) id const pipe concat or and xor bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum info showWarnings nixpkgsVersion version isInOldestRelease diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 3cb96c1b68bc2a4..b475b15eb4c67e6 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -102,6 +102,7 @@ let types updateManyAttrsByPath versions + xor ; testingThrow = expr: { @@ -210,6 +211,16 @@ runTests { expected = false; }; + testXorTrue = { + expr = xor true false; + expected = true; + }; + + testXorFalse = { + expr = xor true true; + expected = false; + }; + testFix = { expr = fix (x: {a = if x ? a then "a" else "b";}); expected = {a = "a";}; diff --git a/lib/trivial.nix b/lib/trivial.nix index 936ad207c03df55..5b7a1ee30f7ad31 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -199,6 +199,24 @@ in { */ and = x: y: x && y; + /** + boolean “exclusive or” + + + # Inputs + + `x` + + : 1\. Function argument + + `y` + + : 2\. Function argument + */ + # We explicitly invert the arguments purely as a type assertion. + # This is invariant under XOR, so it does not affect the result. + xor = x: y: (!x) != (!y); + /** bitwise “not” */ diff --git a/nixos/modules/config/users-groups.nix b/nixos/modules/config/users-groups.nix index f9750b7263cacc3..07d9591cb446b48 100644 --- a/nixos/modules/config/users-groups.nix +++ b/nixos/modules/config/users-groups.nix @@ -871,7 +871,6 @@ in { } { assertion = let - xor = a: b: a && !b || b && !a; isEffectivelySystemUser = user.isSystemUser || (user.uid != null && user.uid < 1000); in xor isEffectivelySystemUser user.isNormalUser; message = '' diff --git a/nixos/modules/services/networking/kea.nix b/nixos/modules/services/networking/kea.nix index 656ddd41fd12b1b..5fd6427c90f86b6 100644 --- a/nixos/modules/services/networking/kea.nix +++ b/nixos/modules/services/networking/kea.nix @@ -9,7 +9,6 @@ with lib; let cfg = config.services.kea; - xor = x: y: (!x && y) || (x && !y); format = pkgs.formats.json {}; chooseNotNull = x: y: if x != null then x else y; diff --git a/pkgs/development/compilers/llvm/13/default.nix b/pkgs/development/compilers/llvm/13/default.nix index cd96231e62c35d8..429dfe5f1f3eead 100644 --- a/pkgs/development/compilers/llvm/13/default.nix +++ b/pkgs/development/compilers/llvm/13/default.nix @@ -42,12 +42,9 @@ }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/14/default.nix b/pkgs/development/compilers/llvm/14/default.nix index 66f5c7385a1501a..6dcdcfa92584095 100644 --- a/pkgs/development/compilers/llvm/14/default.nix +++ b/pkgs/development/compilers/llvm/14/default.nix @@ -41,12 +41,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/15/default.nix b/pkgs/development/compilers/llvm/15/default.nix index 0ccb9c449a8da05..dcafd2970d00559 100644 --- a/pkgs/development/compilers/llvm/15/default.nix +++ b/pkgs/development/compilers/llvm/15/default.nix @@ -41,12 +41,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/16/default.nix b/pkgs/development/compilers/llvm/16/default.nix index 66185071f22b772..46d17c193fc9211 100644 --- a/pkgs/development/compilers/llvm/16/default.nix +++ b/pkgs/development/compilers/llvm/16/default.nix @@ -41,12 +41,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/17/default.nix b/pkgs/development/compilers/llvm/17/default.nix index c8d9569253450de..fb3e967cc39b8ac 100644 --- a/pkgs/development/compilers/llvm/17/default.nix +++ b/pkgs/development/compilers/llvm/17/default.nix @@ -41,12 +41,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/18/default.nix b/pkgs/development/compilers/llvm/18/default.nix index dd3000c04259484..ae1f4399cf2ed55 100644 --- a/pkgs/development/compilers/llvm/18/default.nix +++ b/pkgs/development/compilers/llvm/18/default.nix @@ -41,12 +41,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" + diff --git a/pkgs/development/compilers/llvm/git/default.nix b/pkgs/development/compilers/llvm/git/default.nix index 80cff85372d067c..bd21a75b8c2fc74 100644 --- a/pkgs/development/compilers/llvm/git/default.nix +++ b/pkgs/development/compilers/llvm/git/default.nix @@ -46,12 +46,9 @@ , monorepoSrc ? null }: -assert let - int = a: if a then 1 else 0; - xor = a: b: ((builtins.bitXor (int a) (int b)) == 1); -in +assert lib.assertMsg - (xor + (lib.xor (gitRelease != null) (officialRelease != null)) ("must specify `gitRelease` or `officialRelease`" +