5 inherit (pkgs) buildPackages callPackage;
7 hocon-generator = buildPackages.rustPlatform.buildRustPackage {
8 name = "hocon-generator";
12 passthru.updateScript = ./update.sh;
14 cargoLock.lockFile = ./src/Cargo.lock;
17 hocon-validator = pkgs.writers.writePython3Bin "hocon-validator" {
18 libraries = [ pkgs.python3Packages.pyhocon ];
21 from pyhocon import ConfigFactory
23 if not len(argv) == 2:
24 print("USAGE: hocon-validator <file>")
26 ConfigFactory.parse_file(argv[1])
31 generator ? hocon-generator
32 , validator ? hocon-validator
36 mkInclude = value: let
37 includeStatement = if lib.isAttrs value && !(lib.isDerivation value) then {
42 value = toString value;
48 assert lib.assertMsg (lib.elem includeStatement.type [ "file" "url" "classpath" null ]) ''
49 Type of HOCON mkInclude is not of type 'file', 'url' or 'classpath':
50 ${(lib.generators.toPretty {}) includeStatement}
59 mkSubstitution = value:
65 _type = "substitution";
68 assert lib.assertMsg (lib.isAttrs value) ''
69 Value of invalid type provided to `hocon.lib.mkSubstitution`: ${lib.typeOf value}
71 assert lib.assertMsg (value ? "value") ''
72 Argument to `hocon.lib.mkSubstitution` is missing a `value`:
73 ${builtins.toJSON value}
77 optional = value.optional or false;
78 _type = "substitution";
84 type' = with lib.types; let
85 atomType = nullOr (oneOf [
97 description = "HOCON value";
103 generate = name: value:
105 # TODO: remove in 24.11
106 # Backwards compatibility for generators in the following locations:
107 # - nixos/modules/services/networking/jibri/default.nix (__hocon_envvar)
108 # - nixos/modules/services/networking/jicofo.nix (__hocon_envvar, __hocon_unquoted_string)
109 # - nixos/modules/services/networking/jitsi-videobridge.nix (__hocon_envvar)
110 replaceOldIndicators = value:
111 if lib.isAttrs value then
112 (if value ? "__hocon_envvar"
115 Use of `__hocon_envvar` has been deprecated, and will
116 be removed in the future.
118 Please use `(pkgs.formats.hocon {}).lib.mkSubstitution` instead.
120 (hoconLib.mkSubstitution value.__hocon_envvar)
121 else if value ? "__hocon_unquoted_string"
124 Use of `__hocon_unquoted_string` has been deprecated, and will
125 be removed in the future.
127 Please make use of the freeform options of
128 `(pkgs.formats.hocon {}).format` instead.
131 value = value.__hocon_unquoted_string;
132 _type = "unquoted_string";
134 else lib.mapAttrs (_: replaceOldIndicators) value)
135 else if lib.isList value
136 then map replaceOldIndicators value
139 finalValue = replaceOldIndicators value;
148 stdenvNoCC.mkDerivation rec {
152 preferLocalBuild = true;
154 json = builtins.toJSON finalValue;
155 passAsFile = [ "json" ];
158 nativeBuildInputs = [ hocon-generator ];
161 hocon-generator < $jsonPath > output.conf
166 nativeCheckInputs = [ hocon-validator ];
169 hocon-validator output.conf
179 passthru.json = writeText "${name}.json" json;
182 hocon-generator = generator;
183 hocon-validator = validator;