Home Explore Blog CI



nixpkgs

2nd chunk of `nixos/doc/manual/development/settings-options.section.md`
fa30afd09c98cdf7d9a980e76caa5b6d6c0b5af9427225e70000000100000ff4
    Some of the lib functions will be best understood if you have
    read the reference specification. You can find this
    specification here:

    <https://github.com/lightbend/config/blob/main/HOCON.md>

    Inside of `lib`, you will find these functions

    `mkInclude`

    :   This is used together with a specially named
        attribute `includes`, to include other HOCON
        sources into the document.

        The function has a shorthand variant where it
        is up to the HOCON parser to figure out what type
        of include is being used. The include will default
        to being non-required. If you want to be more
        explicit about the details of the include, you can
        provide an attrset with following arguments

        `required`

        :   Whether the parser should fail upon failure
            to include the document

        `type`

        :   Type of the source of the included document.
            Valid values are `file`, `url` and `classpath`.
            See upstream documentation for the semantics
            behind each value

        `value`

        :   The URI/path/classpath pointing to the source of
            the document to be included.

        `Example usage:`

        ```nix
          let
            format = pkgs.formats.hocon { };
            hocon_file = pkgs.writeText "to_include.hocon" ''
              a = 1;
            '';
          in {
            some.nested.hocon.attrset = {
              _includes = [
                (format.lib.mkInclude hocon_file)
                (format.lib.mkInclude "https://example.com/to_include.hocon")
                (format.lib.mkInclude {
                  required = true;
                  type = "file";
                  value = include_file;
                })
              ];
              ...
            };
          }
        ```

    `mkAppend`

    :   This is used to invoke the `+=` operator.
        This can be useful if you need to add something
        to a list that is included from outside of nix.
        See upstream documentation for the semantics
        behind the `+=` operation.

        `Example usage:`

        ```nix
          let
            format = pkgs.formats.hocon { };
            hocon_file = pkgs.writeText "to_include.hocon" ''
              a = [ 1 ];
              b = [ 2 ];
            '';
          in {
            _includes = [
              (format.lib.mkInclude hocon_file)
            ];

            c = 3;
            a = format.lib.mkAppend 3;
            b = format.lib.mkAppend (format.lib.mkSubstitution "c");
          }
        ```

    `mkSubstitution`

    :   This is used to make HOCON substitutions.
        Similarly to `mkInclude`, this function has
        a shorthand variant where you just give it
        the string with the substitution value.
        The substitution is not optional by default.
        Alternatively, you can provide an attrset
        with more options

        `optional`

        :   Whether the parser should fail upon
            failure to fetch the substitution value.

        `value`

        :   The name of the variable to use for
            substitution.

        See upstream documentation for semantics
        behind the substitution functionality.

        `Example usage:`

        ```nix
          let
            format = pkgs.formats.hocon { };
          in {
            a = 1;
            b = format.lib.mkSubstitution "a";
            c = format.lib.mkSubstitution "SOME_ENVVAR";
            d = format.lib.mkSubstitution {
              value = "SOME_OPTIONAL_ENVVAR";
              optional = true;
            };
          }
        ```

    `Implementation notes:`

    - classpath includes are not implemented in pyhocon,
      which is used for validating the HOCON output. This
      means that if you are using classpath includes,
      you will want to either use an alternative validator
      or set `doCheck = false` in the format options.

`pkgs.formats.libconfig` { *`generator`* ? `<derivation>`, *`validator`* ? `<derivation>` }

Title: HOCON Library Functions: mkInclude, mkAppend, and mkSubstitution
Summary
The `pkgs.formats.hocon` library provides functions like `mkInclude`, `mkAppend`, and `mkSubstitution` to manipulate HOCON configurations. `mkInclude` includes other HOCON sources, with options for specifying the source type (file, URL, classpath) and whether the inclusion is required. `mkAppend` adds values to lists, useful for modifying lists included from external files. `mkSubstitution` allows for HOCON substitutions with options for optional values. Examples are provided for each function. Note that classpath includes might require disabling the validator due to pyhocon limitations.