mirror of
https://github.com/nix-community/nixvim.git
synced 2025-12-14 04:51:11 +01:00
modules/extraFiles: refactor to use symlinks and support directories
Instead of copying source files to the target, use a symlink. This reduces nix store redundancy and enables using entire directories as sources. To support this, additional validation is done on file targets to prevent unexpected conflicts.
This commit is contained in:
parent
ba8f6d40b1
commit
c50d50b168
2 changed files with 293 additions and 31 deletions
|
|
@ -56,6 +56,19 @@ in
|
|||
config =
|
||||
let
|
||||
extraFiles = lib.filter (file: file.enable) (lib.attrValues config.extraFiles);
|
||||
targets = lib.pipe extraFiles [
|
||||
(builtins.groupBy (entry: entry.target))
|
||||
# Explicitly copy to store using "${}"
|
||||
(lib.mapAttrs (_: map (entry: "${entry.finalSource}")))
|
||||
];
|
||||
prefixConflicts = lib.optionals (targets != { }) (
|
||||
let
|
||||
names = lib.attrNames targets;
|
||||
pairs = lib.zipLists names (lib.tail names);
|
||||
in
|
||||
lib.filter ({ fst, snd }: lib.hasPrefix "${fst}/" snd) pairs
|
||||
);
|
||||
|
||||
concatFilesOption = attr: lib.flatten (lib.mapAttrsToList (_: builtins.getAttr attr) config.files);
|
||||
in
|
||||
{
|
||||
|
|
@ -63,7 +76,15 @@ in
|
|||
extraPlugins = concatFilesOption "extraPlugins";
|
||||
extraPackages = concatFilesOption "extraPackages";
|
||||
warnings = concatFilesOption "warnings";
|
||||
assertions = concatFilesOption "assertions";
|
||||
assertions =
|
||||
concatFilesOption "assertions"
|
||||
++ lib.nixvim.mkAssertions "extraFiles" {
|
||||
assertion = prefixConflicts == [ ];
|
||||
message = ''
|
||||
Conflicting target prefixes:
|
||||
${lib.concatMapStringsSep "\n" ({ fst, snd }: " - ${fst} ↔ ${snd}") prefixConflicts}
|
||||
'';
|
||||
};
|
||||
|
||||
# Add files to extraFiles
|
||||
extraFiles = lib.mkDerivedConfig options.files (
|
||||
|
|
@ -76,28 +97,53 @@ in
|
|||
);
|
||||
|
||||
# A directory with all the files in it
|
||||
# Implementation based on NixOS's /etc module
|
||||
build.extraFiles = pkgs.runCommandLocal "nvim-config" { passthru.vimPlugin = true; } ''
|
||||
set -euo pipefail
|
||||
# Implementation inspired by NixOS's /etc module
|
||||
build.extraFiles =
|
||||
pkgs.runCommandLocal "nvim-config"
|
||||
{
|
||||
__structuredAttrs = true;
|
||||
nativeBuildInputs = [ pkgs.jq ];
|
||||
inherit targets;
|
||||
passthru.vimPlugin = true;
|
||||
}
|
||||
''
|
||||
set -euo pipefail
|
||||
|
||||
makeEntry() {
|
||||
src="$1"
|
||||
target="$2"
|
||||
mkdir -p "$out/$(dirname "$target")"
|
||||
cp "$src" "$out/$target"
|
||||
}
|
||||
# Ensure $out is created, in case there are no targets defined
|
||||
mkdir -p "$out"
|
||||
|
||||
mkdir -p "$out"
|
||||
${lib.concatMapStringsSep "\n" (
|
||||
{ target, finalSource, ... }:
|
||||
lib.escapeShellArgs [
|
||||
"makeEntry"
|
||||
# Force local source paths to be added to the store
|
||||
"${finalSource}"
|
||||
target
|
||||
]
|
||||
) extraFiles}
|
||||
'';
|
||||
# Extract targets into a dedicated file,
|
||||
# to avoid reading all of attrs.json multiple times
|
||||
jq .targets "$NIX_ATTRS_JSON_FILE" > targets.json
|
||||
|
||||
# Iterate target keys and read sources into a bash array
|
||||
jq --raw-output 'keys[]' targets.json |
|
||||
while IFS= read -r target; do
|
||||
# read the list of source strings for this target into a Bash array
|
||||
mapfile -t sources < <(
|
||||
jq --raw-output --arg target "$target" '.[$target][]' targets.json
|
||||
)
|
||||
|
||||
# If there are multiple sources, ensure all are identical
|
||||
if (( ''${#sources[@]} > 1 )); then
|
||||
base="''${sources[0]}"
|
||||
for src in "''${sources[@]:1}"; do
|
||||
# Optimistically check store-path equality, then fallback to diff
|
||||
if [ "$src" != "$base" ] && ! diff -q "$base" "$src" >/dev/null
|
||||
then
|
||||
echo "error: target '$target' defined multiple times with different sources:" >&2
|
||||
printf ' %s\n' "''${sources[@]}" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Install target symlink to the canonical source
|
||||
dest="$out/$target"
|
||||
mkdir -p "$(dirname "$dest")"
|
||||
ln -s "''${sources[0]}" "$dest"
|
||||
done
|
||||
'';
|
||||
|
||||
# Never combine user files with the rest of the plugins
|
||||
performance.combinePlugins.standalonePlugins = [ config.build.extraFiles ];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue