diff --git a/pkgs/by-name/js/json2cdn/package.nix b/pkgs/by-name/js/json2cdn/package.nix index 08fe7deb843e..0628d91ed482 100644 --- a/pkgs/by-name/js/json2cdn/package.nix +++ b/pkgs/by-name/js/json2cdn/package.nix @@ -5,6 +5,7 @@ gradle_8, jre_headless, makeBinaryWrapper, + tests, }: stdenv.mkDerivation (finalAttrs: { pname = "json2cdn"; @@ -42,6 +43,10 @@ stdenv.mkDerivation (finalAttrs: { runHook postInstall ''; + passthru = { + tests.formats-cdn = tests.pkgs-lib.formats.passthru.entries.pass-cdnAtoms; + }; + meta = { description = "Converts a JSON file to dzikoysk's CDN format"; homepage = "https://github.com/uku3lig/json2cdn"; diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix index 5bf51a7e6fe6..e02a5803ae0e 100644 --- a/pkgs/pkgs-lib/formats.nix +++ b/pkgs/pkgs-lib/formats.nix @@ -310,6 +310,39 @@ rec { }; + /* dzikoysk's CDN format, see https://github.com/dzikoysk/cdn + + The result is almost identical to YAML when there are no nested properties, + but differs enough in the other case to warrant a separate format. + (see https://github.com/dzikoysk/cdn#supported-formats) + + Currently used by Panda, Reposilite, and FunnyGuilds (as per the repo's readme). + */ + cdn = {}: json {} // { + type = let + valueType = nullOr (oneOf [ + bool + int + float + str + path + (attrsOf valueType) + (listOf valueType) + ]) // { + description = "CDN value"; + }; + in valueType; + + generate = name: value: pkgs.callPackage ({ runCommand, json2cdn }: runCommand name { + nativeBuildInputs = [ json2cdn ]; + value = builtins.toJSON value; + passAsFile = [ "value" ]; + preferLocalBuild = true; + } '' + json2cdn "$valuePath" > $out + '') {}; + }; + /* For configurations of Elixir project, like config.exs or runtime.exs Most Elixir project are configured using the [Config] Elixir DSL diff --git a/pkgs/pkgs-lib/tests/formats.nix b/pkgs/pkgs-lib/tests/formats.nix index 1c6f741353fa..2576c11b6687 100644 --- a/pkgs/pkgs-lib/tests/formats.nix +++ b/pkgs/pkgs-lib/tests/formats.nix @@ -533,6 +533,40 @@ in runBuildTests { ''; }; + cdnAtoms = shouldPass { + format = formats.cdn { }; + input = { + null = null; + false = false; + true = true; + int = 10; + float = 3.141; + str = "foo"; + attrs.foo = null; + list = [ + 1 + null + ]; + path = ./formats.nix; + }; + expected = '' + attrs { + "foo": null + } + "false": false + "float": 3.141 + "int": 10 + list [ + 1, + null + ] + "null": null + "path": "${./formats.nix}" + "str": "foo" + "true": true + ''; + }; + # This test is responsible for # 1. testing type coercions # 2. providing a more readable example test