about summary refs log tree commit diff
path: root/lib/tests/systems.nix
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2023-06-22 00:18:31 -0700
committerAdam Joseph <adam@westernsemico.com>2023-06-22 00:18:33 -0700
commit00a749a3a641799c1732d1a2a6ec81c18d63eaa8 (patch)
tree739dddf70339bb58b6c4a568bca81ae3af423be7 /lib/tests/systems.nix
parent66ee7de7179f8237694cd5195f4f75f2f625b425 (diff)
lib/system: move toLosslessStringMaybe into lib/tests
toLosslessStringMaybe is not used by anything other than lib/tests,
so it can be private to that file.

I don't think this function was terribly well thought-through.  If
people start using it, we will become permanently dependent on the
ability to test platforms for equality.  It also makes the
elaboration process more fragile, because it encourages code outside
of nixpkgs to become sensitive to the minute details of how
elaboration happens.
Diffstat (limited to 'lib/tests/systems.nix')
-rw-r--r--lib/tests/systems.nix20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/tests/systems.nix b/lib/tests/systems.nix
index 862496313e902..fe4657c9dee62 100644
--- a/lib/tests/systems.nix
+++ b/lib/tests/systems.nix
@@ -9,6 +9,22 @@ let
     expr     = lib.sort lib.lessThan x;
     expected = lib.sort lib.lessThan y;
   };
+
+  /*
+    Try to convert an elaborated system back to a simple string. If not possible,
+    return null. So we have the property:
+
+        sys: _valid_ sys ->
+          sys == elaborate (toLosslessStringMaybe sys)
+
+    NOTE: This property is not guaranteed when `sys` was elaborated by a different
+          version of Nixpkgs.
+  */
+  toLosslessStringMaybe = sys:
+    if lib.isString sys then sys
+    else if lib.systems.equals sys (lib.systems.elaborate sys.system) then sys.system
+    else null;
+
 in
 lib.runTests (
 # We assert that the new algorithmic way of generating these lists matches the
@@ -55,11 +71,11 @@ lib.runTests (
   };
 
   test_toLosslessStringMaybe_example_x86_64-linux = {
-    expr = lib.systems.toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux");
+    expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux");
     expected = "x86_64-linux";
   };
   test_toLosslessStringMaybe_fail = {
-    expr = lib.systems.toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; });
+    expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; });
     expected = null;
   };
 }