dotfiles/nixcfgs/users/js0ny/lib/mergetools.nix
2026-01-26 18:23:44 +00:00

119 lines
3 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
config,
lib,
pkgs,
...
}: let
# Helper to handle the logic check string
# 如果 force 为 trueBash 变量 FORCE 为 "true",否则为 "false"
# In Nix, (toString true) yields "1"
mkForceVar = force:
if force
then "true"
else "false";
mkMergedYaml = {
name,
target,
settings,
force ? false,
}: let
yamlContent = lib.generators.toYAML {} settings;
patchFile = "${target}.nix-managed";
in {
home.file."${patchFile}".text = yamlContent;
home.activation."merge-${name}" = lib.hm.dag.entryAfter ["writeBoundary"] ''
TARGET="$HOME/${target}"
PATCH="$HOME/${patchFile}"
FORCE="${mkForceVar force}"
if [ -f "$TARGET" ] || [ "$FORCE" = "true" ]; then
if [ -f "$PATCH" ]; then
verboseEcho "Merging Nix managed YAML config into: $TARGET"
#
run mkdir -p "$(dirname "$TARGET")"
if [ ! -f "$TARGET" ]; then
echo "{}" > "$TARGET"
fi
run ${pkgs.yq-go}/bin/yq -i -oy -P ". *= load(\"$PATCH\")" "$TARGET"
fi
else
verboseEcho "Skipping merge for $TARGET: file missing and force=false"
fi
'';
};
mkMergedJson = {
name,
target,
settings,
force ? false,
}: let
jsonContent = builtins.toJSON settings;
patchFile = "${target}.nix-managed";
in {
home.file."${patchFile}".text = jsonContent;
home.activation."merge-${name}" = lib.hm.dag.entryAfter ["writeBoundary"] ''
TARGET="$HOME/${target}"
PATCH="$HOME/${patchFile}"
FORCE="${mkForceVar force}"
if [ -f "$TARGET" ] || [ "$FORCE" = "true" ]; then
if [ -f "$PATCH" ]; then
verboseEcho "Merging Nix managed JSON config into: $TARGET"
run mkdir -p "$(dirname "$TARGET")"
if [ ! -f "$TARGET" ]; then
echo "{}" > "$TARGET"
fi
run ${pkgs.yq-go}/bin/yq -i -o json -P --indent 2 ". *= load(\"$PATCH\")" "$TARGET"
fi
else
verboseEcho "Skipping merge for $TARGET: file missing and force=false"
fi
'';
};
mkMergedIni = {
name,
target,
settings,
force ? false,
}: let
iniContent = lib.generators.toINI {} settings;
patchFile = "${target}.nix-managed";
in {
home.file."${patchFile}".text = iniContent;
home.activation."merge-${name}" = lib.hm.dag.entryAfter ["writeBoundary"] ''
TARGET="$HOME/${target}"
PATCH="$HOME/${patchFile}"
FORCE="${mkForceVar force}"
if [ -f "$TARGET" ] || [ "$FORCE" = "true" ]; then
if [ -f "$PATCH" ]; then
verboseEcho "Merging Nix managed INI config into: $TARGET"
run mkdir -p "$(dirname "$TARGET")"
if [ ! -f "$TARGET" ]; then
echo "" > "$TARGET"
fi
run ${pkgs.crudini}/bin/crudini --merge "$TARGET" < "$PATCH"
fi
else
verboseEcho "Skipping merge for $TARGET: file missing and force=false"
fi
'';
};
in {
inherit mkMergedYaml mkMergedJson mkMergedIni;
}