modules: refactor extraFiles

Moved `extraFiles` from `modules/output.nix` into its own file `modules/files.nix`.

Users should now assign text to a `text` attribute, however they could
also assign a file path to a `source` attribute instead.

The old method of directly assigning a string still works, and is
coerced to the new type along with a deprecation warning.
This commit is contained in:
Matt Sturgeon 2024-07-07 16:09:31 +01:00
parent c12694f4ba
commit 086873bed9
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
8 changed files with 157 additions and 28 deletions

View file

@ -12,6 +12,7 @@
./diagnostics.nix ./diagnostics.nix
./doc.nix ./doc.nix
./editorconfig.nix ./editorconfig.nix
./files.nix
./filetype.nix ./filetype.nix
./highlights.nix ./highlights.nix
./keymaps.nix ./keymaps.nix

82
modules/files.nix Normal file
View file

@ -0,0 +1,82 @@
{
lib,
helpers,
pkgs,
...
}:
let
fileType = lib.types.submodule (
{
name,
config,
options,
...
}:
{
options = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether this file should be generated.
This option allows specific files to be disabled.
'';
};
target = lib.mkOption {
type = lib.types.str;
defaultText = lib.literalMD "the attribute name";
description = ''
Name of symlink, relative to nvim config.
'';
};
text = lib.mkOption {
type = with lib.types; nullOr lines;
default = null;
description = "Text of the file.";
};
source = lib.mkOption {
type = lib.types.path;
description = "Path of the source file.";
};
};
config =
let
derivationName = "nvim-" + lib.replaceStrings [ "/" ] [ "-" ] name;
in
{
target = lib.mkDefault name;
source = lib.mkIf (config.text != null) (
# mkDerivedConfig uses the option's priority, and calls our function with the option's value.
# This means our `source` definition has the same priority as `text`.
lib.mkDerivedConfig options.text (pkgs.writeText derivationName)
);
};
}
);
# TODO: Added 2024-07-07, remove after 24.11
# Before we had a fileType, we used types.str.
coercedFileType = helpers.transitionType lib.types.str (text: { inherit text; }) fileType;
in
{
options = {
extraFiles = lib.mkOption {
type = lib.types.attrsOf coercedFileType;
description = "Extra files to add to the runtime path";
default = { };
example = lib.literalExpression ''
{
"ftplugin/nix.lua".text = '''
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
''';
}
'';
};
};
}

View file

@ -95,12 +95,6 @@ in
description = "Extra lua packages to include with neovim"; description = "Extra lua packages to include with neovim";
default = _: [ ]; default = _: [ ];
}; };
extraFiles = mkOption {
type = types.attrsOf types.str;
description = "Extra files to add to the runtime path";
default = { };
};
}; };
config = config =

View file

@ -1,6 +1,7 @@
{ {
pkgs, pkgs,
config, config,
options,
lib, lib,
helpers, helpers,
... ...
@ -49,8 +50,8 @@ in
config = config =
let let
inherit (config) files; extraFiles = lib.filter (file: file.enable) (lib.attrValues config.extraFiles);
concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) files); concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) config.files);
in in
{ {
# Each file can declare plugins/packages/warnings/assertions # Each file can declare plugins/packages/warnings/assertions
@ -59,12 +60,38 @@ in
warnings = concatFilesOption "warnings"; warnings = concatFilesOption "warnings";
assertions = concatFilesOption "assertions"; assertions = concatFilesOption "assertions";
# Add files to extraFiles
extraFiles = lib.mkDerivedConfig options.files (
lib.mapAttrs' (
_: file: {
name = file.path;
value.source = file.plugin;
}
)
);
# A directory with all the files in it # A directory with all the files in it
filesPlugin = pkgs.buildEnv { # Implementation based on NixOS's /etc module
name = "nixvim-config"; filesPlugin = pkgs.runCommandLocal "nvim-config" { } ''
paths = set -euo pipefail
(lib.mapAttrsToList (_: file: file.plugin) files)
++ (lib.mapAttrsToList pkgs.writeTextDir config.extraFiles); makeEntry() {
}; src="$1"
target="$2"
mkdir -p "$out/$(dirname "$target")"
cp "$src" "$out/$target"
}
mkdir -p "$out"
${lib.concatMapStringsSep "\n" (
{ target, source, ... }:
lib.escapeShellArgs [
"makeEntry"
# Force local source paths to be added to the store
"${source}"
target
]
) extraFiles}
'';
}; };
} }

View file

@ -14,9 +14,14 @@
internal = true; internal = true;
}; };
}; };
config = { config =
path = name; let
type = lib.mkDefault (if lib.hasSuffix ".vim" name then "vim" else "lua"); derivationName = "nvim-" + lib.replaceStrings [ "/" ] [ "-" ] name;
plugin = pkgs.writeTextDir config.path config.content; in
}; {
path = lib.mkDefault name;
type = lib.mkDefault (if lib.hasSuffix ".vim" name then "vim" else "lua");
# No need to use mkDerivedConfig; this option is readOnly.
plugin = pkgs.writeText derivationName config.content;
};
} }

View file

@ -178,7 +178,7 @@ in
''); '');
extraFiles = mkIf cfg.nixvimInjections { extraFiles = mkIf cfg.nixvimInjections {
"queries/nix/injections.scm" = '' "queries/nix/injections.scm".text = ''
;; extends ;; extends
(binding (binding

View file

@ -1,7 +1,13 @@
{ {
query = { query = {
extraFiles = { extraFiles = {
"queries/lua/injections.scm" = '' "testing/test-case.nix".source = ./extra-files.nix;
"testing/123.txt".text = ''
One
Two
Three
'';
"queries/lua/injections.scm".text = ''
;; extends ;; extends
''; '';
}; };

View file

@ -1,23 +1,37 @@
helpers: helpers:
{ lib, config, ... }: { lib, config, ... }:
let let
inherit (lib) mkOption mkOptionType; inherit (lib)
isAttrs
listToAttrs
map
mkOption
mkOptionType
;
cfg = config.programs.nixvim; cfg = config.programs.nixvim;
extraFiles = lib.filter (file: file.enable) (lib.attrValues cfg.extraFiles);
in in
{ {
helpers = mkOption { helpers = mkOption {
type = mkOptionType { type = mkOptionType {
name = "helpers"; name = "helpers";
description = "Helpers that can be used when writing nixvim configs"; description = "Helpers that can be used when writing nixvim configs";
check = builtins.isAttrs; check = isAttrs;
}; };
description = "Use this option to access the helpers"; description = "Use this option to access the helpers";
default = helpers; default = helpers;
}; };
configFiles = # extraFiles, but nested under "nvim/" for use in etc/xdg config
(lib.mapAttrs' (_: file: lib.nameValuePair "nvim/${file.path}" { text = file.content; }) cfg.files) configFiles = listToAttrs (
// (lib.mapAttrs' ( map (
path: content: lib.nameValuePair "nvim/${path}" { text = content; } { target, source, ... }:
) cfg.extraFiles); {
name = "nvim/" + target;
value = {
inherit source;
};
}
) extraFiles
);
} }