-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.nix
46 lines (41 loc) · 1.35 KB
/
lib.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{ lib }:
{
toTOML = let
inherit (builtins) toJSON concatStringsSep isAttrs isList isFloat;
inherit (lib) isStringLike concatMapStringsSep mapAttrsToList;
# We use `toJSON` for serialization of string, numbers and booleans.
# The only incompatibility is that JSON allows `"\/"` while TOML does not.
# But `builtins.toJSON` does not escape `/` anyway, so it's fine.
inf = 1.0e308 * 10;
toTopLevel = obj:
concatStringsSep ""
(mapAttrsToList
(name: value: "${toJSON name}=${toInline value}\n")
obj);
toInline = obj:
# Exclude drvs here, or we'll easily get infinite recursion.
if isAttrs obj && !isStringLike obj then
"{${concatStringsSep ","
(mapAttrsToList
(name: value: "${toJSON name}=${toInline value}")
obj)
}}"
else if isList obj then
"[${concatMapStringsSep "," toInline obj}]"
else if obj == null then
throw "“null” is not supported by TOML"
else if !isFloat obj then
# Strings, integers and booleans.
toJSON obj
# Sanitize +-inf and NaN. They'll produce "null", which is invalid for TOML.
else if obj == inf then
"inf"
else if obj == -inf then
"-inf"
else if obj != obj then
"nan"
else
toJSON obj;
in
toTopLevel;
}