From fd7ff2d4c52f5cf315e8f0ba3c0c96ef37dba134 Mon Sep 17 00:00:00 2001 From: mochou Date: Thu, 18 Dec 2025 01:23:35 +0800 Subject: [PATCH 1/3] feat(templates): add users-templates config --- nix/home-module.nix | 104 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 18 deletions(-) diff --git a/nix/home-module.nix b/nix/home-module.nix index 785317cda..34e3535a8 100644 --- a/nix/home-module.nix +++ b/nix/home-module.nix @@ -3,17 +3,22 @@ lib, pkgs, ... -}: let +}: +let cfg = config.programs.noctalia-shell; - jsonFormat = pkgs.formats.json {}; - generateJson = name: value: + jsonFormat = pkgs.formats.json { }; + tomlFormat = pkgs.formats.toml { }; + + generateJson = + name: value: if lib.isString value then pkgs.writeText "noctalia-${name}.json" value else if builtins.isPath value || lib.isStorePath value then value else jsonFormat.generate "noctalia-${name}.json" value; -in { +in +{ options.programs.noctalia-shell = { enable = lib.mkEnableOption "Noctalia shell configuration"; @@ -25,13 +30,14 @@ in { }; settings = lib.mkOption { - type = with lib.types; + type = + with lib.types; oneOf [ jsonFormat.type str path ]; - default = {}; + default = { }; example = lib.literalExpression '' { bar = { @@ -56,13 +62,14 @@ in { }; colors = lib.mkOption { - type = with lib.types; + type = + with lib.types; oneOf [ jsonFormat.type str path ]; - default = {}; + default = { }; example = lib.literalExpression '' { mError = "#dddddd"; @@ -87,6 +94,55 @@ in { ''; }; + user-templates = lib.mkOption { + type = lib.types.submodule { + options = { + config = lib.mkOption { + type = lib.types.attrsOf lib.types.anything; + default = { }; + description = "General [config] section for Matugen’s TOML config file."; + }; + templates = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule { + options = { + inputPath = lib.mkOption { + type = lib.types.path; + description = "Template input path for Matugen"; + }; + outputPath = lib.mkOption { + type = lib.types.path; + description = "Output path where the generated file will be written"; + }; + postHook = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = ""; + description = "Command to run after file is generated"; + }; + }; + } + ); + }; + }; + }; + default = { }; + example = lib.literalExpression '' + programs.noctalia-shell.user-templates = { + templates = { + neovim = { + inputPath = "~/.config/matugen/templates/template.lua"; + outputPath = "~/.config/nvim/generated.lua"; + postHook = "pkill -SIGUSR1 nvim"; + }; + }; + }; + ''; + description = '' + Template definitions for Matugen. Each attribute corresponds to a template + such as "neovim", "kitty", "btop". + ''; + }; + app2unit.package = lib.mkOption { type = lib.types.package; default = pkgs.app2unit; @@ -96,10 +152,11 @@ in { }; }; - config = let - restart = "${pkgs.systemd}/bin/systemctl --user try-restart noctalia-shell.service 2>/dev/null || true"; - useApp2Unit = cfg.settings.appLauncher.useApp2Unit or false; - in + config = + let + restart = "${pkgs.systemd}/bin/systemctl --user try-restart noctalia-shell.service 2>/dev/null || true"; + useApp2Unit = cfg.settings.appLauncher.useApp2Unit or false; + in lib.mkIf cfg.enable { systemd.user.services.noctalia-shell = lib.mkIf cfg.systemd.enable { Unit = { @@ -108,8 +165,8 @@ in { PartOf = [ config.wayland.systemd.target ]; After = [ config.wayland.systemd.target ]; X-Restart-Triggers = - lib.optional (cfg.settings != {}) config.xdg.configFile."noctalia/settings.json".source - ++ lib.optional (cfg.colors != {}) config.xdg.configFile."noctalia/colors.json".source; + lib.optional (cfg.settings != { }) config.xdg.configFile."noctalia/settings.json".source + ++ lib.optional (cfg.colors != { }) config.xdg.configFile."noctalia/colors.json".source; }; Service = { @@ -124,18 +181,29 @@ in { }; home.packages = - lib.optional useApp2Unit cfg.app2unit.package - ++ lib.optional (cfg.package != null) cfg.package; + lib.optional useApp2Unit cfg.app2unit.package ++ lib.optional (cfg.package != null) cfg.package; xdg.configFile = { - "noctalia/settings.json" = lib.mkIf (cfg.settings != {}) { + "noctalia/settings.json" = lib.mkIf (cfg.settings != { }) { onChange = lib.mkIf (!cfg.systemd.enable) restart; source = generateJson "settings" cfg.settings; }; - "noctalia/colors.json" = lib.mkIf (cfg.colors != {}) { + "noctalia/colors.json" = lib.mkIf (cfg.colors != { }) { onChange = lib.mkIf (!cfg.systemd.enable) restart; source = generateJson "colors" cfg.colors; }; + "noctalia/user-templates.toml" = { + onChange = lib.mkIf (!cfg.systemd.enable) restart; + source = tomlFormat.generate "user-templates.toml" { + config = cfg.user-templates.config; + templates = lib.mapAttrs (_: t: { + input_path = t.inputPath; + output_path = t.outputPath; + post_hook = t.postHook; + }) cfg.user-templates.templates; + }; + }; + }; assertions = [ From 94da19ba2368990526b9ab87ab48c331e83f2c74 Mon Sep 17 00:00:00 2001 From: mochou Date: Thu, 18 Dec 2025 04:32:21 +0800 Subject: [PATCH 2/3] fix(nix): adding default values to attrs --- nix/home-module.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nix/home-module.nix b/nix/home-module.nix index 34e3535a8..eef6331dd 100644 --- a/nix/home-module.nix +++ b/nix/home-module.nix @@ -103,6 +103,7 @@ in description = "General [config] section for Matugen’s TOML config file."; }; templates = lib.mkOption { + default = { }; type = lib.types.attrsOf ( lib.types.submodule { options = { @@ -116,7 +117,7 @@ in }; postHook = lib.mkOption { type = lib.types.nullOr lib.types.str; - default = ""; + default = null; description = "Command to run after file is generated"; }; }; From 1658f00582922e89141a3322dd629ddd782ce8f4 Mon Sep 17 00:00:00 2001 From: mochou Date: Fri, 19 Dec 2025 19:05:29 +0800 Subject: [PATCH 3/3] refactor(nix): add multi-type support --- nix/home-module.nix | 74 +++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/nix/home-module.nix b/nix/home-module.nix index eef6331dd..fb150777a 100644 --- a/nix/home-module.nix +++ b/nix/home-module.nix @@ -95,40 +95,16 @@ in }; user-templates = lib.mkOption { - type = lib.types.submodule { - options = { - config = lib.mkOption { - type = lib.types.attrsOf lib.types.anything; - default = { }; - description = "General [config] section for Matugen’s TOML config file."; - }; - templates = lib.mkOption { - default = { }; - type = lib.types.attrsOf ( - lib.types.submodule { - options = { - inputPath = lib.mkOption { - type = lib.types.path; - description = "Template input path for Matugen"; - }; - outputPath = lib.mkOption { - type = lib.types.path; - description = "Output path where the generated file will be written"; - }; - postHook = lib.mkOption { - type = lib.types.nullOr lib.types.str; - default = null; - description = "Command to run after file is generated"; - }; - }; - } - ); - }; - }; - }; default = { }; + type = + with lib.types; + oneOf [ + tomlFormat.type + str + path + ]; example = lib.literalExpression '' - programs.noctalia-shell.user-templates = { + { templates = { neovim = { inputPath = "~/.config/matugen/templates/template.lua"; @@ -136,11 +112,15 @@ in postHook = "pkill -SIGUSR1 nvim"; }; }; - }; + } ''; description = '' - Template definitions for Matugen. Each attribute corresponds to a template - such as "neovim", "kitty", "btop". + Template definitions for Matugen. + + This option accepts: + - a Nix attrset (converted to TOML automatically) + - a string containing raw TOML + - a path to an existing TOML file ''; }; @@ -167,7 +147,10 @@ in After = [ config.wayland.systemd.target ]; X-Restart-Triggers = lib.optional (cfg.settings != { }) config.xdg.configFile."noctalia/settings.json".source - ++ lib.optional (cfg.colors != { }) config.xdg.configFile."noctalia/colors.json".source; + ++ lib.optional (cfg.colors != { }) config.xdg.configFile."noctalia/colors.json".source + ++ lib.optional ( + cfg.user-templates != { } + ) config.xdg.configFile."noctalia/user-templates.toml".source; }; Service = { @@ -193,18 +176,15 @@ in onChange = lib.mkIf (!cfg.systemd.enable) restart; source = generateJson "colors" cfg.colors; }; - "noctalia/user-templates.toml" = { - onChange = lib.mkIf (!cfg.systemd.enable) restart; - source = tomlFormat.generate "user-templates.toml" { - config = cfg.user-templates.config; - templates = lib.mapAttrs (_: t: { - input_path = t.inputPath; - output_path = t.outputPath; - post_hook = t.postHook; - }) cfg.user-templates.templates; - }; + "noctalia/user-templates.toml" = lib.mkIf (cfg.user-templates != { }) { + source = + if lib.isString cfg.user-templates then + pkgs.writeText "noctalia-user-templates.toml" cfg.user-templates + else if builtins.isPath cfg.user-templates || lib.isStorePath cfg.user-templates then + cfg.user-templates + else + tomlFormat.generate "noctalia-user-templates.toml" cfg.user-templates; }; - }; assertions = [