-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
formats.kdl: init #295211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
formats.kdl: init #295211
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| lib, | ||
| rustPlatform, | ||
| fetchFromGitHub, | ||
| }: | ||
|
|
||
| rustPlatform.buildRustPackage rec { | ||
| pname = "json2kdl"; | ||
| version = "0.2.0"; | ||
|
|
||
| src = fetchFromGitHub { | ||
| owner = "AgathaSorceress"; | ||
| repo = pname; | ||
| rev = "v${version}"; | ||
| hash = "sha256-NVpIHbv7vbppe+g7YK9OY2oL7axmqG8Kmuv4kO8Jyjs="; | ||
| }; | ||
|
|
||
| cargoHash = "sha256-xlG8p25VBLwUWnyr9JNzSrI0KmwdRpAgL5eckbC/3nk="; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems this tag has changed since: |
||
|
|
||
| meta = { | ||
| description = "Program that converts JSON files to KDL"; | ||
| homepage = "https://github.com/AgathaSorceress/json2kdl"; | ||
| platforms = lib.platforms.all; | ||
| maintainers = (with lib.maintainers; [ feathecutie ]); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -637,4 +637,138 @@ rec { | |
| else | ||
| throw "pkgs.formats.xml: Unknown format: ${format}"; | ||
|
|
||
| # The KDL document language (https://kdl.dev/) | ||
| kdl = {}: { | ||
|
|
||
| type = (with lib.types; let | ||
| # https://github.com/kdl-org/kdl/blob/main/SPEC.md#value | ||
| untypedKdlValue = (nullOr (oneOf [ str bool number ])) // { description = "KDL value"; }; | ||
| kdlValue = either untypedKdlValue ((submodule { | ||
| options = { | ||
| type = lib.mkOption { | ||
| type = nullOr str; | ||
| default = null; | ||
| description = '' | ||
| [Type Annotation](https://github.com/kdl-org/kdl/blob/main/SPEC.md#type-annotation) of the value. | ||
| Set to `null` to prevent generating a type annotation. | ||
| ''; | ||
| }; | ||
| value = lib.mkOption { | ||
| type = untypedKdlValue; | ||
| description = '' | ||
| The actual KDL value. | ||
| ''; | ||
| }; | ||
| }; | ||
| }) // { description = "submodule: { type = /* type annotation */; value = /* KDL value */; }"; }); | ||
| node = submoduleWith { | ||
| modules = lib.toList { | ||
| options = { | ||
| name = lib.mkOption { | ||
| type = str; | ||
| description = '' | ||
| Name of [KDL node](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node). | ||
| ''; | ||
| }; | ||
| type = lib.mkOption { | ||
| type = nullOr str; | ||
| default = null; | ||
| description = '' | ||
| [Type Annotation](https://github.com/kdl-org/kdl/blob/main/SPEC.md#type-annotation) of [KDL node](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node). | ||
| Set to `null` to prevent generating a type annotation. | ||
| ''; | ||
| }; | ||
| arguments = lib.mkOption { | ||
| type = listOf kdlValue; | ||
| default = [ ]; | ||
| description = '' | ||
| [Arguments](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) of [KDL node](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node). | ||
| ''; | ||
| }; | ||
| properties = lib.mkOption { | ||
| type = attrsOf kdlValue; | ||
| default = { }; | ||
| description = '' | ||
| [Properties](https://github.com/kdl-org/kdl/blob/main/SPEC.md#property) of [KDL node](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node). | ||
| ''; | ||
| }; | ||
| children = lib.mkOption { | ||
| type = listOf (node // { | ||
| # Prevent Nix from trying to recurse into suboptions or submodules, as this leads to a stack overflow | ||
| getSubOptions = prefix: {}; | ||
| getSubModules = null; | ||
| }); | ||
| default = [ ]; | ||
| description = '' | ||
| [Children](https://github.com/kdl-org/kdl/blob/main/SPEC.md#children-block) of [KDL node](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node). | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
| description = "KDL node"; | ||
| }; | ||
| valueType = listOf node; | ||
| in | ||
| valueType); | ||
|
|
||
| lib = { | ||
| /** | ||
| Helper function for generating attrsets expect by pkgs.formats.kdl | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: expected |
||
|
|
||
| # Example | ||
|
|
||
| ```nix | ||
| let | ||
| settingsFormat = pkgs.formats.kdl { }; | ||
| inherit (settingsFormat.lib) node; | ||
| in | ||
| settingsFormat.generate "sample.kdl" [ | ||
| (node "foo" null [ ] { } [ | ||
| (node "bar" null [ "baz" ] { a = 1; } [ ]) | ||
| ]) | ||
| ] | ||
| ``` | ||
|
|
||
| # Arguments | ||
|
|
||
| name | ||
| : The name of the node, represented by a string | ||
|
|
||
| type | ||
| : The type annotation of the node, represented by a string, or null to avoid generating a type annotation | ||
|
|
||
| arguments | ||
| : The arguments of the node, represented as a list of KDL values | ||
|
|
||
| properties | ||
| : The properties of the node, represented as an attrset of KDL values | ||
|
|
||
| children | ||
| : The children of the node, represented as a list of nodes | ||
|
|
||
| */ | ||
| node = name: type: arguments: properties: children: { inherit name type arguments properties children; }; | ||
|
|
||
| /** | ||
| Helper function for generting the format of a typed value as expected by pkgs.formats.kdl | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: generating |
||
|
|
||
| type | ||
| : The type of the value, represented by a string | ||
|
|
||
| value | ||
| : The value itself | ||
| */ | ||
| typed = type: value: { inherit type value; }; | ||
| }; | ||
|
|
||
| generate = name: value: pkgs.callPackage ({ runCommand, json2kdl }: runCommand name { | ||
| nativeBuildInputs = [ json2kdl ]; | ||
| value = builtins.toJSON value; | ||
| passAsFile = [ "value" ]; | ||
| } '' | ||
| json2kdl "$valuePath" "$out" | ||
| '') {}; | ||
|
|
||
| }; | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.