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 <something else>` are
not well rendered in the documentation, and should be avoided (this
includes `listOf [submodule <something else>`, so the helpers should be
avoided in this case)
This commit is contained in:
traxys 2023-12-22 16:55:50 +01:00 committed by GitHub
parent 2871dc94b4
commit e0521dde87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -119,7 +119,9 @@ with lib; rec {
mkCompositeOption = desc: options: mkCompositeOption = desc: options:
mkNullOrOption (types.submodule {inherit options;}) desc; mkNullOrOption (types.submodule {inherit options;}) desc;
defaultNullOpts = rec { defaultNullOpts = let
maybeRaw = t: lib.types.either t rawType;
in rec {
mkNullable = type: default: desc: mkNullable = type: default: desc:
mkNullOrOption type ( mkNullOrOption type (
let let
@ -134,21 +136,31 @@ with lib; rec {
'' ''
); );
mkNum = default: mkNullable lib.types.number (toString default); # Note that this function is _not_ to be used with submodule elements, as it may obstruct the
mkInt = default: mkNullable lib.types.int (toString default); # 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 # Positive: >0
mkPositiveInt = default: mkNullable lib.types.ints.positive (toString default); mkPositiveInt = default: mkNullable (maybeRaw lib.types.ints.positive) (toString default);
# Unsigned: >=0 # Unsigned: >=0
mkUnsignedInt = default: mkNullable lib.types.ints.unsigned (toString default); mkUnsignedInt = default: mkNullable (maybeRaw lib.types.ints.unsigned) (toString default);
mkBool = default: mkBool = default:
mkNullable lib.types.bool ( mkNullable (maybeRaw lib.types.bool) (
if default if default
then "true" then "true"
else "false" else "false"
); );
mkStr = default: mkNullable lib.types.str ''${builtins.toString default}''; mkStr = default: mkNullable lib.types.str ''${builtins.toString default}'';
mkAttributeSet = default: mkNullable lib.types.attrs ''${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); mkEnumFirstDefault = enum: mkEnum enum (head enum);
mkBorder = default: name: desc: mkBorder = default: name: desc:
mkNullable mkNullable