lib: Harmonize package options which may not exist in nixpkgs

This commit is contained in:
Quentin Boyer 2025-01-30 22:53:55 +01:00 committed by nix-infra-bot
parent eb611349a7
commit a2f01876f7
6 changed files with 57 additions and 42 deletions

View file

@ -54,6 +54,7 @@ lib.makeExtensible (
mkCompositeOption
mkCompositeOption'
mkLazyLoadOption
mkMaybeUnpackagedOption
mkNullOrLua
mkNullOrLua'
mkNullOrLuaFn
@ -69,6 +70,7 @@ lib.makeExtensible (
mkPackageOption
mkPluginPackageOption
mkSettingsOption
mkUnpackagedOption
pluginDefaultText
;

View file

@ -403,5 +403,43 @@ rec {
if cfg ? lazyLoad then lib.literalMD "`false` when lazy-loading is enabled." else true;
example = false;
};
/**
Create an option for a package not currently available in nixpkgs.
The option will throw an error if a value is not explicitly set by the end user.
*/
mkUnpackagedOption =
optionName: packageName:
lib.mkOption {
type = types.nullOr types.package;
description = ''
Package to use for ${packageName}.
Nixpkgs does not include this package, and as such an external derivation or null must be provided.
'';
default = throw ''
Nixvim (${optionName}): No package is known for ${packageName}, to resolve this either:
- install externally and set this option to `null`
- or provide a derviation to install this package
'';
defaultText = lib.literalMD "No package, throws when undefined";
};
/**
Create an option for a package that may not be currently available in nixpkgs.
See `mkUnpackagedOption`
*/
mkMaybeUnpackagedOption =
optionName: pkgs: packageName: package:
if lib.isOption package then
package
else if package != null then
lib.mkPackageOption pkgs packageName {
nullable = true;
default = package;
}
else
mkUnpackagedOption optionName packageName;
}
// removed

View file

@ -9,7 +9,6 @@ sourceType: sourceName:
let
inherit (import ./packages.nix lib) packaged;
pkg = packaged.${sourceName};
loc = lib.toList pkg;
cfg = config.plugins.none-ls;
cfg' = config.plugins.none-ls.sources.${sourceType}.${sourceName};
@ -41,27 +40,11 @@ in
}
# Only declare a package option if a package is required
// lib.optionalAttrs (packaged ? ${sourceName}) {
package = lib.mkOption (
{
type = lib.types.nullOr lib.types.package;
description =
"Package to use for ${sourceName}."
+ (lib.optionalString (pkg == null) (
"\n\n"
+ ''
Currently not packaged in nixpkgs.
Either set this to `null` and install ${sourceName} outside of nix,
or set this to a custom nix package.
''
));
}
// lib.optionalAttrs (pkg != null) {
default =
lib.attrByPath loc (lib.warn "${lib.concatStringsSep "." loc} cannot be found in pkgs!" null)
pkgs;
defaultText = lib.literalExpression "pkgs.${lib.concatStringsSep "." loc}";
}
);
package =
lib.nixvim.mkMaybeUnpackagedOption "plugins.none-ls.sources.${sourceType}.${sourceName}.package"
pkgs
sourceName
pkg;
};
# TODO: Added 2024-07-16; remove after 24.11

View file

@ -37,7 +37,8 @@ in
{
meta.nixvimInfo = {
# TODO: description
url = args.url or opts.package.default.meta.homepage or null;
# The package default can throw when the server is unpackaged, so use tryEval
url = args.url or (builtins.tryEval opts.package.default).value.meta.homepage or null;
path = [
"plugins"
"lsp"
@ -51,21 +52,8 @@ in
enable = lib.mkEnableOption description;
package =
if lib.isOption package then
package
else if args ? package then
lib.mkPackageOption pkgs name {
nullable = true;
default = package;
}
else
# If we're not provided a package, we should provide a no-default option
lib.mkOption {
type = types.nullOr types.package;
description = ''
The package to use for ${name}. Has no default, but can be set to null.
'';
};
lib.nixvim.mkMaybeUnpackagedOption "plugins.lsp.servers.${name}.package" pkgs name
package;
cmd = mkOption {
type = with types; nullOr (listOf str);

View file

@ -82,7 +82,10 @@ let
{
enable = !(lib.elem server disabled);
}
// lib.optionalAttrs (opts ? package && !(opts.package ? default)) { package = null; }
# Some servers are defined using mkUnpackagedOption whose default will throw
// lib.optionalAttrs (opts ? package && !(builtins.tryEval opts.package.default).success) {
package = null;
}
))
(lib.filterAttrs (server: _: !(lib.elem server renamed)))
];

View file

@ -100,7 +100,6 @@
with-sources =
{
config,
options,
lib,
pkgs,
@ -158,8 +157,10 @@
# Enable unless disabled above
enable = !(lib.elem sourceName disabled);
}
# Some sources have a package option with no default
// lib.optionalAttrs (opts ? package && !(opts.package ? default)) { package = null; }
# Some sources are defined using mkUnpackagedOption whose default will throw
// lib.optionalAttrs (opts ? package && !(builtins.tryEval opts.package.default).success) {
package = null;
}
)
) options.plugins.none-ls.sources;
};