modules/{output,files,test}: move outputs to build scope

Move the following output options into `build`:
- finalPackage -> package
- printInitPackage
- initPath -> initFile
- filesPlugin -> extraFiles
- test.derivation -> test
This commit is contained in:
Matt Sturgeon 2024-09-24 07:02:22 +01:00
parent 7bda0f1ce4
commit 692e39311e
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
14 changed files with 129 additions and 73 deletions

View file

@ -30,7 +30,7 @@ let
); );
}; };
in in
result.config.test.derivation; result.config.build.test;
# Create a nix derivation from a nixvim configuration. # Create a nix derivation from a nixvim configuration.
# The build phase simply consists in running neovim with the given configuration. # The build phase simply consists in running neovim with the given configuration.
@ -71,7 +71,7 @@ let
check = false; check = false;
}; };
in in
result.config.test.derivation; result.config.build.test;
in in
# NOTE: this is exported publicly in the flake outputs as `lib.<system>.check` # NOTE: this is exported publicly in the flake outputs as `lib.<system>.check`
{ {

View file

@ -8,6 +8,7 @@
../. ../.
./files ./files
./output.nix ./output.nix
./readonly-renames.nix
./test.nix ./test.nix
]; ];

View file

@ -36,7 +36,7 @@ in
}; };
}; };
filesPlugin = lib.mkOption { build.extraFiles = lib.mkOption {
type = types.package; type = types.package;
description = "A derivation with all the files inside."; description = "A derivation with all the files inside.";
internal = true; internal = true;
@ -68,7 +68,7 @@ in
# A directory with all the files in it # A directory with all the files in it
# Implementation based on NixOS's /etc module # Implementation based on NixOS's /etc module
filesPlugin = pkgs.runCommandLocal "nvim-config" { } '' build.extraFiles = pkgs.runCommandLocal "nvim-config" { } ''
set -euo pipefail set -euo pipefail
makeEntry() { makeEntry() {
@ -91,6 +91,6 @@ in
''; '';
# Never combine user files with the rest of the plugins # Never combine user files with the rest of the plugins
performance.combinePlugins.standalonePlugins = [ config.filesPlugin ]; performance.combinePlugins.standalonePlugins = [ config.build.extraFiles ];
}; };
} }

View file

@ -68,24 +68,31 @@ in
''; '';
}; };
finalPackage = mkOption { build = {
type = types.package; # TODO: `standalonePackage`; i.e. package + printInitPackage + man-docs bundled together
description = "Wrapped Neovim.";
readOnly = true;
};
initPath = mkOption { package = mkOption {
type = types.str; type = types.package;
description = "The path to the `init.lua` file."; description = "Wrapped Neovim.";
readOnly = true; readOnly = true;
visible = false; };
};
printInitPackage = mkOption { initFile = mkOption {
type = types.package; type = types.path;
description = "A tool to show the content of the generated `init.lua` file."; description = "The generated `init.lua` file.";
readOnly = true; readOnly = true;
visible = false; visible = false;
};
printInitPackage = mkOption {
type = types.package;
description = ''
A tool to show the content of the generated `init.lua` file.
Run using `${config.build.printInitPackage.meta.mainProgram}`.
'';
readOnly = true;
visible = false;
};
}; };
}; };
@ -208,7 +215,7 @@ in
# Combined plugins # Combined plugins
combinedPlugins = [ pluginPack ] ++ standaloneStartPlugins ++ optPlugins; combinedPlugins = [ pluginPack ] ++ standaloneStartPlugins ++ optPlugins;
# Plugins to use in finalPackage # Plugins to use in build.package
plugins = if config.performance.combinePlugins.enable then combinedPlugins else normalizedPlugins; plugins = if config.performance.combinePlugins.enable then combinedPlugins else normalizedPlugins;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig ( neovimConfig = pkgs.neovimUtils.makeNeovimConfig (
@ -244,7 +251,7 @@ in
textInit = builders.writeLua "init.lua" customRC; textInit = builders.writeLua "init.lua" customRC;
byteCompiledInit = builders.writeByteCompiledLua "init.lua" customRC; byteCompiledInit = builders.writeByteCompiledLua "init.lua" customRC;
init = initFile =
if if
config.type == "lua" config.type == "lua"
&& config.performance.byteCompileLua.enable && config.performance.byteCompileLua.enable
@ -258,7 +265,7 @@ in
(optional ( (optional (
config.extraPackages != [ ] config.extraPackages != [ ]
) ''--prefix PATH : "${lib.makeBinPath config.extraPackages}"'') ) ''--prefix PATH : "${lib.makeBinPath config.extraPackages}"'')
++ (optional config.wrapRc ''--add-flags -u --add-flags "${init}"'') ++ (optional config.wrapRc ''--add-flags -u --add-flags "${initFile}"'')
); );
package = package =
@ -291,15 +298,17 @@ in
); );
in in
{ {
finalPackage = wrappedNeovim; build = {
initPath = "${init}"; package = wrappedNeovim;
inherit initFile;
printInitPackage = pkgs.writeShellApplication { printInitPackage = pkgs.writeShellApplication {
name = "nixvim-print-init"; name = "nixvim-print-init";
runtimeInputs = [ pkgs.bat ]; runtimeInputs = [ pkgs.bat ];
text = '' text = ''
bat --language=lua "${textInit}" bat --language=lua "${textInit}"
''; '';
};
}; };
# Set `wrapRc` and `impureRtp`s option defaults with even lower priority than `mkOptionDefault` # Set `wrapRc` and `impureRtp`s option defaults with even lower priority than `mkOptionDefault`
@ -322,6 +331,6 @@ in
) )
); );
extraPlugins = lib.mkIf config.wrapRc [ config.filesPlugin ]; extraPlugins = lib.mkIf config.wrapRc [ config.build.extraFiles ];
}; };
} }

View file

@ -0,0 +1,41 @@
{ lib, options, ... }:
let
# Recursively maps an attrset of option-refs into an attrset of option alias declarations.
#
# NOTE: `mkRenameOptionModule` is not appropriate for readOnly options, and we don't need a full two-way alias anyway (since it is read only).
toReadOnlyRenameOptions =
let
mkAlias =
from: toOpt:
assert lib.assertMsg toOpt.readOnly
"toReadOnlyRenameOptions used on non-readOnly option `${lib.showOption toOpt.loc}'.";
assert lib.assertMsg toOpt.isDefined
"toReadOnlyRenameOptions used on undefined option `${lib.showOption toOpt.loc}'.";
lib.mkOption {
inherit (toOpt) type;
default = toOpt.value;
apply = lib.warn "Obsolete option `${lib.showOption from}' is used. It was renamed to `${lib.showOption toOpt.loc}'.";
description = "Alias of {option}`${lib.showOption toOpt.loc}`.";
readOnly = true;
visible = false;
};
go =
path: name: opt:
let
loc = path ++ lib.singleton name;
in
if lib.isOption opt then mkAlias loc opt else lib.mapAttrs (go loc) opt;
in
lib.mapAttrs (go [ ]);
in
{
# TODO: Added 2024-09-24; remove after 24.11
options = toReadOnlyRenameOptions {
inherit (options.build) printInitPackage;
finalPackage = options.build.package;
initPath = options.build.initFile;
filesPlugin = options.build.extraFiles;
test.derivation = options.build.test;
};
}

View file

@ -36,9 +36,10 @@ in
description = "Whether to check `config.assertions` in the test."; description = "Whether to check `config.assertions` in the test.";
default = true; default = true;
}; };
};
# Output options.build = {
derivation = lib.mkOption { test = lib.mkOption {
type = lib.types.package; type = lib.types.package;
description = '' description = ''
A derivation that tests the config by running neovim. A derivation that tests the config by running neovim.
@ -66,10 +67,10 @@ in
) "" toCheck; ) "" toCheck;
in in
{ {
test.derivation = build.test =
pkgs.runCommandNoCCLocal cfg.name pkgs.runCommandNoCCLocal cfg.name
{ {
nativeBuildInputs = [ config.finalPackage ]; nativeBuildInputs = [ config.build.package ];
# Allow inspecting the test's module a little from the repl # Allow inspecting the test's module a little from the repl
# e.g. # e.g.

View file

@ -14,7 +14,7 @@ let
in in
pkgs.runCommand "extra-files-test" pkgs.runCommand "extra-files-test"
{ {
root = build.config.filesPlugin; root = build.config.build.extraFiles;
files = builtins.attrNames extraFiles; files = builtins.attrNames extraFiles;
} }
'' ''

View file

@ -90,7 +90,7 @@ in
assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not") assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not")
-- nixvim-print-init prints text -- nixvim-print-init prints text
local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init") local init_content = vim.fn.system("${lib.getExe config.build.printInitPackage}")
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled") assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
-- lua extraFiles are byte compiled -- lua extraFiles are byte compiled
@ -116,7 +116,11 @@ in
}; };
disabled = disabled =
{ config, ... }: {
config,
lib,
...
}:
{ {
performance.byteCompileLua.enable = false; performance.byteCompileLua.enable = false;
@ -140,7 +144,7 @@ in
assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is") assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is")
-- nixvim-print-init prints text -- nixvim-print-init prints text
local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init") local init_content = vim.fn.system("${lib.getExe config.build.printInitPackage}")
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled") assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
-- Nothing is byte compiled -- Nothing is byte compiled

View file

@ -2,10 +2,10 @@
let let
inherit (pkgs) lib; inherit (pkgs) lib;
# Count plugins of given type excluding 'filesPlugin' # Count plugins of given type excluding 'build.extraFiles'
pluginCount = pluginCount =
pkg: filesPlugin: type: pkg: files: type:
builtins.length (builtins.filter (p: p != filesPlugin) pkg.packpathDirs.myNeovimPackages.${type}); builtins.length (builtins.filter (p: p != files) pkg.packpathDirs.myNeovimPackages.${type});
in in
{ {
# Test basic functionality # Test basic functionality
@ -33,7 +33,7 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one plugin is defined in packpathDirs, expected one plugin pack."; message = "More than one plugin is defined in packpathDirs, expected one plugin pack.";
} }
]; ];
@ -50,7 +50,7 @@ in
]; ];
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" >= 2; assertion = pluginCount config.build.package config.build.extraFiles "start" >= 2;
message = "Only one plugin is defined in packpathDirs, expected at least two."; message = "Only one plugin is defined in packpathDirs, expected at least two.";
} }
]; ];
@ -77,7 +77,7 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one plugin is defined in packpathDirs."; message = "More than one plugin is defined in packpathDirs.";
} }
]; ];
@ -105,7 +105,7 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one plugin is defined in packpathDirs."; message = "More than one plugin is defined in packpathDirs.";
} }
]; ];
@ -132,7 +132,7 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one plugin is defined in packpathDirs."; message = "More than one plugin is defined in packpathDirs.";
} }
]; ];
@ -186,11 +186,11 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one start plugin is defined in packpathDirs"; message = "More than one start plugin is defined in packpathDirs";
} }
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "opt" == 2; assertion = pluginCount config.build.package config.build.extraFiles "opt" == 2;
message = "Less than two opt plugins are defined in packpathDirs"; message = "Less than two opt plugins are defined in packpathDirs";
} }
]; ];
@ -233,13 +233,13 @@ in
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one start plugin is defined in packpathDirs"; message = "More than one start plugin is defined in packpathDirs";
} }
]; ];
}; };
# Test that config.filesPlugin is not combined # Test that config.build.extraFiles is not combined
files-plugin = files-plugin =
{ config, ... }: { config, ... }:
{ {
@ -248,7 +248,7 @@ in
nvim-treesitter nvim-treesitter
vim-nix vim-nix
]; ];
# Ensure that filesPlugin is added extraPlugins # Ensure that build.extraFiles is added extraPlugins
wrapRc = true; wrapRc = true;
# Extra user files colliding with plugins # Extra user files colliding with plugins
extraFiles = { extraFiles = {
@ -287,23 +287,23 @@ in
assert(#get_paths("ftdetect/nix.vim") == 2, "only one version of ftdetect/nix.vim") assert(#get_paths("ftdetect/nix.vim") == 2, "only one version of ftdetect/nix.vim")
assert(#get_paths("queries/nix/highlights.scm") == 2, "only one version of queries/nix/highlights.scm") assert(#get_paths("queries/nix/highlights.scm") == 2, "only one version of queries/nix/highlights.scm")
-- First found file is from filesPlugin -- First found file is from build.extraFiles
assert( assert(
get_paths("ftplugin/nix.vim")[1]:find("${lib.getName config.filesPlugin}", 1, true), get_paths("ftplugin/nix.vim")[1]:find("${lib.getName config.build.extraFiles}", 1, true),
"first found ftplugin/nix.vim isn't in filesPlugin runtime path" "first found ftplugin/nix.vim isn't in build.extraFiles runtime path"
) )
assert( assert(
get_paths("queries/nix/highlights.scm")[1]:find("${lib.getName config.filesPlugin}", 1, true), get_paths("queries/nix/highlights.scm")[1]:find("${lib.getName config.build.extraFiles}", 1, true),
"first found queries/nix/highlights.scm isn't in filesPlugin runtime path" "first found queries/nix/highlights.scm isn't in build.extraFiles runtime path"
) )
assert( assert(
get_paths("queries/nix/highlights.scm")[1]:find("${lib.getName config.filesPlugin}", 1, true), get_paths("queries/nix/highlights.scm")[1]:find("${lib.getName config.build.extraFiles}", 1, true),
"first found queries/nix/highlights.scm isn't in filesPlugin runtime path" "first found queries/nix/highlights.scm isn't in build.extraFiles runtime path"
) )
''; '';
assertions = [ assertions = [
{ {
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 1; assertion = pluginCount config.build.package config.build.extraFiles "start" == 1;
message = "More than one start plugin is defined in packpathDirs"; message = "More than one start plugin is defined in packpathDirs";
} }
]; ];
@ -369,7 +369,7 @@ in
assertions = [ assertions = [
{ {
# plugin-pack, nvim-treesitter, nvim-lspconfig, telescope-nvim, nvim-cmp # plugin-pack, nvim-treesitter, nvim-lspconfig, telescope-nvim, nvim-cmp
assertion = pluginCount config.finalPackage config.filesPlugin "start" == 5; assertion = pluginCount config.build.package config.build.extraFiles "start" == 5;
message = "Wrong number of plugins in packpathDirs"; message = "Wrong number of plugins in packpathDirs";
} }
]; ];

View file

@ -74,7 +74,7 @@ in
) extraFiles ) extraFiles
) )
// { // {
${filesPrefix + initName}.source = cfg.initPath; ${filesPrefix + initName}.source = cfg.build.initFile;
} }
) )
) )

View file

@ -41,8 +41,8 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.systemPackages = [ environment.systemPackages = [
cfg.finalPackage cfg.build.package
cfg.printInitPackage cfg.build.printInitPackage
] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs; ] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
}; };
} }

View file

@ -55,8 +55,8 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
home.packages = [ home.packages = [
cfg.finalPackage cfg.build.package
cfg.printInitPackage cfg.build.printInitPackage
] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs; ] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = "nvim"; }; home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = "nvim"; };

View file

@ -49,8 +49,8 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.systemPackages = [ environment.systemPackages = [
cfg.finalPackage cfg.build.package
cfg.printInitPackage cfg.build.printInitPackage
] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs; ] ++ lib.optional cfg.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
environment.variables = { environment.variables = {

View file

@ -23,13 +23,13 @@ let
defaultPkgs = pkgs; defaultPkgs = pkgs;
} // extraSpecialArgs; } // extraSpecialArgs;
}; };
inherit (nixvimConfig.config) enableMan finalPackage printInitPackage; inherit (nixvimConfig.config) enableMan build;
in in
(pkgs.symlinkJoin { (pkgs.symlinkJoin {
name = "nixvim"; name = "nixvim";
paths = [ paths = [
finalPackage build.package
printInitPackage build.printInitPackage
] ++ pkgs.lib.optional enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs; ] ++ pkgs.lib.optional enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
meta.mainProgram = "nvim"; meta.mainProgram = "nvim";
}) })