From 0cdf9f51bf1709a5852f17bd9ceab38e964b9ba0 Mon Sep 17 00:00:00 2001 From: js0ny Date: Mon, 15 Dec 2025 01:19:02 +0000 Subject: [PATCH] util: mkMergedYAML --- nixcfgs/modules/home/programs/cider-2.nix | 122 ++++++++++++++++++++++ nixcfgs/users/js0ny/lib/mergetools.nix | 44 ++++++++ nixcfgs/users/js0ny/programs/cider-2.nix | 69 ++++++++++++ nixcfgs/users/js0ny/programs/cider2.nix | 45 -------- nixcfgs/users/js0ny/zephyrus.nix | 2 +- 5 files changed, 236 insertions(+), 46 deletions(-) create mode 100644 nixcfgs/modules/home/programs/cider-2.nix create mode 100644 nixcfgs/users/js0ny/lib/mergetools.nix create mode 100644 nixcfgs/users/js0ny/programs/cider-2.nix delete mode 100644 nixcfgs/users/js0ny/programs/cider2.nix diff --git a/nixcfgs/modules/home/programs/cider-2.nix b/nixcfgs/modules/home/programs/cider-2.nix new file mode 100644 index 0000000..2a9ac12 --- /dev/null +++ b/nixcfgs/modules/home/programs/cider-2.nix @@ -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/ + mkTheme = name: entry: + nameValuePair + "${ciderConfigDir}/themes/${name}" + {source = resolveEntry name entry;}; + + # 生成 Plugin 路径: .../plugins/ + mkPlugin = name: entry: + nameValuePair + "${ciderConfigDir}/plugins/${name}" + {source = resolveEntry name entry;}; + in + (mapAttrs' mkTheme cfg.themes) // (mapAttrs' mkPlugin cfg.plugins); + }; +} diff --git a/nixcfgs/users/js0ny/lib/mergetools.nix b/nixcfgs/users/js0ny/lib/mergetools.nix new file mode 100644 index 0000000..3d69bde --- /dev/null +++ b/nixcfgs/users/js0ny/lib/mergetools.nix @@ -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; +} diff --git a/nixcfgs/users/js0ny/programs/cider-2.nix b/nixcfgs/users/js0ny/programs/cider-2.nix new file mode 100644 index 0000000..7663b55 --- /dev/null +++ b/nixcfgs/users/js0ny/programs/cider-2.nix @@ -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="; + }; + }; + }; + }; +} diff --git a/nixcfgs/users/js0ny/programs/cider2.nix b/nixcfgs/users/js0ny/programs/cider2.nix deleted file mode 100644 index 9b4eebe..0000000 --- a/nixcfgs/users/js0ny/programs/cider2.nix +++ /dev/null @@ -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; -} diff --git a/nixcfgs/users/js0ny/zephyrus.nix b/nixcfgs/users/js0ny/zephyrus.nix index cccb669..6937ea3 100644 --- a/nixcfgs/users/js0ny/zephyrus.nix +++ b/nixcfgs/users/js0ny/zephyrus.nix @@ -75,7 +75,7 @@ in { ./programs/sioyek.nix ./programs/celluloid.nix ./programs/picard.nix - ./programs/cider2.nix + ./programs/cider-2.nix # Desktop Linux ./programs/desktop/gnome