util: mkMergedYAML

This commit is contained in:
js0ny 2025-12-15 01:19:02 +00:00
parent ba412e143b
commit 0cdf9f51bf
5 changed files with 236 additions and 46 deletions

View file

@ -0,0 +1,122 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.programs.cider-2;
ciderConfigDir = "sh.cider.genten";
# --- 1. 辅助函数Cider Marketplace Fetcher ---
fetchCiderMarketplace = {
projectId,
version,
sha256,
}:
pkgs.runCommand "cider-extension-${toString projectId}" {
nativeBuildInputs = [pkgs.unzip];
src = pkgs.fetchurl {
url = "https://api.connect.cider.sh/marketplace/projects/${toString projectId}/versions/${version}/download";
inherit sha256;
};
} ''
mkdir -p $out
unzip $src -d $out
'';
# --- 2. 类型定义Entry Submodule ---
# 这个 submodule 定义了单一 Theme 或 Plugin 的配置结构
entryType = types.submodule ({
name,
config,
...
}: {
options = {
src = mkOption {
type = types.nullOr types.path;
default = null;
description = "Direct path or derivation to the theme/plugin directory.";
};
marketplace = mkOption {
default = null;
description = "Download from Cider Marketplace.";
type = types.nullOr (types.submodule {
options = {
id = mkOption {
type = types.int;
description = "Project ID (e.g. 10)";
};
version = mkOption {
type = types.str;
description = "Version string (e.g. 1.1.0)";
};
sha256 = mkOption {
type = types.str;
description = "SRI Hash or SHA256";
};
};
});
};
};
});
# --- 3. 逻辑转换函数 ---
# 将用户的 submodule 配置转换为最终的 store path
resolveEntry = name: entryCfg:
if entryCfg.src != null
then entryCfg.src
else if entryCfg.marketplace != null
then
fetchCiderMarketplace {
projectId = entryCfg.marketplace.id;
inherit (entryCfg.marketplace) version sha256;
}
else throw "programs.cider-2: Theme/Plugin '${name}' must have either 'src' or 'marketplace' defined.";
in {
# --- Options 定义 ---
options.programs.cider-2 = {
enable = mkEnableOption "Cider 2";
package = mkOption {
type = types.package;
default = pkgs.cider-2;
defaultText = literalExpression "pkgs.cider-2";
};
themes = mkOption {
# Key 是目录名 (对于 Marketplace theme 通常是 ID如 "12")
type = types.attrsOf entryType;
default = {};
description = "Themes configuration.";
};
plugins = mkOption {
# Key 是 literalId (如 "ch.kaifa.listenbrainz")
type = types.attrsOf entryType;
default = {};
description = "Plugins configuration.";
};
};
# --- Config 实现 ---
config = mkIf cfg.enable {
home.packages = [cfg.package];
xdg.configFile = let
# 生成 Theme 路径: .../themes/<Key>
mkTheme = name: entry:
nameValuePair
"${ciderConfigDir}/themes/${name}"
{source = resolveEntry name entry;};
# 生成 Plugin 路径: .../plugins/<Key (LiteralID)>
mkPlugin = name: entry:
nameValuePair
"${ciderConfigDir}/plugins/${name}"
{source = resolveEntry name entry;};
in
(mapAttrs' mkTheme cfg.themes) // (mapAttrs' mkPlugin cfg.plugins);
};
}

View file

@ -0,0 +1,44 @@
{
config,
lib,
pkgs,
...
}: let
mkMergedYaml = {
name, # This value should be unique for each merged YAML config
target, # The target YAML file path relative to $HOME
settings, # The Nix Settings to convert to YAML and merge
}: let
yamlContent = lib.generators.toYAML {} settings;
# This file will hold the Nix-managed YAML content
# symlink to /nix/store/***.nix-managed for clarity
patchFile = "${target}.nix-managed";
in {
# Write the Nix-managed YAML content for merging and preservation
home.file."${patchFile}".text = yamlContent;
home.activation."merge-${name}" = lib.hm.dag.entryAfter ["writeBoundary"] ''
TARGET="$HOME/${target}"
PATCH="$HOME/${patchFile}"
if [ -f "$PATCH" ]; then
verboseEcho "Merging Nix managed YAML config into: $TARGET"
run mkdir -p "$(dirname "$TARGET")"
if [ ! -f "$TARGET" ]; then
echo "{}" > "$TARGET"
fi
# Arguments:
# * -i: In-place edit
# * -oy: Output YAML
# * -P: Pretty Print (indentation, etc.)
run ${pkgs.yq-go}/bin/yq -i -oy -P ". *= load(\"$PATCH\")" "$TARGET"
fi
'';
};
in {
inherit mkMergedYaml;
}

View file

@ -0,0 +1,69 @@
{
pkgs,
lib,
config,
...
}: let
catppuccinCider = pkgs.fetchFromGitHub {
owner = "catppuccin";
repo = "cider";
rev = "d336144a63f7dc1510b072cfb94eda1730db45cb";
sha256 = "sha256-wzlRiGDDmVAqAazhhXAl4LepNY/UyyWdLLQVvkHTTSE=";
};
ctp-mocha = pkgs.runCommand "cider-theme-ctp-mocha" {} ''
cp -r ${catppuccinCider}/themes/ctp-mocha $out
chmod -R u+w $out
echo "marketplaceID: 12" >> "$out/theme.yml"
echo 'version: "25.02"' >> "$out/theme.yml"
'';
mergetools = import ../lib/mergetools.nix {inherit pkgs lib config;};
mkMergedYaml = mergetools.mkMergedYaml;
ciderSpaConfig = mkMergedYaml {
name = "cider-spa-config";
target = ".config/sh.cider.genten/spa-config.yml";
settings = {
general = {
language = "zh-CN";
keybindings = {
commandCenter = ["ctrlKey" "KeyO"];
};
closeToTray = true;
checkForUpdates = false;
};
updates = {
# Managed by Nix, disable built-in update checks
checkForUpdates = false;
};
};
};
in {
imports = [
../../../modules/home/programs/cider-2.nix
ciderSpaConfig
];
programs.cider-2 = {
enable = true;
# --- Themes ---
themes = {
# This maps to ~/.config/sh.cider.genten/themes/12
"12" = {
src = ctp-mocha;
};
};
# --- Plugins ---
plugins = {
# This maps to ~/.config/sh.cider.genten/plugins/ch.kaifa.listenbrainz
"ch.kaifa.listenbrainz" = {
marketplace = {
id = 10;
version = "1.1.0";
sha256 = "sha256-YelqonGEQVZk4+IQ8YwgfqP93a+enN6XxVktlyBCEZI=";
};
};
};
};
}

View file

@ -1,45 +0,0 @@
{pkgs, ...}: let
catppuccinCider = pkgs.fetchFromGitHub {
owner = "catppuccin";
repo = "cider";
rev = "d336144a63f7dc1510b072cfb94eda1730db45cb";
sha256 = "sha256-wzlRiGDDmVAqAazhhXAl4LepNY/UyyWdLLQVvkHTTSE=";
};
ctp-mocha = pkgs.runCommand "cider-theme-ctp-mocha" {} ''
cp -r ${catppuccinCider}/themes/ctp-mocha $out
chmod -R u+w $out
echo "marketplaceID: 12" >> "$out/theme.yml"
echo 'version: "25.02"' >> "$out/theme.yml"
'';
ciderConfigDir = "sh.cider.genten";
themeDir = "${ciderConfigDir}/themes";
pluginDir = "${ciderConfigDir}/plugins";
ciderThemes = [
{
package = ctp-mocha;
id = 12;
version = "25.02";
}
];
plugin-listenBrainz =
pkgs.runCommand "cider-plugin-listenbrainz" {
# name = "ListenBrainz-1.1.0.zip";
nativeBuildInputs = [pkgs.unzip];
src = pkgs.fetchurl {
url = "https://api.connect.cider.sh/marketplace/projects/10/versions/1.1.0/download";
sha256 = "sha256-YelqonGEQVZk4+IQ8YwgfqP93a+enN6XxVktlyBCEZI=";
};
recursive = true;
}
''
mkdir -p $out
unzip $src -d $out
chmod -R u+w $out
'';
in {
home.packages = [pkgs.cider-2];
xdg.configFile."${themeDir}/12".source = ctp-mocha;
xdg.configFile."${pluginDir}/ch.kaifa.listenbrainz".source = plugin-listenBrainz;
}

View file

@ -75,7 +75,7 @@ in {
./programs/sioyek.nix ./programs/sioyek.nix
./programs/celluloid.nix ./programs/celluloid.nix
./programs/picard.nix ./programs/picard.nix
./programs/cider2.nix ./programs/cider-2.nix
# Desktop Linux # Desktop Linux
./programs/desktop/gnome ./programs/desktop/gnome