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

View file

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

View file

@ -5,7 +5,6 @@
./helpers.nix ./helpers.nix
./lib.nix ./lib.nix
./legacy-packages.nix ./legacy-packages.nix
./modules.nix
./overlays.nix ./overlays.nix
./packages.nix ./packages.nix
./templates.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, pkgsUnfree,
config, config,
rawModules,
helpers, helpers,
... ...
}: }:
{ {
packages = import ../docs { packages = import ../docs {
inherit rawModules helpers; inherit helpers;
# Building the docs evaluates each plugin's default package, some of which are unfree # Building the docs evaluates each plugin's default package, some of which are unfree
pkgs = pkgsUnfree; pkgs = pkgsUnfree;
}; };

View file

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

View file

@ -1,5 +1,6 @@
{ {
imports = [ imports = [
./misc
./autocmd.nix ./autocmd.nix
./clipboard.nix ./clipboard.nix
./colorscheme.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 }: helpers:
{ { lib, config, ... }:
lib,
pkgs,
config,
...
}:
let let
inherit (lib) inherit (lib) mkOption mkOptionType;
mkEnableOption cfg = config.programs.nixvim;
mkOption
mkOptionType
mkForce
mkMerge
mkIf
types
;
in in
{ {
topLevelModules = [ topLevelModules = [
../modules
./modules/output.nix ./modules/output.nix
(import ./modules/files.nix (modules pkgs)) ./modules/files.nix
] ++ (modules pkgs); ];
helpers = mkOption { helpers = mkOption {
type = mkOptionType { type = mkOptionType {
@ -33,9 +22,6 @@ in
}; };
configFiles = configFiles =
let
cfg = config.programs.nixvim;
in
(lib.mapAttrs' (_: file: lib.nameValuePair "nvim/${file.path}" { text = file.content; }) cfg.files) (lib.mapAttrs' (_: file: lib.nameValuePair "nvim/${file.path}" { text = file.content; }) cfg.files)
// (lib.mapAttrs' ( // (lib.mapAttrs' (
path: content: lib.nameValuePair "nvim/${path}" { text = content; } path: content: lib.nameValuePair "nvim/${path}" { text = content; }

View file

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

View file

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

View file

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

View file

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

View file

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