From 57003fea4e0dce32b518c1a1356f9de9226ff141 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 16:26:24 +0100 Subject: [PATCH 01/12] lib/options: inline `mkDefaultDesc` into `mkDesc` And add function documentation. --- lib/options.nix | 52 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 07cc9feff0..1d71a6c2b7 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -56,26 +56,46 @@ rec { }; defaultNullOpts = rec { - # Description helpers - mkDefaultDesc = - defaultValue: - let - default = - # Assume a string default is already formatted as intended, - # historically strings were the only type accepted here. - # TODO consider deprecating this behavior so we can properly quote strings - if isString defaultValue then defaultValue else generators.toPretty { } defaultValue; - in - "Plugin default:" - + ( - # Detect whether `default` is multiline or inline: - if hasInfix "\n" default then "\n\n```nix\n${default}\n```" else " `${default}`" - ); + /** + Build a description with a plugin default. + + The [default] can be any value, and it will be formatted using `lib.generators.toPretty`. + + If [default] is a String, it will not be formatted. + This behavior will likely change in the future. + # Example + ```nix + mkDesc 1 "foo" + => '' + foo + + Plugin default: `1` + '' + ``` + + # Type + ``` + mkDesc :: Any -> String -> String + ``` + + # Arguments + - [default] The plugin's default + - [desc] The option's description + */ mkDesc = default: desc: let - defaultDesc = mkDefaultDesc default; + # Assume a string default is already formatted as intended, + # historically strings were the only type accepted here. + # TODO deprecate this behavior so we can properly quote strings + defaultString = if isString default then default else generators.toPretty { } default; + defaultDesc = + "Plugin default:" + + ( + # Detect whether `default` is multiline or inline: + if hasInfix "\n" defaultString then "\n\n```nix\n${defaultString}\n```" else " `${defaultString}`" + ); in if desc == "" then defaultDesc From fc542329cd023c0909ef7244ddda31c788e0fccd Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:14:45 +0100 Subject: [PATCH 02/12] lib/options: add `mkNullOrOption'` variant --- lib/options.nix | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 1d71a6c2b7..d8d268e79c 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -7,13 +7,20 @@ with lib; with nixvimUtils; rec { # Creates an option with a nullable type that defaults to null. - mkNullOrOption = - type: desc: - lib.mkOption { - type = lib.types.nullOr type; - default = null; - description = desc; - }; + mkNullOrOption' = + { + type, + default ? null, + ... + }@args: + lib.mkOption ( + args + // { + type = lib.types.nullOr type; + inherit default; + } + ); + mkNullOrOption = type: description: mkNullOrOption' { inherit type description; }; mkCompositeOption = desc: options: mkNullOrOption (types.submodule { inherit options; }) desc; From 9bf7724b98250aeca9d4c478c8035594f813ccd6 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 18:41:49 +0100 Subject: [PATCH 03/12] lib/options: add `mkCompositeOption'` variant --- lib/options.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/options.nix b/lib/options.nix index d8d268e79c..070c22c974 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -22,7 +22,12 @@ rec { ); mkNullOrOption = type: description: mkNullOrOption' { inherit type description; }; - mkCompositeOption = desc: options: mkNullOrOption (types.submodule { inherit options; }) desc; + mkCompositeOption' = + { options, ... }@args: + mkNullOrOption' ( + (filterAttrs (n: _: n != "options") args) // { type = types.submodule { inherit options; }; } + ); + mkCompositeOption = description: options: mkCompositeOption' { inherit description options; }; mkNullOrStr = mkNullOrOption (with nixvimTypes; maybeRaw str); From 3a151bbf094977668d85379b67154a5eb0d76c68 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:16:13 +0100 Subject: [PATCH 04/12] lib/options: add `mkNullOrStr'` variant --- lib/options.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/options.nix b/lib/options.nix index 070c22c974..a630127ce3 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -29,7 +29,8 @@ rec { ); mkCompositeOption = description: options: mkCompositeOption' { inherit description options; }; - mkNullOrStr = mkNullOrOption (with nixvimTypes; maybeRaw str); + mkNullOrStr' = args: mkNullOrOption' (args // { type = with nixvimTypes; maybeRaw str; }); + mkNullOrStr = description: mkNullOrStr' { inherit description; }; mkNullOrLua = desc: From 207bfc6e694a1da256e25fc69a96172bf40995a0 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:16:50 +0100 Subject: [PATCH 05/12] lib/options: add `mkNullOrLua'` variant --- lib/options.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index a630127ce3..6311f49b23 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -32,14 +32,16 @@ rec { mkNullOrStr' = args: mkNullOrOption' (args // { type = with nixvimTypes; maybeRaw str; }); mkNullOrStr = description: mkNullOrStr' { inherit description; }; - mkNullOrLua = - desc: - lib.mkOption { - type = lib.types.nullOr nixvimTypes.strLua; - default = null; - description = desc; - apply = mkRaw; - }; + mkNullOrLua' = + args: + mkNullOrOption' ( + args + // { + type = nixvimTypes.strLua; + apply = mkRaw; + } + ); + mkNullOrLua = description: mkNullOrLua' { inherit description; }; mkNullOrLuaFn = desc: From 56ee982cb47c23f81fc67293bf88056c3eb98085 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:17:13 +0100 Subject: [PATCH 06/12] lib/options: add `mkNullOrLuaFn'` variant --- lib/options.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 6311f49b23..ba715b718c 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -43,14 +43,16 @@ rec { ); mkNullOrLua = description: mkNullOrLua' { inherit description; }; - mkNullOrLuaFn = - desc: - lib.mkOption { - type = lib.types.nullOr nixvimTypes.strLuaFn; - default = null; - description = desc; - apply = mkRaw; - }; + mkNullOrLuaFn' = + args: + mkNullOrOption' ( + args + // { + type = nixvimTypes.strLuaFn; + apply = mkRaw; + } + ); + mkNullOrLuaFn = description: mkNullOrLua' { inherit description; }; mkNullOrStrLuaOr = ty: desc: From 5bcb6184b0c1ef2c16e79db964eb1d0f639ba4df Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:17:43 +0100 Subject: [PATCH 07/12] lib/options: add `mkNullOrStrLuaOr'` variant --- lib/options.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index ba715b718c..20fc4a8e11 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -54,14 +54,16 @@ rec { ); mkNullOrLuaFn = description: mkNullOrLua' { inherit description; }; - mkNullOrStrLuaOr = - ty: desc: - lib.mkOption { - type = lib.types.nullOr (types.either nixvimTypes.strLua ty); - default = null; - description = desc; - apply = v: if builtins.isString v then mkRaw v else v; - }; + mkNullOrStrLuaOr' = + { type, ... }@args: + mkNullOrOption' ( + args + // { + type = with nixvimTypes; either strLua type; + apply = v: if isString v then mkRaw v else v; + } + ); + mkNullOrStrLuaOr = type: description: mkNullOrStrLuaOr' { inherit type description; }; mkNullOrStrLuaFnOr = ty: desc: From ed56221499b68d57d1db87029070cf97e2b43f91 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:18:17 +0100 Subject: [PATCH 08/12] lib/options: add `mkNullOrStrLuaFnOr'` variant --- lib/options.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 20fc4a8e11..534ee87f39 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -65,14 +65,16 @@ rec { ); mkNullOrStrLuaOr = type: description: mkNullOrStrLuaOr' { inherit type description; }; - mkNullOrStrLuaFnOr = - ty: desc: - lib.mkOption { - type = lib.types.nullOr (types.either nixvimTypes.strLuaFn ty); - default = null; - description = desc; - apply = v: if builtins.isString v then mkRaw v else v; - }; + mkNullOrStrLuaFnOr' = + { type, ... }@args: + mkNullOrOption' ( + args + // { + type = with nixvimTypes; either strLuaFn type; + apply = v: if isString v then mkRaw v else v; + } + ); + mkNullOrStrLuaFnOr = type: description: mkNullOrStrLuaFnOr' { inherit type description; }; defaultNullOpts = rec { /** From 84b2b0d90c2434be99c648edf5957ebd9ceb76e9 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:18:58 +0100 Subject: [PATCH 09/12] lib/options: add `mkNullable'` variant --- lib/options.nix | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 534ee87f39..7fe7d3b262 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -127,9 +127,18 @@ rec { ${defaultDesc} ''; + mkNullable' = + { default, description, ... }@args: + mkNullOrOption' ( + args + // { + default = null; + description = mkDesc default description; + } + ); mkNullable = - type: default: desc: - mkNullOrOption type (mkDesc default desc); + type: default: description: + mkNullable' { inherit type default description; }; mkNullableWithRaw = type: mkNullable (nixvimTypes.maybeRaw type); From e0b60bac8b7c9fda6c4555ac2e497b2e758711a0 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:19:20 +0100 Subject: [PATCH 10/12] lib/options: add `mkNullableWithRaw'` variant --- lib/options.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/options.nix b/lib/options.nix index 7fe7d3b262..632642dc38 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -140,7 +140,11 @@ rec { type: default: description: mkNullable' { inherit type default description; }; - mkNullableWithRaw = type: mkNullable (nixvimTypes.maybeRaw type); + mkNullableWithRaw' = + { type, ... }@args: mkNullable' (args // { type = nixvimTypes.maybeRaw type; }); + mkNullableWithRaw = + type: default: description: + mkNullableWithRaw' { inherit type default description; }; mkStrLuaOr = type: default: desc: From 1bb4cb9c6c334a6d52755066b1747547da137b00 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 19:19:48 +0100 Subject: [PATCH 11/12] lib/options: add `mkAttrsOf'` & `mkListOf'` (etc) --- lib/options.nix | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 632642dc38..b1152eacf0 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -171,9 +171,22 @@ rec { default: assert default == null || isString default; mkNullableWithRaw types.str (generators.toPretty { } default); - mkAttributeSet = mkNullable nixvimTypes.attrs; - mkListOf = ty: default: mkNullable (with nixvimTypes; listOf (maybeRaw ty)) default; - mkAttrsOf = ty: default: mkNullable (with nixvimTypes; attrsOf (maybeRaw ty)) default; + + mkAttributeSet' = args: mkNullable' (args // { type = nixvimTypes.attrs; }); + mkAttributeSet = default: description: mkAttributeSet' { inherit default description; }; + + mkListOf' = + { type, ... }@args: mkNullable' (args // { type = with nixvimTypes; listOf (maybeRaw type); }); + mkListOf = + type: default: description: + mkListOf' { inherit type default description; }; + + mkAttrsOf' = + { type, ... }@args: mkNullable' (args // { type = with nixvimTypes; attrsOf (maybeRaw type); }); + mkAttrsOf = + type: default: description: + mkAttrsOf' { inherit type default description; }; + mkEnum = enumValues: default: mkNullableWithRaw (types.enum enumValues) ( From 297aa6d0a28c3ade20e669c08e7d5f6a3f519782 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 31 May 2024 17:28:31 +0100 Subject: [PATCH 12/12] lib/options: make `mkPackageOption` use `mkNullOrOption'` --- lib/options.nix | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index b1152eacf0..6e8d5cd965 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -248,23 +248,25 @@ rec { mkPackageOption = { - name ? null, # Can be null if a custom description is given. - default, + name ? null, # Can be omitted if a custom description is given. description ? null, - example ? null, - }: - mkOption { - type = with types; nullOr package; - inherit default example; - description = - if description == null then - '' - Which package to use for `${name}`. - Set to `null` to disable its automatic installation. - '' - else - description; - }; + default, # `default` is not optional + ... + }@args: + mkNullOrOption' ( + (filterAttrs (n: _: n != "name") args) + // { + type = types.package; + description = + if description == null then + '' + Which package to use for `${name}`. + Set to `null` to disable its automatic installation. + '' + else + description; + } + ); mkPluginPackageOption = name: default: