From e0521dde87825e4ed16e1ac5b6df9f1b7e60af05 Mon Sep 17 00:00:00 2001 From: traxys Date: Fri, 22 Dec 2023 16:55:50 +0100 Subject: [PATCH] helpers: Allow to set raw lua code in more places (#837) With this change a user should be able to set raw lua code in more places than before. The `mkStr` helper was _not_ changed as a large number of places in nixvim are using this helper to directly implement raw lua code. Also note that types of the form `oneOf [submodule ` are not well rendered in the documentation, and should be avoided (this includes `listOf [submodule `, so the helpers should be avoided in this case) --- lib/helpers.nix | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/helpers.nix b/lib/helpers.nix index 44f9da18..a3d31426 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -119,7 +119,9 @@ with lib; rec { mkCompositeOption = desc: options: mkNullOrOption (types.submodule {inherit options;}) desc; - defaultNullOpts = rec { + defaultNullOpts = let + maybeRaw = t: lib.types.either t rawType; + in rec { mkNullable = type: default: desc: mkNullOrOption type ( let @@ -134,21 +136,31 @@ with lib; rec { '' ); - mkNum = default: mkNullable lib.types.number (toString default); - mkInt = default: mkNullable lib.types.int (toString default); + # Note that this function is _not_ to be used with submodule elements, as it may obstruct the + # documentation + mkNullableWithRaw = type: mkNullable (maybeRaw type); + + mkNum = default: mkNullable (maybeRaw lib.types.number) (toString default); + mkInt = default: mkNullable (maybeRaw lib.types.int) (toString default); # Positive: >0 - mkPositiveInt = default: mkNullable lib.types.ints.positive (toString default); + mkPositiveInt = default: mkNullable (maybeRaw lib.types.ints.positive) (toString default); # Unsigned: >=0 - mkUnsignedInt = default: mkNullable lib.types.ints.unsigned (toString default); + mkUnsignedInt = default: mkNullable (maybeRaw lib.types.ints.unsigned) (toString default); mkBool = default: - mkNullable lib.types.bool ( + mkNullable (maybeRaw lib.types.bool) ( if default then "true" else "false" ); mkStr = default: mkNullable lib.types.str ''${builtins.toString default}''; mkAttributeSet = default: mkNullable lib.types.attrs ''${default}''; - mkEnum = enum: default: mkNullable (lib.types.enum enum) ''"${default}"''; + # Note that this function is _not_ to be used with submodule elements, as it may obstruct the + # documentation + mkListOf = ty: default: mkNullable (lib.types.listOf (maybeRaw ty)) default; + # Note that this function is _not_ to be used with submodule elements, as it may obstruct the + # documentation + mkAttrsOf = ty: default: mkNullable (lib.types.attrsOf (maybeRaw ty)) default; + mkEnum = enum: default: mkNullable (maybeRaw (lib.types.enum enum)) ''"${default}"''; mkEnumFirstDefault = enum: mkEnum enum (head enum); mkBorder = default: name: desc: mkNullable