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

@ -1,6 +1,7 @@
{
pkgs,
config,
options,
lib,
helpers,
...
@ -49,8 +50,8 @@ in
config =
let
inherit (config) files;
concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) files);
extraFiles = lib.filter (file: file.enable) (lib.attrValues config.extraFiles);
concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) config.files);
in
{
# Each file can declare plugins/packages/warnings/assertions
@ -59,12 +60,38 @@ in
warnings = concatFilesOption "warnings";
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
filesPlugin = pkgs.buildEnv {
name = "nixvim-config";
paths =
(lib.mapAttrsToList (_: file: file.plugin) files)
++ (lib.mapAttrsToList pkgs.writeTextDir config.extraFiles);
};
# Implementation based on NixOS's /etc module
filesPlugin = pkgs.runCommandLocal "nvim-config" { } ''
set -euo pipefail
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;
};
};
config = {
path = name;
type = lib.mkDefault (if lib.hasSuffix ".vim" name then "vim" else "lua");
plugin = pkgs.writeTextDir config.path config.content;
};
config =
let
derivationName = "nvim-" + lib.replaceStrings [ "/" ] [ "-" ] name;
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;
};
}