From d3bb2e0e5766984f919f515194a1894906fa35aa Mon Sep 17 00:00:00 2001 From: Anthony Rodriguez Date: Tue, 18 Feb 2025 09:33:53 +0100 Subject: [PATCH] ghostty: support nesting attrs This makes the hjem ghostty module more RFC42 compliant, as it can now map nested attribute sets one level deep (as per Ghostty's configuration format). --- .../programs/terminal/emulators/ghostty.nix | 80 +++++++++---------- .../hjem/collection/programs/ghostty.nix | 62 ++++++++------ 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/modules/programs/terminal/emulators/ghostty.nix b/modules/programs/terminal/emulators/ghostty.nix index ee67ca3..9f32dd7 100644 --- a/modules/programs/terminal/emulators/ghostty.nix +++ b/modules/programs/terminal/emulators/ghostty.nix @@ -15,30 +15,30 @@ in with colors; { base16 = { - palette = [ - "0=#${base00}" - "1=#${base08}" - "2=#${base0B}" - "3=#${base0A}" - "4=#${base0D}" - "5=#${base0E}" - "6=#${base0C}" - "7=#${base05}" - "8=#${base02}" - "9=#${base08}" - "10=#${base0B}" - "11=#${base0A}" - "12=#${base0D}" - "13=#${base0E}" - "14=#${base0C}" - "15=#${base07}" - "16=#${base09}" - "17=#${base0F}" - "18=#${base01}" - "19=#${base02}" - "20=#${base04}" - "21=#${base06}" - ]; + palette = { + "0" = "#${base00}"; + "1" = "#${base08}"; + "2" = "#${base0B}"; + "3" = "#${base0A}"; + "4" = "#${base0D}"; + "5" = "#${base0E}"; + "6" = "#${base0C}"; + "7" = "#${base05}"; + "8" = "#${base02}"; + "9" = "#${base08}"; + "10" = "#${base0B}"; + "11" = "#${base0A}"; + "12" = "#${base0D}"; + "13" = "#${base0E}"; + "14" = "#${base0C}"; + "15" = "#${base07}"; + "16" = "#${base09}"; + "17" = "#${base0F}"; + "18" = "#${base01}"; + "19" = "#${base02}"; + "20" = "#${base04}"; + "21" = "#${base06}"; + }; background = base00; foreground = base05; cursor-color = base06; @@ -59,23 +59,23 @@ in { gtk-adwaita = false; confirm-close-surface = false; - keybind = [ - "${prefix}>c=new_tab" - "${prefix}>p=move_tab:-1" - "${prefix}>n=move_tab:1" + keybind = { + "${prefix}>c" = "new_tab"; + "${prefix}>p" = "move_tab:-1"; + "${prefix}>n" = "move_tab:1"; - "${prefix}>\\=new_split:right" - "${prefix}>-=new_split:down" - "${prefix}>h=goto_split:left" - "${prefix}>j=goto_split:bottom" - "${prefix}>k=goto_split:top" - "${prefix}>l=goto_split:right" - "${prefix}>shift+h=resize_split:left,10" - "${prefix}>shift+j=resize_split:down,10" - "${prefix}>shift+k=resize_split:up,10" - "${prefix}>shift+l=resize_split:right,11" - "${prefix}>z=toggle_split_zoom" - ]; + "${prefix}>\\" = "new_split:right"; + "${prefix}>-" = "new_split:down"; + "${prefix}>h" = "goto_split:left"; + "${prefix}>j" = "goto_split:bottom"; + "${prefix}>k" = "goto_split:top"; + "${prefix}>l" = "goto_split:right"; + "${prefix}>shift+h" = "resize_split:left,10"; + "${prefix}>shift+j" = "resize_split:down,10"; + "${prefix}>shift+k" = "resize_split:up,10"; + "${prefix}>shift+l" = "resize_split:right,11"; + "${prefix}>z" = "toggle_split_zoom"; + }; adw-toolbar-style = "flat"; gtk-tabs-location = "bottom"; diff --git a/shared/modules/hjem/collection/programs/ghostty.nix b/shared/modules/hjem/collection/programs/ghostty.nix index 884c6ad..c6607db 100644 --- a/shared/modules/hjem/collection/programs/ghostty.nix +++ b/shared/modules/hjem/collection/programs/ghostty.nix @@ -4,11 +4,21 @@ pkgs, ... }: let - inherit (lib.attrsets) mapAttrs' nameValuePair; + inherit (lib.attrsets) isAttrs mapAttrs' mapAttrsToList nameValuePair; + inherit (lib.generators) mkKeyValueDefault mkValueStringDefault toINIWithGlobalSection; inherit (lib.options) mkEnableOption mkOption; inherit (lib.types) attrs attrsOf package; - toINI = lib.generators.toINIWithGlobalSection {listsAsDuplicateKeys = true;}; + mkKeyValue = key: value: + if isAttrs value + # ghostty's configuration format supports (so far) one level of nested keys as key1=key2=value + then builtins.concatStringsSep "\n" (mapAttrsToList (k: v: "${key}=${k}=${v}") value) + else (mkKeyValueDefault {mkValueString = mkValueStringDefault {};} "=" key value); + + toGhosttyConf = toINIWithGlobalSection { + listsAsDuplicateKeys = true; + inherit mkKeyValue; + }; cfg = config.programs.ghostty; @@ -18,7 +28,7 @@ nameValuePair ".config/ghostty/themes/${name}" { - text = toINI {globalSection = value.${name};}; + text = toGhosttyConf {globalSection = value.${name};}; }) themes; in { @@ -37,10 +47,10 @@ in { example = { theme = "example-theme"; font-size = 10; - keybind = [ - "ctrl+h=goto_split:left" - "ctrl+l=goto_split:right" - ]; + keybind = { + "ctrl+h" = "goto_split:left"; + "ctrl+l" = "goto_split:right"; + }; }; description = '' The configuration converted to INI and written to `${config.directory}/.config/ghostty/config`. @@ -52,24 +62,24 @@ in { default = {}; example = { example-theme = { - palette = [ - "0=#51576d" - "1=#e78284" - "2=#a6d189" - "3=#e5c890" - "4=#8caaee" - "5=#f4b8e4" - "6=#81c8be" - "7=#a5adce" - "8=#626880" - "9=#e67172" - "10=#8ec772" - "11=#d9ba73" - "12=#7b9ef0" - "13=#f2a4db" - "14=#5abfb5" - "15=#b5bfe2" - ]; + palette = { + "0" = "#51576d"; + "1" = "#e78284"; + "2" = "#a6d189"; + "3" = "#e5c890"; + "4" = "#8caaee"; + "5" = "#f4b8e4"; + "6" = "#81c8be"; + "7" = "#a5adce"; + "8" = "#626880"; + "9" = "#e67172"; + "10" = "#8ec772"; + "11" = "#d9ba73"; + "12" = "#7b9ef0"; + "13" = "#f2a4db"; + "14" = "#5abfb5"; + "15" = "#b5bfe2"; + }; background = "#303446"; foreground = "#c6d0f5"; @@ -90,7 +100,7 @@ in { packages = [cfg.package]; files = { - ".config/ghostty/config".text = toINI {globalSection = cfg.settings;}; + ".config/ghostty/config".text = toGhosttyConf {globalSection = cfg.settings;}; } // mkThemes cfg.themes; };