Skip to content

Commit

Permalink
Use nested attrsets for nested sections (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCash authored Mar 23, 2024
1 parent 26acf8f commit dda62b1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
3 changes: 1 addition & 2 deletions example/home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
};
}
53 changes: 30 additions & 23 deletions modules/files.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,38 @@ 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.
Expand Down Expand Up @@ -98,7 +105,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
Expand All @@ -107,7 +114,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
Expand All @@ -116,7 +123,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
Expand Down
2 changes: 1 addition & 1 deletion modules/kwin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions modules/shortcuts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions script/rc2nix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def parse
############################################################################
def parse_group(line)
line.gsub(/\s*\[([^\]]+)\]\s*/) do |match|
$1 + "."
end.sub(/\.$/, '')
$1 + "\".\""
end.sub(/\"."$/, '')
end
end

Expand Down
7 changes: 1 addition & 6 deletions script/write_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit dda62b1

Please sign in to comment.