From 373f10e49243b66c36c818beed911d4515099683 Mon Sep 17 00:00:00 2001 From: Matthew_Cash Date: Sat, 9 Mar 2024 17:46:18 -0800 Subject: [PATCH 1/2] Use nested attrsets for nested sections --- example/home.nix | 3 +-- modules/files.nix | 46 +++++++++++++++++++++--------------------- modules/kwin.nix | 2 +- modules/shortcuts.nix | 7 +------ script/rc2nix.rb | 4 ++-- script/write_config.py | 7 +------ 6 files changed, 29 insertions(+), 40 deletions(-) diff --git a/example/home.nix b/example/home.nix index 2f28e07a..9f8768d8 100644 --- a/example/home.nix +++ b/example/home.nix @@ -65,8 +65,7 @@ # configFile = { "baloofilerc"."Basic Settings"."Indexing-Enabled" = false; - # If a group name has dots you need to escape them - "kwinrc"."org\\.kde\\.kdecoration2"."ButtonsOnLeft" = "SF"; + "kwinrc"."org.kde.kdecoration2"."ButtonsOnLeft" = "SF"; }; }; } diff --git a/modules/files.nix b/modules/files.nix index 47d17e3c..6ad0a172 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -16,31 +16,31 @@ let (prependPath config.xdg.configHome plasmaCfg.configFile) // (prependPath config.xdg.dataHome plasmaCfg.dataFile); - ############################################################################## - # A module for storing settings. - settingType = { name, ... }: { - freeformType = with lib.types; - attrsOf (nullOr (oneOf [ bool float int str ])); + # Flatten nested sections + flatten = config: builtins.listToAttrs (lib.flatten (lib.mapAttrsToList (n: v: let + keySet = lib.filterAttrs (n: v: !builtins.isAttrs v) v; + sectionSet = flatten (lib.filterAttrs (n: v: builtins.isAttrs v) v); + in lib.optionals (keySet != {}) [ { + name = n; + value = keySet; + } ] ++ lib.mapAttrsToList (group: v: { + name = "${n}][${group}"; + value = v; + }) sectionSet) config)); - options = { - configGroupNesting = lib.mkOption { - type = lib.types.nonEmptyListOf lib.types.str; - # We allow escaping periods using \\. - default = (map - (e: builtins.replaceStrings [ "\\u002E" ] [ "." ] e) - (lib.splitString "." - (builtins.replaceStrings [ "\\." ] [ "\\u002E" ] name) - ) - ); - description = "Group name, and sub-group names."; - }; - }; - }; + flatCfg = builtins.mapAttrs(file: flatten) cfg; + + ############################################################################## + # A type for storing settings. + settingsFileType = with lib.types; let + valueType = attrsOf (nullOr (oneOf [ bool float int str valueType ])) // + { description = "Plasma settings"; }; + in attrsOf (attrsOf (valueType)); ############################################################################## # Generate a script that will use write_config.py to update all # settings. - script = pkgs.writeScript "plasma-config" (writeConfig cfg); + script = pkgs.writeScript "plasma-config" (writeConfig flatCfg); ############################################################################## # Generate a script that will remove all the current config files. @@ -98,7 +98,7 @@ in { options.programs.plasma = { file = lib.mkOption { - type = with lib.types; attrsOf (attrsOf (submodule settingType)); + type = settingsFileType; default = { }; description = '' An attribute set where the keys are file names (relative to @@ -107,7 +107,7 @@ in ''; }; configFile = lib.mkOption { - type = with lib.types; attrsOf (attrsOf (submodule settingType)); + type = settingsFileType; default = { }; description = '' An attribute set where the keys are file names (relative to @@ -116,7 +116,7 @@ in ''; }; dataFile = lib.mkOption { - type = with lib.types; attrsOf (attrsOf (submodule settingType)); + type = settingsFileType; default = { }; description = '' An attribute set where the keys are file names (relative to diff --git a/modules/kwin.nix b/modules/kwin.nix index 95e03732..9ebe58d9 100644 --- a/modules/kwin.nix +++ b/modules/kwin.nix @@ -74,7 +74,7 @@ in config = mkIf cfg.enable { # Titlebar buttons - programs.plasma.configFile."kwinrc"."org\\.kde\\.kdecoration2" = mkMerge [ + programs.plasma.configFile."kwinrc"."org.kde.kdecoration2" = mkMerge [ ( mkIf (cfg.kwin.titlebarButtons.left != null) { "ButtonsOnLeft" = strings.concatStrings (getShortNames cfg.kwin.titlebarButtons.left); diff --git a/modules/shortcuts.nix b/modules/shortcuts.nix index ff599852..0ad622b1 100644 --- a/modules/shortcuts.nix +++ b/modules/shortcuts.nix @@ -25,12 +25,7 @@ let shortcutsToSettings = groups: lib.mapAttrs - (group: attrs: - (lib.mapAttrs shortcutToNameValuePair attrs) // { - # Some shortcut groups have a dot in their name so we - # explicitly set the group nesting to only one level deep: - configGroupNesting = [ group ]; - }) + (group: attrs: (lib.mapAttrs shortcutToNameValuePair attrs)) groups; in { diff --git a/script/rc2nix.rb b/script/rc2nix.rb index 22d015b4..d97de637 100755 --- a/script/rc2nix.rb +++ b/script/rc2nix.rb @@ -144,8 +144,8 @@ def parse ############################################################################ def parse_group(line) line.gsub(/\s*\[([^\]]+)\]\s*/) do |match| - $1 + "." - end.sub(/\.$/, '') + $1 + "\".\"" + end.sub(/\"."$/, '') end end diff --git a/script/write_config.py b/script/write_config.py index b4dbb225..eab6b935 100644 --- a/script/write_config.py +++ b/script/write_config.py @@ -105,13 +105,8 @@ def key_value_to_line(key: str, value: str) -> str: def write_config_single(filepath: str, items: Dict): config = KConfParser(filepath) - for entry in items.values(): - group = f"{']['.join(entry['configGroupNesting'])}" - + for group, entry in items.items(): for key, value in entry.items(): - if key == "configGroupNesting": - continue - # If the nix expression is null, resulting in the value None here, # we remove the key/option (and the group/section if it is empty # after removal). From 1e4aaceaf02c36f0b23339ce905191a6508b79e1 Mon Sep 17 00:00:00 2001 From: magnouvean Date: Sat, 23 Mar 2024 14:11:04 +0100 Subject: [PATCH 2/2] Fix formatting --- modules/files.nix | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/modules/files.nix b/modules/files.nix index 6ad0a172..335cc808 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -17,25 +17,32 @@ let (prependPath config.xdg.dataHome plasmaCfg.dataFile); # Flatten nested sections - flatten = config: builtins.listToAttrs (lib.flatten (lib.mapAttrsToList (n: v: let - keySet = lib.filterAttrs (n: v: !builtins.isAttrs v) v; - sectionSet = flatten (lib.filterAttrs (n: v: builtins.isAttrs v) v); - in lib.optionals (keySet != {}) [ { - name = n; - value = keySet; - } ] ++ lib.mapAttrsToList (group: v: { - name = "${n}][${group}"; - value = v; - }) sectionSet) config)); + flatten = config: builtins.listToAttrs (lib.flatten (lib.mapAttrsToList + (n: v: + let + keySet = lib.filterAttrs (n: v: !builtins.isAttrs v) v; + sectionSet = flatten (lib.filterAttrs (n: v: builtins.isAttrs v) v); + in + lib.optionals (keySet != { }) [{ + name = n; + value = keySet; + }] ++ lib.mapAttrsToList + (group: v: { + name = "${n}][${group}"; + value = v; + }) + sectionSet) + config)); - flatCfg = builtins.mapAttrs(file: flatten) cfg; + flatCfg = builtins.mapAttrs (file: flatten) cfg; ############################################################################## # A type for storing settings. settingsFileType = with lib.types; let valueType = attrsOf (nullOr (oneOf [ bool float int str valueType ])) // { description = "Plasma settings"; }; - in attrsOf (attrsOf (valueType)); + in + attrsOf (attrsOf (valueType)); ############################################################################## # Generate a script that will use write_config.py to update all