core/pkgs/pkgs-lib/formats/hocon/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
6.1 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ lib, pkgs }:
let
inherit (pkgs) buildPackages callPackage;
hocon-generator = buildPackages.rustPlatform.buildRustPackage {
name = "hocon-generator";
version = "0.1.0";
src = ./src;
passthru.updateScript = ./update.sh;
cargoLock.lockFile = ./src/Cargo.lock;
};
hocon-validator =
pkgs.writers.writePython3Bin "hocon-validator" { libraries = [ pkgs.python3Packages.pyhocon ]; }
''
from sys import argv
from pyhocon import ConfigFactory
if not len(argv) == 2:
print("USAGE: hocon-validator <file>")
ConfigFactory.parse_file(argv[1])
'';
in
{
# https://github.com/lightbend/config/blob/main/HOCON.md
format =
{
generator ? hocon-generator,
validator ? hocon-validator,
# `include classpath("")` is not implemented in pyhocon.
# In the case that you need this functionality,
# you will have to disable pyhocon validation.
doCheck ? true,
}:
let
hoconLib = {
mkInclude =
value:
let
includeStatement =
if lib.isAttrs value && !(lib.isDerivation value) then
{
required = false;
type = null;
_type = "include";
}
// value
else
{
value = toString value;
required = false;
type = null;
_type = "include";
2024-06-30 08:12:46 +00:00
};
in
2024-05-02 00:46:19 +00:00
assert lib.assertMsg
(lib.elem includeStatement.type [
2024-06-30 08:12:46 +00:00
"file"
"url"
2024-05-02 00:46:19 +00:00
"classpath"
2024-06-30 08:12:46 +00:00
null
])
''
2024-05-02 00:46:19 +00:00
Type of HOCON mkInclude is not of type 'file', 'url' or 'classpath':
${(lib.generators.toPretty { }) includeStatement}
2024-06-30 08:12:46 +00:00
'';
2024-05-02 00:46:19 +00:00
includeStatement;
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
mkAppend = value: {
inherit value;
_type = "append";
};
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
mkSubstitution =
2024-06-30 08:12:46 +00:00
value:
2024-05-02 00:46:19 +00:00
if lib.isString value then
2024-06-30 08:12:46 +00:00
{
2024-05-02 00:46:19 +00:00
inherit value;
optional = false;
_type = "substitution";
2024-06-30 08:12:46 +00:00
}
2024-05-02 00:46:19 +00:00
else
assert lib.assertMsg (lib.isAttrs value) ''
Value of invalid type provided to `hocon.lib.mkSubstition`: ${lib.typeOf value}
'';
assert lib.assertMsg (value ? "value") ''
Argument to `hocon.lib.mkSubstition` is missing a `value`:
${builtins.toJSON value}
'';
{
value = value.value;
optional = value.optional or false;
_type = "substitution";
2024-06-30 08:12:46 +00:00
};
2024-05-02 00:46:19 +00:00
};
2024-06-30 08:12:46 +00:00
in
{
type =
let
type' =
2024-05-02 00:46:19 +00:00
with lib.types;
2024-06-30 08:12:46 +00:00
let
2024-05-02 00:46:19 +00:00
atomType = nullOr (oneOf [
2024-06-30 08:12:46 +00:00
bool
float
int
path
2024-05-02 00:46:19 +00:00
str
2024-06-30 08:12:46 +00:00
]);
in
(oneOf [
atomType
2024-05-02 00:46:19 +00:00
(listOf atomType)
(attrsOf type')
2024-06-30 08:12:46 +00:00
])
// {
2024-05-02 00:46:19 +00:00
description = "HOCON value";
2024-06-30 08:12:46 +00:00
};
in
type';
2024-05-02 00:46:19 +00:00
lib = hoconLib;
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
generate =
name: value:
2024-06-30 08:12:46 +00:00
let
2024-05-02 00:46:19 +00:00
# TODO: remove in 24.11
# Backwards compatibility for generators in the following locations:
# - nixos/modules/services/networking/jibri/default.nix (__hocon_envvar)
# - nixos/modules/services/networking/jicofo.nix (__hocon_envvar, __hocon_unquoted_string)
# - nixos/modules/services/networking/jitsi-videobridge.nix (__hocon_envvar)
replaceOldIndicators =
value:
if lib.isAttrs value then
(
if value ? "__hocon_envvar" then
lib.warn ''
Use of `__hocon_envvar` has been deprecated, and will
be removed in the future.
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
Please use `(pkgs.formats.hocon {}).lib.mkSubstitution` instead.
'' (hoconLib.mkSubstitution value.__hocon_envvar)
else if value ? "__hocon_unquoted_string" then
2024-06-30 08:12:46 +00:00
lib.warn
''
2024-05-02 00:46:19 +00:00
Use of `__hocon_unquoted_string` has been deprecated, and will
be removed in the future.
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
Please make use of the freeform options of
`(pkgs.formats.hocon {}).format` instead.
2024-06-30 08:12:46 +00:00
''
{
2024-05-02 00:46:19 +00:00
value = value.__hocon_unquoted_string;
_type = "unquoted_string";
2024-06-30 08:12:46 +00:00
}
else
2024-05-02 00:46:19 +00:00
lib.mapAttrs (_: replaceOldIndicators) value
2024-06-30 08:12:46 +00:00
)
2024-05-02 00:46:19 +00:00
else if lib.isList value then
map replaceOldIndicators value
2024-06-30 08:12:46 +00:00
else
value;
2024-05-02 00:46:19 +00:00
finalValue = replaceOldIndicators value;
2024-06-30 08:12:46 +00:00
in
2024-05-02 00:46:19 +00:00
callPackage
2024-06-30 08:12:46 +00:00
(
{
2024-05-02 00:46:19 +00:00
stdenvNoCC,
hocon-generator,
hocon-validator,
writeText,
2024-06-30 08:12:46 +00:00
}:
2024-05-02 00:46:19 +00:00
stdenvNoCC.mkDerivation rec {
inherit name;
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
dontUnpack = true;
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
json = builtins.toJSON finalValue;
passAsFile = [ "json" ];
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
strictDeps = true;
nativeBuildInputs = [ hocon-generator ];
buildPhase = ''
runHook preBuild
hocon-generator < $jsonPath > output.conf
runHook postBuild
2024-06-30 08:12:46 +00:00
'';
2024-05-02 00:46:19 +00:00
inherit doCheck;
nativeCheckInputs = [ hocon-validator ];
checkPhase = ''
runHook preCheck
hocon-validator output.conf
runHook postCheck
2024-06-30 08:12:46 +00:00
'';
2024-05-02 00:46:19 +00:00
installPhase = ''
runHook preInstall
mv output.conf $out
runHook postInstall
'';
2024-06-30 08:12:46 +00:00
2024-05-02 00:46:19 +00:00
passthru.json = writeText "${name}.json" json;
2024-06-30 08:12:46 +00:00
}
)
2024-05-02 00:46:19 +00:00
{
hocon-generator = generator;
hocon-validator = validator;
};
};
}