Home Explore Blog Models CI



nixpkgs

6th chunk of `nixos/doc/manual/development/settings-options.section.md`
db5e9dbc27bfd95a5faa9299ea6c4ac4ce98e790b0e2138e0000000100000908
    :   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:

`type`

:   A module system type representing a value of the format

`lib`

:   Utility functions for convenience, or special interactions with the format.
    This attribute is optional. It may contain inside a `types` attribute
    containing types specific to this format.

`generate` *`filename jsonValue`*

:   A function that can render a value of the format to a file. Returns
    a file path.

    ::: {.note}
    This function puts the value contents in the Nix store. So this
    should be avoided for secrets.
    :::

::: {#ex-settings-nix-representable .example}
### Module with conventional `settings` option

The following shows a module for an example program that uses a JSON
configuration file. It demonstrates how above values can be used, along
with some other related best practices. See the comments for
explanations.

```nix
{
  options,
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.foo;
  # Define the settings format used for this program
  settingsFormat = pkgs.formats.json { };
in
{

  options.services.foo = {
    enable = lib.mkEnableOption "foo service";

    settings = lib.mkOption {
      # Setting this type allows for correct merging behavior
      type = settingsFormat.type;
      default = { };
      description = ''
        Configuration for foo, see
        <link xlink:href="https://example.com/docs/foo"/>
        for supported settings.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    # We can assign some default settings here to make the service work by just

Title: Nix Formats: PHP Utilities, Common Return Attributes, and Module Configuration Example
Summary
This document elaborates on the `lib` attribute of `pkgs.formats.php`, detailing `mkRaw` for raw PHP code output and `mkMixedArray` for creating PHP arrays with both indexed and associative values. It then specifies the common attributes returned by all `pkgs.formats` functions: `type` (a module system type), an optional `lib` (utility functions), and `generate` (a function to render a value of the format to a file, with a cautionary note about secrets). The text concludes with a practical example demonstrating how to integrate `pkgs.formats` within a Nix module, using `pkgs.formats.json` to define a `settingsFormat` and setting its `type` for a `settings` option to ensure proper merging behavior for configuration.