diff --git a/nixos/doc/manual/development/settings-options.section.md b/nixos/doc/manual/development/settings-options.section.md index 806eee563790..cedc82d32f89 100644 --- a/nixos/doc/manual/development/settings-options.section.md +++ b/nixos/doc/manual/development/settings-options.section.md @@ -146,6 +146,27 @@ have a predefined type and string generator already declared under : Outputs the given attribute set as an Elixir map, instead of the default Elixir keyword list +`pkgs.formats.php { finalVariable }` []{#pkgs-formats-php} + +: A function taking an attribute set with values + + `finalVariable` + + : The variable that will store generated expression (usually `config`). If set to `null`, generated expression will contain `return`. + + It returns a set with PHP-Config-specific attributes `type`, `lib`, and + `generate` as specified [below](#pkgs-formats-result). + + The `lib` attribute contains functions to be used in settings, for + generating special PHP values: + + `mkRaw phpCode` + + : Outputs the given string as raw PHP code + + `mkMixedArray list set` + + : Creates PHP array that contains both indexed and associative values. For example, `lib.mkMixedArray [ "hello" "world" ] { "nix" = "is-great"; }` returns `['hello', 'world', 'nix' => 'is-great']` []{#pkgs-formats-result} These functions all return an attribute set with these values: diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix index 1b72270b43ca..7cc0bf4f4e50 100644 --- a/pkgs/pkgs-lib/formats.nix +++ b/pkgs/pkgs-lib/formats.nix @@ -43,6 +43,8 @@ rec { hocon = (import ./formats/hocon/default.nix { inherit lib pkgs; }).format; + php = (import ./formats/php/default.nix { inherit lib pkgs; }).format; + json = {}: { type = with lib.types; let diff --git a/pkgs/pkgs-lib/formats/php/default.nix b/pkgs/pkgs-lib/formats/php/default.nix new file mode 100644 index 000000000000..9bd1de958d0b --- /dev/null +++ b/pkgs/pkgs-lib/formats/php/default.nix @@ -0,0 +1,69 @@ +{pkgs, lib}: { + # Format for defining configuration of some PHP services, that use "include 'config.php';" approach. + format = {finalVariable ? null}: let + toPHP = value: { + "null" = "null"; + "bool" = if value then "true" else "false"; + "int" = toString value; + "float" = toString value; + "string" = string value; + "set" = attrs value; + "list" = list value; + } + .${builtins.typeOf value} or + (abort "should never happen: unknown value type ${builtins.typeOf value}"); + + # https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single + escapeSingleQuotedString = lib.escape [ "'" "\\" ]; + string = value: "'${escapeSingleQuotedString value}'"; + + listContent = values: lib.concatStringsSep ", " (map toPHP values); + list = values: "[" + (listContent values) + "]"; + + attrsContent = values: lib.pipe values [ + (lib.mapAttrsToList (k: v: "${toPHP k} => ${toPHP v}")) + (lib.concatStringsSep ", ") + ]; + attrs = set: + if set ? _phpType then specialType set + else + "[" + attrsContent set + "]"; + + mixedArray = {list, set}: if list == [] then attrs set else "[${listContent list}, ${attrsContent set}]"; + + specialType = {value, _phpType}: { + "mixed_array" = mixedArray value; + "raw" = value; + }.${_phpType}; + + type = with lib.types; + nullOr (oneOf [ + bool + int + float + str + (attrsOf type) + (listOf type) + ]) + // { + description = "PHP value"; + }; + in { + + inherit type; + + lib = { + mkMixedArray = list: set: {_phpType = "mixed_array"; value = { inherit list set;}; }; + mkRaw = raw: {_phpType = "raw"; value = raw;}; + }; + + generate = name: value: pkgs.writeTextFile { + inherit name; + text = let + # strict_types enabled here to easily debug problems when calling functions of incorrect type using `mkRaw`. + phpHeader = " ['foo' => null], 'false' => false, 'float' => 3.141000, 'int' => 10, 'list' => [null, null], 'mixed' => [10, 3.141000, 'attrs' => ['foo' => null], 'str' => 'foo'], 'null' => null, 'raw' => random_function(), 'str' => 'foo', 'str_special' => 'foo + testhello\'\'\'${"'"}, 'true' => true]; + ''; + }; + + phpReturn = shouldPass { + format = formats.php { }; + input = { + int = 10; + float = 3.141; + str = "foo"; + str_special = "foo\ntesthello'''"; + attrs.foo = null; + }; + expected = '' + ['foo' => null], 'float' => 3.141000, 'int' => 10, 'str' => 'foo', 'str_special' => 'foo + testhello\'\'\'${"'"}]; + ''; + }; + }