lib/generators: builtins.isFloat is in Nix 2.3

There's a couple of aliases in play:

1. `lib.isList` is an alias for `builtins.isList`.
2. `lib.strings.concatStringsSep` is an alias for `builtins.concatStringsSep`
This commit is contained in:
Philip Taron
2024-03-06 15:49:31 -08:00
parent 18d2b24a84
commit 8041b9b2b3

View File

@@ -19,7 +19,30 @@ let
libStr = lib.strings; libStr = lib.strings;
libAttr = lib.attrsets; libAttr = lib.attrsets;
inherit (lib) isFunction; inherit (builtins)
addErrorContext
attrNames
concatStringsSep
elem
filter
head
isAttrs
isFloat
isInt
isList
isPath
isString
length
mapAttrs
match
replaceStrings
split
tail
;
inherit (lib)
isFunction
;
in in
rec { rec {
@@ -30,7 +53,7 @@ rec {
* The builtin `toString` function has some strange defaults, * The builtin `toString` function has some strange defaults,
* suitable for bash scripts but not much else. * suitable for bash scripts but not much else.
*/ */
mkValueStringDefault = {}: v: with builtins; mkValueStringDefault = {}: v:
let err = t: v: abort let err = t: v: abort
("generators.mkValueStringDefault: " + ("generators.mkValueStringDefault: " +
"${t} not supported: ${toPretty {} v}"); "${t} not supported: ${toPretty {} v}");
@@ -86,7 +109,7 @@ rec {
}: }:
let mkLine = k: v: indent + mkKeyValue k v + "\n"; let mkLine = k: v: indent + mkKeyValue k v + "\n";
mkLines = if listsAsDuplicateKeys mkLines = if listsAsDuplicateKeys
then k: v: map (mkLine k) (if lib.isList v then v else [v]) then k: v: map (mkLine k) (if isList v then v else [v])
else k: v: [ (mkLine k v) ]; else k: v: [ (mkLine k v) ];
in attrs: libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs)); in attrs: libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs));
@@ -195,7 +218,6 @@ rec {
*> name = "edolstra" *> name = "edolstra"
*/ */
toGitINI = attrs: toGitINI = attrs:
with builtins;
let let
mkSectionName = name: mkSectionName = name:
let let
@@ -266,7 +288,7 @@ rec {
/* If this option is true, an error will be thrown, if a certain given depth is exceeded */ /* If this option is true, an error will be thrown, if a certain given depth is exceeded */
, throwOnDepthLimit ? true , throwOnDepthLimit ? true
}: }:
assert builtins.isInt depthLimit; assert isInt depthLimit;
let let
specialAttrs = [ specialAttrs = [
"__functor" "__functor"
@@ -275,7 +297,7 @@ rec {
"__pretty" "__pretty"
]; ];
stepIntoAttr = evalNext: name: stepIntoAttr = evalNext: name:
if builtins.elem name specialAttrs if elem name specialAttrs
then id then id
else evalNext; else evalNext;
transform = depth: transform = depth:
@@ -284,7 +306,7 @@ rec {
then throw "Exceeded maximum eval-depth limit of ${toString depthLimit} while trying to evaluate with `generators.withRecursion'!" then throw "Exceeded maximum eval-depth limit of ${toString depthLimit} while trying to evaluate with `generators.withRecursion'!"
else const "<unevaluated>" else const "<unevaluated>"
else id; else id;
mapAny = with builtins; depth: v: mapAny = depth: v:
let let
evalNext = x: mapAny (depth + 1) (transform (depth + 1) x); evalNext = x: mapAny (depth + 1) (transform (depth + 1) x);
in in
@@ -311,9 +333,8 @@ rec {
indent ? "" indent ? ""
}: }:
let let
go = indent: v: with builtins; go = indent: v:
let isPath = v: typeOf v == "path"; let introSpace = if multiline then "\n${indent} " else " ";
introSpace = if multiline then "\n${indent} " else " ";
outroSpace = if multiline then "\n${indent}" else " "; outroSpace = if multiline then "\n${indent}" else " ";
in if isInt v then toString v in if isInt v then toString v
# toString loses precision on floats, so we use toJSON instead. This isn't perfect # toString loses precision on floats, so we use toJSON instead. This isn't perfect
@@ -322,7 +343,7 @@ rec {
else if isFloat v then builtins.toJSON v else if isFloat v then builtins.toJSON v
else if isString v then else if isString v then
let let
lines = filter (v: ! isList v) (builtins.split "\n" v); lines = filter (v: ! isList v) (split "\n" v);
escapeSingleline = libStr.escape [ "\\" "\"" "\${" ]; escapeSingleline = libStr.escape [ "\\" "\"" "\${" ];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
singlelineResult = "\"" + concatStringsSep "\\n" (map escapeSingleline lines) + "\""; singlelineResult = "\"" + concatStringsSep "\\n" (map escapeSingleline lines) + "\"";
@@ -359,10 +380,10 @@ rec {
else if v ? type && v.type == "derivation" then else if v ? type && v.type == "derivation" then
"<derivation ${v.name or "???"}>" "<derivation ${v.name or "???"}>"
else "{" + introSpace else "{" + introSpace
+ libStr.concatStringsSep introSpace (libAttr.mapAttrsToList + concatStringsSep introSpace (libAttr.mapAttrsToList
(name: value: (name: value:
"${libStr.escapeNixIdentifier name} = ${ "${libStr.escapeNixIdentifier name} = ${
builtins.addErrorContext "while evaluating an attribute `${name}`" addErrorContext "while evaluating an attribute `${name}`"
(go (indent + " ") value) (go (indent + " ") value)
};") v) };") v)
+ outroSpace + "}" + outroSpace + "}"
@@ -371,9 +392,7 @@ rec {
# PLIST handling # PLIST handling
toPlist = {}: v: let toPlist = {}: v: let
isFloat = builtins.isFloat or (x: false); expr = ind: x:
isPath = x: builtins.typeOf x == "path";
expr = ind: x: with builtins;
if x == null then "" else if x == null then "" else
if isBool x then bool ind x else if isBool x then bool ind x else
if isInt x then int ind x else if isInt x then int ind x else
@@ -396,20 +415,20 @@ rec {
item = ind: libStr.concatMapStringsSep "\n" (indent ind); item = ind: libStr.concatMapStringsSep "\n" (indent ind);
list = ind: x: libStr.concatStringsSep "\n" [ list = ind: x: concatStringsSep "\n" [
(literal ind "<array>") (literal ind "<array>")
(item ind x) (item ind x)
(literal ind "</array>") (literal ind "</array>")
]; ];
attrs = ind: x: libStr.concatStringsSep "\n" [ attrs = ind: x: concatStringsSep "\n" [
(literal ind "<dict>") (literal ind "<dict>")
(attr ind x) (attr ind x)
(literal ind "</dict>") (literal ind "</dict>")
]; ];
attr = let attrFilter = name: value: name != "_module" && value != null; attr = let attrFilter = name: value: name != "_module" && value != null;
in ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList in ind: x: concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList
(name: value: lib.optionals (attrFilter name value) [ (name: value: lib.optionals (attrFilter name value) [
(key "\t${ind}" name) (key "\t${ind}" name)
(expr "\t${ind}" value) (expr "\t${ind}" value)
@@ -426,8 +445,7 @@ ${expr "" v}
* the Natural type. * the Natural type.
*/ */
toDhall = { }@args: v: toDhall = { }@args: v:
with builtins; let concatItems = concatStringsSep ", ";
let concatItems = lib.strings.concatStringsSep ", ";
in if isAttrs v then in if isAttrs v then
"{ ${ "{ ${
concatItems (lib.attrsets.mapAttrsToList concatItems (lib.attrsets.mapAttrsToList
@@ -488,7 +506,6 @@ ${expr "" v}
/* Interpret as variable bindings */ /* Interpret as variable bindings */
asBindings ? false, asBindings ? false,
}@args: v: }@args: v:
with builtins;
let let
innerIndent = "${indent} "; innerIndent = "${indent} ";
introSpace = if multiline then "\n${innerIndent}" else " "; introSpace = if multiline then "\n${innerIndent}" else " ";