plugins/none-ls: refactor using mkSourcePlugin

Introduce `_mk-source-plugin.nix`, which returns a module handling a
specific none-ls source plugin.

This wasn't possible previously due to IFD conflicting with evaluating
module imports.

Adjusted the test to use the `options` provided via module args, and
cleaned up some stuff allowing "unpackaged" sources to not be listed
again in the test file.
This commit is contained in:
Matt Sturgeon 2024-07-11 19:07:54 +01:00
parent 195978e627
commit d8f3113e90
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
4 changed files with 130 additions and 162 deletions

View file

@ -0,0 +1,58 @@
# mkSourcePlugin, returns a module
sourceType: sourceName:
{
config,
pkgs,
lib,
helpers,
...
}:
let
inherit (import ./packages.nix pkgs) packaged unpackaged;
cfg = config.plugins.none-ls;
cfg' = config.plugins.none-ls.sources.${sourceType}.${sourceName};
in
{
options.plugins.none-ls.sources.${sourceType}.${sourceName} =
{
enable = lib.mkEnableOption "the ${sourceName} ${sourceType} source for none-ls";
withArgs = helpers.mkNullOrOption helpers.nixvimTypes.strLua ''
Raw Lua code passed as an argument to the source's `with` method.
'';
}
# Only declare a package option if a package is required
// lib.optionalAttrs (packaged ? ${sourceName} || lib.elem sourceName unpackaged) {
package =
let
pkg = packaged.${sourceName} or null;
in
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 = pkg; }
);
};
config = lib.mkIf (cfg.enable && cfg'.enable) {
plugins.none-ls.settings.sources = lib.mkDefault [
(
"require('null-ls').builtins.${sourceType}.${sourceName}"
+ lib.optionalString (cfg'.withArgs != null) ".with(${cfg'.withArgs})"
)
];
extraPackages = [ (cfg'.package or null) ];
};
}

View file

@ -1,82 +1,22 @@
{ { config, lib, ... }:
pkgs,
config,
lib,
helpers,
...
}:
let let
noneLsBuiltins = import ../../generated/none-ls.nix; noneLsBuiltins = import ../../generated/none-ls.nix;
mkSourcePlugin = import ./_mk-source-plugin.nix;
inherit (import ./packages.nix pkgs) packaged unpackaged;
# Does this builitin require a package ?
builitinNeedsPackage = source: lib.hasAttr source packaged || lib.elem source unpackaged;
in in
{ {
imports = [ ./prettier.nix ]; imports =
[ ./prettier.nix ]
options.plugins.none-ls.sources = lib.mapAttrs ( ++ (lib.flatten (
sourceType: sources: lib.mapAttrsToList (category: (lib.map (mkSourcePlugin category))) noneLsBuiltins
lib.listToAttrs ( ));
lib.map (source: {
name = source;
value =
{
enable = lib.mkEnableOption "the ${source} ${sourceType} source for none-ls";
withArgs = helpers.mkNullOrOption helpers.nixvimTypes.strLua ''
Raw Lua code passed as an argument to the source's `with` method.
'';
}
// lib.optionalAttrs (builitinNeedsPackage source) {
package =
let
pkg = packaged.${source} or null;
in
lib.mkOption (
{
type = lib.types.nullOr lib.types.package;
description =
"Package to use for ${source} by none-ls. "
+ (lib.optionalString (pkg == null) ''
Not handled in nixvim, either install externally and set to null or set the option with a derivation.
'');
}
// lib.optionalAttrs (pkg != null) { default = pkg; }
);
};
}) sources
)
) noneLsBuiltins;
config = config =
let let
cfg = config.plugins.none-ls; cfg = config.plugins.none-ls;
gitsignsEnabled = cfg.sources.code_actions.gitsigns.enable; gitsignsEnabled = cfg.sources.code_actions.gitsigns.enable;
flattenedSources = lib.flatten (
lib.mapAttrsToList (
sourceType: sources:
(lib.mapAttrsToList (sourceName: source: source // { inherit sourceType sourceName; }) sources)
) cfg.sources
);
enabledSources = builtins.filter (source: source.enable) flattenedSources;
in in
lib.mkIf cfg.enable { lib.mkIf cfg.enable {
plugins.none-ls.settings.sources = lib.mkIf (enabledSources != [ ]) ( # Enable gitsigns if the gitsigns source is enabled
map (
{
sourceType,
sourceName,
withArgs,
...
}:
"require('null-ls').builtins.${sourceType}.${sourceName}"
+ lib.optionalString (withArgs != null) ".with(${withArgs})"
) enabledSources
);
plugins.gitsigns.enable = lib.mkIf gitsignsEnabled true; plugins.gitsigns.enable = lib.mkIf gitsignsEnabled true;
extraPackages = map (source: source.package or null) enabledSources;
}; };
} }

View file

@ -53,10 +53,6 @@ let
inherit pkgs lib helpers; inherit pkgs lib helpers;
config = { }; config = { };
}; };
nonels-sources-options = import ../plugins/none-ls/servers.nix {
inherit pkgs lib helpers;
config = { };
};
}; };
inherit namespace; inherit namespace;
} }

View file

@ -1,4 +1,3 @@
{ pkgs, nonels-sources-options, ... }:
{ {
# Empty configuration # Empty configuration
empty = { empty = {
@ -100,95 +99,70 @@
}; };
with-sources = { with-sources = {
plugins.none-ls = { module =
# sandbox-exec: pattern serialization length 159032 exceeds maximum (65535) {
enable = !pkgs.stdenv.isDarwin; config,
options,
lib,
pkgs,
...
}:
{
plugins.none-ls = {
# sandbox-exec: pattern serialization length 159032 exceeds maximum (65535)
enable = !pkgs.stdenv.isDarwin;
sources = sources =
let let
options = nonels-sources-options.options.plugins.none-ls.sources; disabled =
[
unpackaged = # As of 2024-03-22, pkgs.d2 is broken
[ # TODO: re-enable this test when fixed
"blade_formatter" "d2_fmt"
"bsfmt" # TODO: can this be re-enabled?
"bslint" "yamlfix"
"cljstyle" ]
"cueimports" ++ (lib.optionals (pkgs.stdenv.isDarwin && pkgs.stdenv.isx86_64) [
# As of 2024-03-22, pkgs.d2 is broken # As of 2024-03-27, pkgs.graalvm-ce (a dependency of pkgs.clj-kondo) is broken on x86_64-darwin
# TODO: re-enable this test when fixed # TODO: re-enable this test when fixed
"d2_fmt" "clj_kondo"
"erb_lint" ])
"findent" ++ (lib.optionals pkgs.stdenv.isDarwin [
"forge_fmt" # As of 2024-05-22, python311Packages.k5test (one of ansible-lint's dependenvies) is broken on darwin
"gccdiag" # TODO: re-enable this test when fixed
"gersemi" "ansible_lint"
"markuplint" "clazy"
"mlint" "gdformat"
"nginx_beautifier" "gdlint"
"npm_groovy_lint" "haml_lint"
"ocdc" # As of 2024-06-29, pkgs.rubyfmt is broken on darwin
"packer" # TODO: re-enable this test when fixed
"perlimports" "rubyfmt"
"pint" "verilator"
"pretty_php" "verible_verilog_format"
"purs_tidy" ])
"pyink" ++ (lib.optionals pkgs.stdenv.isAarch64 [
"reek" "semgrep"
"regal" "smlfmt"
"remark" # As of 2024-03-11, swift-format is broken on aarch64
"rescript" # TODO: re-enable this test when fixed
"saltlint" "swift_format"
"solhint" ]);
"spectral" in
"sqlfmt" # Enable every none-ls source that has an option
"sql_formatter" lib.mapAttrs (
"styler" _:
"stylint" lib.mapAttrs (
"swiftformat" sourceName: opts:
"swiftlint" {
"textidote" # Enable unless disabled above
"textlint" enable = !(lib.elem sourceName disabled);
"twigcs" }
"vacuum" # Some sources have a package option with no default
"yamlfix" // lib.optionalAttrs (opts ? package && !(opts.package ? default)) { package = null; }
] )
++ (pkgs.lib.optionals (pkgs.stdenv.isDarwin && pkgs.stdenv.isx86_64) [ ) options.plugins.none-ls.sources;
# As of 2024-03-27, pkgs.graalvm-ce (a dependency of pkgs.clj-kondo) is broken on x86_64-darwin };
# TODO: re-enable this test when fixed };
"clj_kondo"
])
++ (pkgs.lib.optionals pkgs.stdenv.isDarwin [
# As of 2024-05-22, python311Packages.k5test (one of ansible-lint's dependenvies) is broken on darwin
# TODO: re-enable this test when fixed
"ansible_lint"
"clazy"
"gdformat"
"gdlint"
"haml_lint"
# As of 2024-06-29, pkgs.rubyfmt is broken on darwin
# TODO: re-enable this test when fixed
"rubyfmt"
"verilator"
"verible_verilog_format"
])
++ (pkgs.lib.optionals pkgs.stdenv.isAarch64 [
"semgrep"
"smlfmt"
# As of 2024-03-11, swift-format is broken on aarch64
# TODO: re-enable this test when fixed
"swift_format"
]);
sources = pkgs.lib.mapAttrs (
_: sources:
pkgs.lib.mapAttrs (
source: _:
{ enable = true; } // pkgs.lib.optionalAttrs (builtins.elem source unpackaged) { package = null; }
) sources
) options;
in
sources;
};
}; };
} }