summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorVladimír Čunát <v@cunat.cz>2020-08-04 21:38:08 +0200
committerVladimír Čunát <v@cunat.cz>2020-08-04 21:38:08 +0200
commit01c2ba857590ca8d156682e478d297201b715ea2 (patch)
tree132d524d390bbdf5f08a3b3d9e251ea82177909f /lib
parentccedb29f4b0b3a02f28be0a06cae480da2be432b (diff)
parent8bbc202208d0a972e1a1a973f19b4ae0415bce4b (diff)
downloadnixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar.gz
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar.bz2
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar.lz
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar.xz
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.tar.zst
nixpkgs-01c2ba857590ca8d156682e478d297201b715ea2.zip
Merge branch 'staging-next' into staging
Diffstat (limited to 'lib')
-rw-r--r--lib/generators.nix6
-rw-r--r--lib/licenses.nix5
-rw-r--r--lib/strings.nix16
3 files changed, 25 insertions, 2 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index efe6ea6031d..abd237eb7d3 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -48,8 +48,10 @@ rec {
     else if isAttrs    v then err "attrsets" v
     # functions can’t be printed of course
     else if isFunction v then err "functions" v
-    # let’s not talk about floats. There is no sensible `toString` for them.
-    else if isFloat    v then err "floats" v
+    # Floats currently can't be converted to precise strings,
+    # condition warning on nix version once this isn't a problem anymore
+    # See https://github.com/NixOS/nix/pull/3480
+    else if isFloat    v then libStr.floatToString v
     else err "this value is" (toString v);
 
 
diff --git a/lib/licenses.nix b/lib/licenses.nix
index 2c6da3a865f..2f9fc04cb7c 100644
--- a/lib/licenses.nix
+++ b/lib/licenses.nix
@@ -110,6 +110,11 @@ lib.mapAttrs (n: v: v // { shortName = n; }) {
     fullName = ''BSD 4-clause "Original" or "Old" License'';
   };
 
+  bsdProtection = spdx {
+    spdxId = "BSD-Protection";
+    fullName = "BSD Protection License";
+  };
+
   bsl11 = {
     fullName = "Business Source License 1.1";
     url = "https://mariadb.com/bsl11";
diff --git a/lib/strings.nix b/lib/strings.nix
index 74e3eaa0722..0baa942355c 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -612,6 +612,22 @@ rec {
   */
   fixedWidthNumber = width: n: fixedWidthString width "0" (toString n);
 
+  /* Convert a float to a string, but emit a warning when precision is lost
+     during the conversion
+
+     Example:
+       floatToString 0.000001
+       => "0.000001"
+       floatToString 0.0000001
+       => trace: warning: Imprecise conversion from float to string 0.000000
+          "0.000000"
+  */
+  floatToString = float: let
+    result = toString float;
+    precise = float == builtins.fromJSON result;
+  in if precise then result
+    else lib.warn "Imprecise conversion from float to string ${result}" result;
+
   /* Check whether a value can be coerced to a string */
   isCoercibleToString = x:
     builtins.elem (builtins.typeOf x) [ "path" "string" "null" "int" "float" "bool" ] ||