2020-03-26 01:55:16 +00:00
|
|
|
{ lib, pkgs }:
|
|
|
|
rec {
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Every following entry represents a format for program configuration files
|
|
|
|
used for `settings`-style options (see https://github.com/NixOS/rfcs/pull/42).
|
|
|
|
Each entry should look as follows:
|
|
|
|
|
|
|
|
<format> = <parameters>: {
|
|
|
|
# ^^ Parameters for controlling the format
|
|
|
|
|
|
|
|
# The module system type most suitable for representing such a format
|
|
|
|
# The description needs to be overwritten for recursive types
|
|
|
|
type = ...;
|
|
|
|
|
|
|
|
# generate :: Name -> Value -> Path
|
|
|
|
# A function for generating a file with a value of such a type
|
|
|
|
generate = ...;
|
|
|
|
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
json = {}: {
|
|
|
|
|
|
|
|
type = with lib.types; let
|
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
2020-11-15 16:54:15 +00:00
|
|
|
path
|
2020-03-26 01:55:16 +00:00
|
|
|
(attrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
]) // {
|
|
|
|
description = "JSON value";
|
|
|
|
};
|
|
|
|
in valueType;
|
|
|
|
|
2021-08-15 15:12:23 +00:00
|
|
|
generate = name: value: pkgs.runCommand name {
|
2020-03-26 01:55:16 +00:00
|
|
|
nativeBuildInputs = [ pkgs.jq ];
|
|
|
|
value = builtins.toJSON value;
|
|
|
|
passAsFile = [ "value" ];
|
|
|
|
} ''
|
|
|
|
jq . "$valuePath"> $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-08-13 19:31:14 +00:00
|
|
|
yaml = {}: {
|
|
|
|
|
|
|
|
generate = name: value: pkgs.runCommand name {
|
|
|
|
nativeBuildInputs = [ pkgs.remarshal ];
|
|
|
|
value = builtins.toJSON value;
|
|
|
|
passAsFile = [ "value" ];
|
|
|
|
} ''
|
|
|
|
json2yaml "$valuePath" "$out"
|
|
|
|
'';
|
|
|
|
|
|
|
|
type = with lib.types; let
|
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
|
|
|
path
|
|
|
|
(attrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
]) // {
|
2020-03-26 01:55:16 +00:00
|
|
|
description = "YAML value";
|
|
|
|
};
|
2021-08-13 19:31:14 +00:00
|
|
|
in valueType;
|
|
|
|
|
|
|
|
};
|
2020-03-26 01:55:16 +00:00
|
|
|
|
2021-05-04 07:49:25 +00:00
|
|
|
ini = {
|
|
|
|
# Represents lists as duplicate keys
|
|
|
|
listsAsDuplicateKeys ? false,
|
|
|
|
# Alternative to listsAsDuplicateKeys, converts list to non-list
|
|
|
|
# listToValue :: [IniAtom] -> IniAtom
|
|
|
|
listToValue ? null,
|
|
|
|
...
|
|
|
|
}@args:
|
|
|
|
assert !listsAsDuplicateKeys || listToValue == null;
|
|
|
|
{
|
2020-03-26 01:55:16 +00:00
|
|
|
|
|
|
|
type = with lib.types; let
|
|
|
|
|
|
|
|
singleIniAtom = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
|
|
|
]) // {
|
|
|
|
description = "INI atom (null, bool, int, float or string)";
|
|
|
|
};
|
|
|
|
|
|
|
|
iniAtom =
|
|
|
|
if listsAsDuplicateKeys then
|
|
|
|
coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // {
|
|
|
|
description = singleIniAtom.description + " or a list of them for duplicate keys";
|
|
|
|
}
|
2021-05-04 07:49:25 +00:00
|
|
|
else if listToValue != null then
|
|
|
|
coercedTo singleIniAtom lib.singleton (nonEmptyListOf singleIniAtom) // {
|
|
|
|
description = singleIniAtom.description + " or a non-empty list of them";
|
|
|
|
}
|
2020-03-26 01:55:16 +00:00
|
|
|
else
|
|
|
|
singleIniAtom;
|
|
|
|
|
|
|
|
in attrsOf (attrsOf iniAtom);
|
|
|
|
|
2021-05-04 07:49:25 +00:00
|
|
|
generate = name: value:
|
|
|
|
let
|
|
|
|
transformedValue =
|
|
|
|
if listToValue != null
|
|
|
|
then
|
|
|
|
lib.mapAttrs (section: lib.mapAttrs (key: val:
|
|
|
|
if lib.isList val then listToValue val else val
|
|
|
|
)) value
|
|
|
|
else value;
|
|
|
|
in pkgs.writeText name (lib.generators.toINI (removeAttrs args ["listToValue"]) transformedValue);
|
2020-03-26 01:55:16 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
toml = {}: json {} // {
|
|
|
|
type = with lib.types; let
|
|
|
|
valueType = oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
2020-11-15 16:54:15 +00:00
|
|
|
path
|
2020-03-26 01:55:16 +00:00
|
|
|
(attrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
] // {
|
|
|
|
description = "TOML value";
|
|
|
|
};
|
|
|
|
in valueType;
|
|
|
|
|
2021-08-15 15:12:23 +00:00
|
|
|
generate = name: value: pkgs.runCommand name {
|
2020-03-26 01:55:16 +00:00
|
|
|
nativeBuildInputs = [ pkgs.remarshal ];
|
|
|
|
value = builtins.toJSON value;
|
|
|
|
passAsFile = [ "value" ];
|
|
|
|
} ''
|
|
|
|
json2toml "$valuePath" "$out"
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|