modules: refactor module bootstrapping

Let's simplify things by defining all modules in `./plugins`, `./modules`
and `./wrappers/modules`.

Instead of currying `pkgs` into a bootstrapping module, we can require
`defaultPkgs` be provided as a special arg.

This refactor allows us to completely remove `flake-modules/modules.nix`!
This commit is contained in:
Matt Sturgeon 2024-07-02 13:30:29 +01:00
parent 3d96960348
commit d2afb176ff
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
16 changed files with 129 additions and 159 deletions

View file

@ -1,8 +1,4 @@
{
rawModules,
helpers,
pkgs,
}:
{ helpers, pkgs }:
let
# Extend nixpkg's lib, so that we can handle recursive leaf types such as `either`
lib = pkgs.lib.extend (
@ -68,6 +64,7 @@ let
};
topLevelModules = [
../modules
../wrappers/modules/output.nix
# Fake module to avoid a duplicated documentation
(lib.setDefaultModuleLocation "${nixvimPath}/wrappers/modules/files.nix" {
@ -85,7 +82,7 @@ let
};
};
})
] ++ (rawModules pkgsDoc);
];
hmOptions = builtins.removeAttrs (lib.evalModules {
modules = [ (import ../wrappers/modules/hm.nix { inherit lib; }) ];
@ -97,7 +94,10 @@ rec {
inherit
(lib.evalModules {
modules = topLevelModules;
specialArgs.helpers = helpers;
specialArgs = {
inherit helpers;
defaultPkgs = pkgsDoc;
};
})
options
;

View file

@ -12,7 +12,8 @@ let
options = lib.evalModules {
inherit modules;
specialArgs = {
inherit pkgs lib helpers;
inherit helpers;
defaultPkgs = pkgs;
};
};

View file

@ -5,7 +5,6 @@
./helpers.nix
./lib.nix
./legacy-packages.nix
./modules.nix
./overlays.nix
./packages.nix
./templates.nix

View file

@ -1,85 +0,0 @@
{ modules, ... }:
{
_module.args = {
modules =
pkgs:
let
nixpkgsMaintainersList = pkgs.path + "/nixos/modules/misc/meta.nix";
nixvimExtraArgsModule = rec {
_file = ./flake.nix;
key = _file;
config = {
_module.args = {
pkgs = pkgs.lib.mkForce pkgs;
inherit (pkgs) lib;
};
};
};
in
[
../modules
nixpkgsMaintainersList
nixvimExtraArgsModule
(
{ lib, ... }:
with lib;
{
# Attribute may contain the following fields:
# - path: Path to the module, e.g. [ "plugins" "<name>" ]
# - description: A short description of the plugin
# - url: Url for the plugin
#
# We need to use an attrs instead of a submodule to handle the merge.
options.meta.nixvimInfo = mkOption {
type = (types.nullOr types.attrs) // {
# This will create an attrset of the form:
#
# { path.to.plugin.name = <info>; }
#
#
# Where <info> is an attrset of the form:
# {
# file = "path";
# description = null or "<DESCRIPTION>";
# url = null or "<URL>";
# }
merge =
_: defs:
lib.foldl'
(
acc: def:
lib.recursiveUpdate acc (
setAttrByPath def.value.path {
inherit (def) file;
url = def.value.url or null;
description = def.value.description or null;
}
)
)
{
plugins = { };
colorschemes = { };
}
defs;
};
internal = true;
default = null;
description = ''
Nixvim related information on the module
'';
};
}
)
];
};
perSystem =
{ pkgs, config, ... }:
{
_module.args = {
modules = modules pkgs;
rawModules = modules;
};
};
}

View file

@ -3,13 +3,12 @@
{
pkgsUnfree,
config,
rawModules,
helpers,
...
}:
{
packages = import ../docs {
inherit rawModules helpers;
inherit helpers;
# Building the docs evaluates each plugin's default package, some of which are unfree
pkgs = pkgsUnfree;
};

View file

@ -1,13 +1,11 @@
{
inputs,
modules,
getHelpers,
self,
...
}:
let
wrapperArgs = {
inherit modules;
inherit self;
inherit getHelpers;
};

View file

@ -1,5 +1,6 @@
{
imports = [
./misc
./autocmd.nix
./clipboard.nix
./colorscheme.nix

13
modules/misc/default.nix Normal file
View file

@ -0,0 +1,13 @@
{ defaultPkgs, ... }:
let
# We can't use config._module.args to define imports,
# so we're forced to use specialArgs.defaultPkgs's path
nixosModules = defaultPkgs.path + "/nixos/modules/";
in
{
imports = [
./nixpkgs.nix
./nixvim-info.nix
(nixosModules + "/misc/meta.nix")
];
}

24
modules/misc/nixpkgs.nix Normal file
View file

@ -0,0 +1,24 @@
{
defaultPkgs,
pkgs,
lib,
...
}:
let
# TODO: https://github.com/nix-community/nixvim/issues/1784
finalPackage = defaultPkgs;
in
{
config = {
_module.args = {
# We explicitly set the default override priority, so that we do not need
# to evaluate finalPkgs in case an override is placed on `_module.args.pkgs`.
# After all, to determine a definition priority, we need to evaluate `._type`,
# which is somewhat costly for Nixpkgs. With an explicit priority, we only
# evaluate the wrapper to find out that the priority is lower, and then we
# don't need to evaluate `finalPkgs`.
pkgs = lib.mkOverride lib.modules.defaultOverridePriority finalPackage;
inherit (pkgs) lib;
};
};
}

View file

@ -0,0 +1,48 @@
{ lib, ... }:
with lib;
{
# Attribute may contain the following fields:
# - path: Path to the module, e.g. [ "plugins" "<name>" ]
# - description: A short description of the plugin
# - url: Url for the plugin
#
# We need to use an attrs instead of a submodule to handle the merge.
options.meta.nixvimInfo = mkOption {
type = (types.nullOr types.attrs) // {
# This will create an attrset of the form:
#
# { path.to.plugin.name = <info>; }
#
#
# Where <info> is an attrset of the form:
# {
# file = "path";
# description = null or "<DESCRIPTION>";
# url = null or "<URL>";
# }
merge =
_: defs:
lib.foldl'
(
acc: def:
lib.recursiveUpdate acc (
setAttrByPath def.value.path {
inherit (def) file;
url = def.value.url or null;
description = def.value.description or null;
}
)
)
{
plugins = { };
colorschemes = { };
}
defs;
};
internal = true;
default = null;
description = ''
Nixvim related information on the module
'';
};
}

View file

@ -1,26 +1,15 @@
{ modules, helpers }:
{
lib,
pkgs,
config,
...
}:
helpers:
{ lib, config, ... }:
let
inherit (lib)
mkEnableOption
mkOption
mkOptionType
mkForce
mkMerge
mkIf
types
;
inherit (lib) mkOption mkOptionType;
cfg = config.programs.nixvim;
in
{
topLevelModules = [
../modules
./modules/output.nix
(import ./modules/files.nix (modules pkgs))
] ++ (modules pkgs);
./modules/files.nix
];
helpers = mkOption {
type = mkOptionType {
@ -33,9 +22,6 @@ in
};
configFiles =
let
cfg = config.programs.nixvim;
in
(lib.mapAttrs' (_: file: lib.nameValuePair "nvim/${file.path}" { text = file.content; }) cfg.files)
// (lib.mapAttrs' (
path: content: lib.nameValuePair "nvim/${path}" { text = content; }

View file

@ -1,8 +1,4 @@
{
modules,
self,
getHelpers,
}:
{ self, getHelpers }:
{
pkgs,
config,
@ -20,7 +16,7 @@ let
types
;
helpers = getHelpers pkgs false;
shared = import ./_shared.nix { inherit modules helpers; } args;
shared = import ./_shared.nix helpers args;
cfg = config.programs.nixvim;
in
{
@ -31,6 +27,7 @@ in
shorthandOnlyDefinesConfig = true;
specialArgs = {
darwinConfig = config;
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/darwin.nix ] ++ shared.topLevelModules;

View file

@ -1,8 +1,4 @@
{
modules,
self,
getHelpers,
}:
{ self, getHelpers }:
{
pkgs,
config,
@ -19,7 +15,7 @@ let
types
;
helpers = getHelpers pkgs false;
shared = import ./_shared.nix { inherit modules helpers; } args;
shared = import ./_shared.nix helpers args;
cfg = config.programs.nixvim;
files = shared.configFiles // {
"nvim/init.lua".text = cfg.initContent;
@ -33,6 +29,7 @@ in
shorthandOnlyDefinesConfig = true;
specialArgs = {
hmConfig = config;
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/hm.nix ] ++ shared.topLevelModules;

View file

@ -1,4 +1,3 @@
modules:
{
pkgs,
config,
@ -10,12 +9,15 @@ let
inherit (lib) types;
fileModuleType = types.submoduleWith {
shorthandOnlyDefinesConfig = true;
specialArgs.helpers = helpers;
specialArgs = {
inherit helpers;
defaultPkgs = pkgs;
};
modules = [
(
{ name, config, ... }:
{
imports = modules;
imports = [ ../../modules ];
options.plugin = lib.mkOption {
type = types.package;
description = "A derivation with the content of the file in it";

View file

@ -1,8 +1,4 @@
{
modules,
self,
getHelpers,
}:
{ self, getHelpers }:
{
pkgs,
config,
@ -20,7 +16,7 @@ let
types
;
helpers = getHelpers pkgs false;
shared = import ./_shared.nix { inherit modules helpers; } args;
shared = import ./_shared.nix helpers args;
cfg = config.programs.nixvim;
files = shared.configFiles // {
"nvim/sysinit.lua".text = cfg.initContent;
@ -34,6 +30,7 @@ in
shorthandOnlyDefinesConfig = true;
specialArgs = {
nixosConfig = config;
defaultPkgs = pkgs;
inherit helpers;
};
modules = [ ./modules/nixos.nix ] ++ shared.topLevelModules;

View file

@ -1,9 +1,5 @@
default_pkgs:
{
modules,
self,
getHelpers,
}:
{ self, getHelpers }:
{
pkgs ? default_pkgs,
extraSpecialArgs ? { },
@ -14,23 +10,11 @@ let
inherit (pkgs) lib;
helpers = getHelpers pkgs _nixvimTests;
shared = import ./_shared.nix { inherit modules helpers; } {
shared = import ./_shared.nix helpers {
inherit pkgs lib;
config = { };
};
mkEval =
mod:
lib.evalModules {
modules = [
mod
{ wrapRc = true; }
] ++ shared.topLevelModules;
specialArgs = {
inherit helpers;
} // extraSpecialArgs;
};
handleAssertions =
config:
let
@ -44,7 +28,16 @@ let
mkNvim =
mod:
let
evaledModule = mkEval mod;
evaledModule = lib.evalModules {
modules = [
mod
{ wrapRc = true; }
] ++ shared.topLevelModules;
specialArgs = {
inherit helpers;
defaultPkgs = pkgs;
} // extraSpecialArgs;
};
config = handleAssertions evaledModule.config;
in
(pkgs.symlinkJoin {