nixos-config/modules/flake-parts/setups/nixvim.nix

219 lines
6.5 KiB
Nix
Raw Normal View History

2024-01-25 14:49:57 +00:00
{ inputs
, lib
, config
, ...
}:
2024-01-25 14:49:57 +00:00
let
cfg = config.setups.nixvim;
nixvimModules = ../../nixvim;
2024-01-25 14:49:57 +00:00
mkNixvimConfig = {
system,
pkgs,
nixvimBranch ? "nixvim",
modules ? [ ],
specialArgs ? { },
}:
inputs.${nixvimBranch}.legacyPackages.${system}.makeNixvimWithModule {
2024-01-25 14:49:57 +00:00
inherit pkgs;
module = {
imports = modules;
};
extraSpecialArgs = specialArgs // {
foodogsquaredModulesPath = builtins.toString nixvimModules;
};
2024-01-25 14:49:57 +00:00
};
modulesOption = lib.mkOption {
type = with lib.types; listOf deferredModule;
2024-02-02 04:40:16 +00:00
default = [ ];
2024-01-25 14:49:57 +00:00
};
modulesOption' = configEnv: modulesOption // {
description = ''
A list of NixVim modules to be applied across all NixVim configurations
when imported as part of ${configEnv}.
'';
};
componentType = { lib, config, ... }: {
2024-01-25 14:49:57 +00:00
options = {
nixpkgsBranch = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "nixpkgs";
example = "nixos-unstable";
2024-01-25 14:49:57 +00:00
description = ''
The nixpkgs branch for the NixVim configuration to be built
2024-01-25 14:49:57 +00:00
against.
::: {.note}
This refers to your flake inputs so in order to support multiple
nixpkgs branches, you need to import multiple nixpkgs as part of the
`inputs` flake attribute.
:::
2024-01-25 14:49:57 +00:00
'';
};
nixvimBranch = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "nixvim";
example = "nixvim-unstable";
description = ''
The NixVim branch to be used for the NixVim configuration.
It is recommend to match the NixVim branch with the nixpkgs branch.
For example, a NixVim configuration of `nixos-24.05` should be paired
with nixpkgs `nixos-24.05` branch.
::: {.note}
This refers to your flake inputs so in order to support multiple
NixVim branches, you need to import multiple NixVim branches as part
of the `inputs` flake attribute.
:::
'';
};
neovimPackage = lib.mkOption {
type = with lib.types; functionTo package;
default = pkgs: pkgs.neovim;
defaultText = "pkgs: pkgs.neovim";
example = lib.literalExpression ''
pkgs: pkgs.neovim-nightly
'';
description = ''
The package to be used for the NixVim configuration. Since this is
used per-system, it has to be a function returning a package from the
given nixpkgs instance.
'';
};
overlays = lib.mkOption {
type = with lib.types; listOf (functionTo raw);
default = [ ];
example = lib.literalExpression ''
[
inputs.neovim-nightly-overlay.overlays.default
]
'';
description = ''
A list of overlays to be applied for the nixpkgs instance.
'';
};
2024-01-25 14:49:57 +00:00
};
};
configType = { name, lib, config, ... }: {
options = {
components = lib.mkOption {
type = with lib.types; listOf (submodule componentType);
description = ''
A list of specific components for the NixVim configuration to be
built against.
'';
example = [
{ nixpkgsBranch = "nixos-unstable"; nixvimBranch = "nixvim-unstable"; }
{ nixpkgsBranch = "nixos-stable"; nixvimBranch = "nixvim-stable"; }
{ nixpkgsBranch = "nixos-stable"; nixvimBranch = "nixvim-stable"; neovimPackage = pkgs: pkgs.neovim-nightly; }
];
};
};
2024-01-25 14:49:57 +00:00
config = {
modules = [
../../../configs/nixvim/${config.configName}
2024-01-25 14:49:57 +00:00
];
};
};
in
{
options.setups.nixvim = {
configs = lib.mkOption {
type = with lib.types; attrsOf (submoduleWith {
specialArgs = { inherit (config) systems; };
modules = [
./shared/config-options.nix
configType
];
});
2024-02-02 04:40:16 +00:00
default = { };
2024-01-25 14:49:57 +00:00
description = ''
A set of NixVim configurations to be integrated into the declarative
setups configuration. Each of them will be available as part of
`nixvimConfigurations`.
'';
};
sharedModules = modulesOption // {
description = ''
A list of NixVim modules to be shared across all of the NixVim
configurations. This is also to be shared among wider-scoped
environments when NixVim-specific integrations has been enabled.
2024-01-25 14:49:57 +00:00
'';
};
standaloneConfigModules = modulesOption' "standalone configuration";
};
2024-02-02 04:40:16 +00:00
config = lib.mkIf (cfg.configs != { }) {
setups.nixvim.sharedModules = [
nixvimModules
# Import our private modules.
../../nixvim/_private
];
2024-01-25 14:49:57 +00:00
perSystem = { system, config, lib, ... }:
(
let
validConfigs = lib.filterAttrs
2024-02-02 04:40:16 +00:00
(_: metadata: lib.elem system metadata.systems)
cfg.configs;
2024-01-25 14:49:57 +00:00
nixvimConfigurations =
let
generateNixvimConfigs = name: metadata:
let
mkNixvimConfig' = component:
let
pkgs = import inputs.${component.nixpkgsBranch} {
inherit (component) overlays;
inherit system;
};
neovimPackage = component.neovimPackage pkgs;
in
lib.nameValuePair
"${name}-${component.nixpkgsBranch}-${neovimPackage.pname}"
(mkNixvimConfig {
inherit system pkgs;
inherit (component) nixvimBranch;
modules =
cfg.sharedModules
++ cfg.standaloneConfigModules
++ metadata.modules
++ [{ package = neovimPackage; }];
});
nixvimConfigs = builtins.map mkNixvimConfig' metadata.components;
2024-01-25 14:49:57 +00:00
in
lib.listToAttrs nixvimConfigs;
in
lib.concatMapAttrs generateNixvimConfigs validConfigs;
in
{
# We'll reuse these.
inherit nixvimConfigurations;
checks =
lib.mapAttrs'
(name: nvim:
lib.nameValuePair
"nixvim-check-${name}"
(inputs.nixvim.lib.${system}.check.mkTestDerivationFromNvim {
2024-02-02 04:40:16 +00:00
inherit nvim;
name = "${name} configuration";
2024-01-25 14:49:57 +00:00
}))
nixvimConfigurations;
}
);
};
}