Skip to content

Commit

Permalink
lib: add xor
Browse files Browse the repository at this point in the history
This gets clumsily reimplemented in various places, to no useful end.
  • Loading branch information
edef1c committed Apr 4, 2024
1 parent d03a448 commit 7c923db
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ let
types
updateManyAttrsByPath
versions
xor
;

testingThrow = expr: {
Expand Down Expand Up @@ -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";};
Expand Down
18 changes: 18 additions & 0 deletions lib/trivial.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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”
*/
Expand Down
1 change: 0 additions & 1 deletion nixos/modules/config/users-groups.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
1 change: 0 additions & 1 deletion nixos/modules/services/networking/kea.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/13/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/14/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/15/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/16/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/17/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/18/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down
7 changes: 2 additions & 5 deletions pkgs/development/compilers/llvm/git/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`" +
Expand Down

0 comments on commit 7c923db

Please sign in to comment.