lol

formats.hocon: add backwards compatibility

h7x4 0e65eca7 9ebcb6f5

+170 -21
+61 -21
pkgs/pkgs-lib/formats/hocon/default.nix
··· 35 35 # In the case that you need this functionality, 36 36 # you will have to disable pyhocon validation. 37 37 , doCheck ? true 38 - }: { 39 - type = let 40 - type' = with lib.types; let 41 - atomType = nullOr (oneOf [ 42 - bool 43 - float 44 - int 45 - path 46 - str 47 - ]); 48 - in (oneOf [ 49 - atomType 50 - (listOf atomType) 51 - (attrsOf type') 52 - ]) // { 53 - description = "HOCON value"; 54 - }; 55 - in type'; 56 - 57 - lib = { 38 + }: let 39 + hoconLib = { 58 40 mkInclude = value: let 59 41 includeStatement = if lib.isAttrs value && !(lib.isDerivation value) then { 60 42 required = false; ··· 101 83 }; 102 84 }; 103 85 86 + in { 87 + type = let 88 + type' = with lib.types; let 89 + atomType = nullOr (oneOf [ 90 + bool 91 + float 92 + int 93 + path 94 + str 95 + ]); 96 + in (oneOf [ 97 + atomType 98 + (listOf atomType) 99 + (attrsOf type') 100 + ]) // { 101 + description = "HOCON value"; 102 + }; 103 + in type'; 104 + 105 + lib = hoconLib; 106 + 104 107 generate = name: value: 108 + let 109 + # TODO: remove in 24.11 110 + # Backwards compatability for generators in the following locations: 111 + # - nixos/modules/services/networking/jibri/default.nix (__hocon_envvar) 112 + # - nixos/modules/services/networking/jicofo.nix (__hocon_envvar, __hocon_unquoted_string) 113 + # - nixos/modules/services/networking/jitsi-videobridge.nix (__hocon_envvar) 114 + replaceOldIndicators = value: 115 + if lib.isAttrs value then 116 + (if value ? "__hocon_envvar" 117 + then 118 + lib.warn '' 119 + Use of `__hocon_envvar` has been deprecated, and will 120 + be removed in the future. 121 + 122 + Please use `(pkgs.formats.hocon {}).lib.mkSubstitution` instead. 123 + '' 124 + (hoconLib.mkSubstitution value.__hocon_envvar) 125 + else if value ? "__hocon_unquoted_string" 126 + then 127 + lib.warn '' 128 + Use of `__hocon_unquoted_string` has been deprecated, and will 129 + be removed in the future. 130 + 131 + Please make use of the freeform options of 132 + `(pkgs.formats.hocon {}).format` instead. 133 + '' 134 + { 135 + value = value.__hocon_unquoted_string; 136 + _type = "unquoted_string"; 137 + } 138 + else lib.mapAttrs (_: replaceOldIndicators) value) 139 + else if lib.isList value 140 + then map replaceOldIndicators value 141 + else value; 142 + 143 + finalValue = replaceOldIndicators value; 144 + in 105 145 callPackage 106 146 ({ 107 147 stdenvNoCC ··· 114 154 115 155 dontUnpack = true; 116 156 117 - json = builtins.toJSON value; 157 + json = builtins.toJSON finalValue; 118 158 passAsFile = [ "json" ]; 119 159 120 160 strictDeps = true;
+11
pkgs/pkgs-lib/formats/hocon/src/src/main.rs
··· 10 10 List(Vec<HOCONValue>), 11 11 Substitution(String, bool), 12 12 Object(Vec<HOCONInclude>, Vec<(String, HOCONValue)>), 13 + Literal(String), 13 14 } 14 15 15 16 #[derive(Debug)] ··· 91 92 let value = o.get("value").expect("Missing value for append"); 92 93 93 94 HOCONValue::Append(Box::new(json_to_hocon(value))) 95 + } 96 + "unquoted_string" => { 97 + let value = o 98 + .get("value") 99 + .expect("Missing value for unquoted_string") 100 + .as_str() 101 + .unwrap_or_else(|| panic!("Unquoted string value is not a string: {:?}", o)); 102 + 103 + HOCONValue::Literal(value.to_string()) 94 104 } 95 105 _ => panic!( 96 106 "\ ··· 210 220 format!("{{\n{}\n}}", content) 211 221 } 212 222 HOCONValue::Append(_) => panic!("Append should not be present at this point"), 223 + Self::Literal(s) => s.to_string(), 213 224 } 214 225 } 215 226 }
+65
pkgs/pkgs-lib/formats/hocon/test/backwards-compatibility/default.nix
··· 1 + { lib, formats, stdenvNoCC, writeText, ... }: 2 + let 3 + hocon = formats.hocon { }; 4 + 5 + expression = { 6 + substitution = { __hocon_envvar = "PATH"; }; 7 + literal = { 8 + __hocon_unquoted_string = '' 9 + [ 10 + 1, 11 + "a", 12 + ]''; 13 + }; 14 + 15 + nested = { 16 + substitution = { __hocon_envvar = "PATH"; }; 17 + literal = { 18 + __hocon_unquoted_string = '' 19 + [ 20 + 1, 21 + "a", 22 + ]''; 23 + }; 24 + }; 25 + 26 + nested_in_array = [ 27 + { __hocon_envvar = "PATH"; } 28 + { 29 + __hocon_unquoted_string = '' 30 + [ 31 + 1, 32 + "a", 33 + ]''; 34 + } 35 + ]; 36 + }; 37 + 38 + hocon-test-conf = hocon.generate "hocon-test.conf" expression; 39 + in 40 + stdenvNoCC.mkDerivation { 41 + name = "pkgs.formats.hocon-test-backwards-compatibility"; 42 + 43 + dontUnpack = true; 44 + dontBuild = true; 45 + 46 + doCheck = true; 47 + checkPhase = '' 48 + runHook preCheck 49 + 50 + diff -U3 ${./expected.txt} ${hocon-test-conf} 51 + 52 + runHook postCheck 53 + ''; 54 + 55 + installPhase = '' 56 + runHook preInstall 57 + 58 + mkdir $out 59 + cp ${./expected.txt} $out/expected.txt 60 + cp ${hocon-test-conf} $out/hocon-test.conf 61 + cp ${hocon-test-conf.passthru.json} $out/hocon-test.json 62 + 63 + runHook postInstall 64 + ''; 65 + }
+22
pkgs/pkgs-lib/formats/hocon/test/backwards-compatibility/expected.txt
··· 1 + { 2 + "literal" = [ 3 + 1, 4 + "a", 5 + ] 6 + "nested" = { 7 + "literal" = [ 8 + 1, 9 + "a", 10 + ] 11 + "substitution" = ${?PATH} 12 + } 13 + "nested_in_array" = [ 14 + ${?PATH}, 15 + [ 16 + 1, 17 + "a", 18 + ] 19 + ] 20 + "substitution" = ${?PATH} 21 + } 22 +
+11
pkgs/pkgs-lib/formats/hocon/test/default.nix
··· 1 1 { pkgs, ... }: 2 2 { 3 3 comprehensive = pkgs.callPackage ./comprehensive { }; 4 + backwards-compatibility = 5 + let 6 + pkgsNoWarn = pkgs.extend (final: prev: { 7 + lib = prev.lib.extend (libFinal: libPrev: { 8 + warn = msg: v: v; 9 + trivial = libPrev.trivial // { 10 + warn = msg: v: v; 11 + }; 12 + }); 13 + }); 14 + in pkgsNoWarn.callPackage ./backwards-compatibility { }; 4 15 }