From 9ffb206050bab47122eac18ea7cb289849f8d128 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 3 May 2021 21:54:35 -0600 Subject: [PATCH] i3, sway: replace fonts with submodule (#1950) This applies to both the root-level and the bar configs. Closes #1937. --- .../services/window-managers/i3-sway/i3.nix | 14 ++- .../window-managers/i3-sway/lib/functions.nix | 22 +++- .../window-managers/i3-sway/lib/options.nix | 54 +++++++-- .../services/window-managers/i3-sway/sway.nix | 12 +- .../services/window-managers/i3/default.nix | 2 + .../i3/i3-followmouse-expected.conf | 4 +- .../i3/i3-fonts-deprecated.nix | 39 +++++++ .../window-managers/i3/i3-fonts-expected.conf | 105 ++++++++++++++++++ .../services/window-managers/i3/i3-fonts.nix | 43 +++++++ .../i3/i3-keybindings-expected.conf | 4 +- .../window-managers/sway/sway-default.conf | 4 +- .../sway/sway-followmouse-expected.conf | 2 +- .../sway-followmouse-legacy-expected.conf | 2 +- .../window-managers/sway/sway-modules.conf | 4 +- .../sway/sway-null-package.conf | 4 +- 15 files changed, 284 insertions(+), 31 deletions(-) create mode 100644 tests/modules/services/window-managers/i3/i3-fonts-deprecated.nix create mode 100644 tests/modules/services/window-managers/i3/i3-fonts-expected.conf create mode 100644 tests/modules/services/window-managers/i3/i3-fonts.nix diff --git a/modules/services/window-managers/i3-sway/i3.nix b/modules/services/window-managers/i3-sway/i3.nix index 35efee454..b213635e2 100644 --- a/modules/services/window-managers/i3-sway/i3.nix +++ b/modules/services/window-managers/i3-sway/i3.nix @@ -140,7 +140,8 @@ let inherit (commonFunctions) keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr - floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString; + floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString + fontConfigStr; startupEntryStr = { command, always, notification, workspace, ... }: '' ${if always then "exec_always" else "exec"} ${ @@ -155,7 +156,7 @@ let configFile = pkgs.writeText "i3.conf" ((if cfg.config != null then with cfg.config; '' - font pango:${concatStringsSep ", " fonts} + ${fontConfigStr fonts} floating_modifier ${floating.modifier} ${windowBorderString window floating} hide_edge_borders ${window.hideEdgeBorders} @@ -257,6 +258,15 @@ in { mkDefault (if (cfg.config.gaps != null) then pkgs.i3-gaps else pkgs.i3); }) + { + warnings = (optional (isList cfg.config.fonts) + "Specifying i3.config.fonts as a list is deprecated. Use the attrset version instead.") + ++ flatten (map (b: + optional (isList b.fonts) + "Specifying i3.config.bars[].fonts as a list is deprecated. Use the attrset version instead.") + cfg.config.bars); + } + (mkIf (cfg.config != null && (any (s: s.workspace != null) cfg.config.startup)) { warnings = [ diff --git a/modules/services/window-managers/i3-sway/lib/functions.nix b/modules/services/window-managers/i3-sway/lib/functions.nix index 967dbb5e0..09f1e2a12 100644 --- a/modules/services/window-managers/i3-sway/lib/functions.nix +++ b/modules/services/window-managers/i3-sway/lib/functions.nix @@ -39,6 +39,23 @@ rec { concatStringsSep "\n" (map (c: "assign ${criteriaStr c} ${workspace}") criteria); + fontConfigStr = let + toFontStr = { names, style ? "", size ? "" }: + optionalString (names != [ ]) concatStringsSep " " (filter (x: x != "") [ + "font" + "pango:${concatStringsSep ", " names}" + style + size + ]); + in fontCfg: + if isList fontCfg then + toFontStr { names = fontCfg; } + else + toFontStr { + inherit (fontCfg) names style; + size = toString fontCfg.size; + }; + barStr = { id, fonts, mode, hiddenState, position, workspaceButtons , workspaceNumbers, command, statusCommand, colors, trayOutput, extraConfig , ... }: @@ -46,10 +63,7 @@ rec { in '' bar { ${optionalString (id != null) "id ${id}"} - ${ - optionalString (fonts != [ ]) - "font pango:${concatStringsSep ", " fonts}" - } + ${fontConfigStr fonts} ${optionalString (mode != null) "mode ${mode}"} ${optionalString (hiddenState != null) "hidden_state ${hiddenState}"} ${optionalString (position != null) "position ${position}"} diff --git a/modules/services/window-managers/i3-sway/lib/options.nix b/modules/services/window-managers/i3-sway/lib/options.nix index 1662b3542..252562073 100644 --- a/modules/services/window-managers/i3-sway/lib/options.nix +++ b/modules/services/window-managers/i3-sway/lib/options.nix @@ -7,14 +7,37 @@ let isI3 = moduleName == "i3"; isSway = !isI3; - fonts = mkOption { - type = types.listOf types.str; - default = [ "monospace 8" ]; - description = '' - Font list used for window titles. Only FreeType fonts are supported. - The order here is important (e.g. icons font should go before the one used for text). - ''; - example = [ "FontAwesome 10" "Terminus 10" ]; + fontOptions = types.submodule { + options = { + names = mkOption { + type = types.listOf types.str; + default = [ "monospace" ]; + defaultText = literalExample ''[ "monospace" ]''; + description = '' + List of font names list used for window titles. Only FreeType fonts are supported. + The order here is important (e.g. icons font should go before the one used for text). + ''; + example = literalExample ''[ "FontAwesome" "Terminus" ]''; + }; + + style = mkOption { + type = types.str; + default = ""; + description = '' + The font style to use for window titles. + ''; + example = "Bold Semi-Condensed"; + }; + + size = mkOption { + type = types.float; + default = 8.0; + description = '' + The font size to use for window titles. + ''; + example = 11.5; + }; + }; }; startupModule = types.submodule { @@ -69,7 +92,10 @@ let ''; }); in { - fonts = fonts // optionalAttrs versionAtLeast2009 { default = [ ]; }; + fonts = mkOption { + type = with types; either (listOf str) fontOptions; + default = { }; + }; extraConfig = mkOption { type = types.lines; @@ -310,7 +336,10 @@ let criteriaModule = types.attrsOf types.str; in { - inherit fonts; + fonts = mkOption { + type = with types; either (listOf str) fontOptions; + default = { }; + }; window = mkOption { type = types.submodule { @@ -605,7 +634,10 @@ in { workspaceButtons = true; workspaceNumbers = true; statusCommand = "${pkgs.i3status}/bin/i3status"; - fonts = [ "monospace 8" ]; + fonts = { + names = [ "monospace" ]; + size = 8.0; + }; trayOutput = "primary"; colors = { background = "#000000"; diff --git a/modules/services/window-managers/i3-sway/sway.nix b/modules/services/window-managers/i3-sway/sway.nix index da646988e..ff0531d0a 100644 --- a/modules/services/window-managers/i3-sway/sway.nix +++ b/modules/services/window-managers/i3-sway/sway.nix @@ -245,7 +245,8 @@ let inherit (commonFunctions) keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr - floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString; + floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString + fontConfigStr; startupEntryStr = { command, always, ... }: '' ${if always then "exec_always" else "exec"} ${command} @@ -263,7 +264,7 @@ let configFile = pkgs.writeText "sway.conf" ((if cfg.config != null then with cfg.config; '' - font pango:${concatStringsSep ", " fonts} + ${fontConfigStr fonts} floating_modifier ${floating.modifier} ${windowBorderString window floating} hide_edge_borders ${window.hideEdgeBorders} @@ -410,6 +411,13 @@ in { }; config = mkIf cfg.enable { + warnings = (optional (isList cfg.config.fonts) + "Specifying sway.config.fonts as a list is deprecated. Use the attrset version instead.") + ++ flatten (map (b: + optional (isList b.fonts) + "Specifying sway.config.bars[].fonts as a list is deprecated. Use the attrset version instead.") + cfg.config.bars); + home.packages = optional (cfg.package != null) cfg.package ++ optional cfg.xwayland pkgs.xwayland; xdg.configFile."sway/config" = { diff --git a/tests/modules/services/window-managers/i3/default.nix b/tests/modules/services/window-managers/i3/default.nix index c523dfcd0..af48aef45 100644 --- a/tests/modules/services/window-managers/i3/default.nix +++ b/tests/modules/services/window-managers/i3/default.nix @@ -1,4 +1,6 @@ { i3-followmouse = ./i3-followmouse.nix; + i3-fonts = ./i3-fonts.nix; + i3-fonts-deprecated = ./i3-fonts-deprecated.nix; i3-keybindings = ./i3-keybindings.nix; } diff --git a/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf b/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf index 3727068b1..d1ddfa8b1 100644 --- a/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border normal 2 default_floating_border normal 2 @@ -76,7 +76,7 @@ bindsym Up resize shrink height 10 px or 10 ppt bar { - font pango:monospace 8 + font pango:monospace 8.000000 mode dock hidden_state hide position bottom diff --git a/tests/modules/services/window-managers/i3/i3-fonts-deprecated.nix b/tests/modules/services/window-managers/i3/i3-fonts-deprecated.nix new file mode 100644 index 000000000..fd11ac51a --- /dev/null +++ b/tests/modules/services/window-managers/i3/i3-fonts-deprecated.nix @@ -0,0 +1,39 @@ +{ config, lib, ... }: + +with lib; + +{ + config = { + xsession.windowManager.i3 = { + enable = true; + + config = { + bars = [{ fonts = [ "FontAwesome" "Iosevka 11.500000" ]; }]; + fonts = [ "DejaVuSansMono" "Terminus Bold Semi-Condensed 13.500000" ]; + }; + }; + + nixpkgs.overlays = [ + (self: super: { + dmenu = super.dmenu // { outPath = "@dmenu@"; }; + + i3 = super.writeScriptBin "i3" "" // { outPath = "@i3@"; }; + + i3-gaps = super.writeScriptBin "i3" "" // { outPath = "@i3-gaps@"; }; + + i3status = super.i3status // { outPath = "@i3status@"; }; + }) + ]; + + nmt.script = '' + assertFileExists home-files/.config/i3/config + assertFileContent home-files/.config/i3/config \ + ${./i3-fonts-expected.conf} + ''; + + test.asserts.warnings.expected = [ + "Specifying i3.config.fonts as a list is deprecated. Use the attrset version instead." + "Specifying i3.config.bars[].fonts as a list is deprecated. Use the attrset version instead." + ]; + }; +} diff --git a/tests/modules/services/window-managers/i3/i3-fonts-expected.conf b/tests/modules/services/window-managers/i3/i3-fonts-expected.conf new file mode 100644 index 000000000..59fe6a7b6 --- /dev/null +++ b/tests/modules/services/window-managers/i3/i3-fonts-expected.conf @@ -0,0 +1,105 @@ +font pango:DejaVuSansMono, Terminus Bold Semi-Condensed 13.500000 +floating_modifier Mod1 +default_border normal 2 +default_floating_border normal 2 +hide_edge_borders none +force_focus_wrapping no +focus_follows_mouse yes +focus_on_window_activation smart +mouse_warping output +workspace_layout default +workspace_auto_back_and_forth no + +client.focused #4c7899 #285577 #ffffff #2e9ef4 #285577 +client.focused_inactive #333333 #5f676a #ffffff #484e50 #5f676a +client.unfocused #333333 #222222 #888888 #292d2e #222222 +client.urgent #2f343a #900000 #ffffff #900000 #900000 +client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c +client.background #ffffff + +bindsym Mod1+0 workspace number 10 +bindsym Mod1+1 workspace number 1 +bindsym Mod1+2 workspace number 2 +bindsym Mod1+3 workspace number 3 +bindsym Mod1+4 workspace number 4 +bindsym Mod1+5 workspace number 5 +bindsym Mod1+6 workspace number 6 +bindsym Mod1+7 workspace number 7 +bindsym Mod1+8 workspace number 8 +bindsym Mod1+9 workspace number 9 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Shift+1 move container to workspace number 1 +bindsym Mod1+Shift+2 move container to workspace number 2 +bindsym Mod1+Shift+3 move container to workspace number 3 +bindsym Mod1+Shift+4 move container to workspace number 4 +bindsym Mod1+Shift+5 move container to workspace number 5 +bindsym Mod1+Shift+6 move container to workspace number 6 +bindsym Mod1+Shift+7 move container to workspace number 7 +bindsym Mod1+Shift+8 move container to workspace number 8 +bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+Down move down +bindsym Mod1+Shift+Left move left +bindsym Mod1+Shift+Right move right +bindsym Mod1+Shift+Up move up +bindsym Mod1+Shift+c reload +bindsym Mod1+Shift+e exec i3-nagbar -t warning -m 'Do you want to exit i3?' -b 'Yes' 'i3-msg exit' +bindsym Mod1+Shift+minus move scratchpad +bindsym Mod1+Shift+q kill +bindsym Mod1+Shift+r restart +bindsym Mod1+Shift+space floating toggle +bindsym Mod1+Up focus up +bindsym Mod1+a focus parent +bindsym Mod1+d exec @dmenu@/bin/dmenu_run +bindsym Mod1+e layout toggle split +bindsym Mod1+f fullscreen toggle +bindsym Mod1+h split h +bindsym Mod1+minus scratchpad show +bindsym Mod1+r mode resize +bindsym Mod1+s layout stacking +bindsym Mod1+space focus mode_toggle +bindsym Mod1+v split v +bindsym Mod1+w layout tabbed + +mode "resize" { +bindsym Down resize grow height 10 px or 10 ppt +bindsym Escape mode default +bindsym Left resize shrink width 10 px or 10 ppt +bindsym Return mode default +bindsym Right resize grow width 10 px or 10 ppt +bindsym Up resize shrink height 10 px or 10 ppt +} + + +bar { + + font pango:FontAwesome, Iosevka 11.500000 + mode dock + hidden_state hide + position bottom + status_command @i3status@/bin/i3status + i3bar_command @i3@/bin/i3bar + workspace_buttons yes + strip_workspace_numbers no + tray_output primary + colors { + background #000000 + statusline #ffffff + separator #666666 + focused_workspace #4c7899 #285577 #ffffff + active_workspace #333333 #5f676a #ffffff + inactive_workspace #333333 #222222 #888888 + urgent_workspace #2f343a #900000 #ffffff + binding_mode #2f343a #900000 #ffffff + } + +} + + + + + + diff --git a/tests/modules/services/window-managers/i3/i3-fonts.nix b/tests/modules/services/window-managers/i3/i3-fonts.nix new file mode 100644 index 000000000..e594f6363 --- /dev/null +++ b/tests/modules/services/window-managers/i3/i3-fonts.nix @@ -0,0 +1,43 @@ +{ config, lib, ... }: + +with lib; + +{ + config = { + xsession.windowManager.i3 = { + enable = true; + + config = { + bars = [{ + fonts = { + names = [ "FontAwesome" "Iosevka" ]; + size = 11.5; + }; + }]; + fonts = { + names = [ "DejaVuSansMono" "Terminus" ]; + style = "Bold Semi-Condensed"; + size = 13.5; + }; + }; + }; + + nixpkgs.overlays = [ + (self: super: { + dmenu = super.dmenu // { outPath = "@dmenu@"; }; + + i3 = super.writeScriptBin "i3" "" // { outPath = "@i3@"; }; + + i3-gaps = super.writeScriptBin "i3" "" // { outPath = "@i3-gaps@"; }; + + i3status = super.i3status // { outPath = "@i3status@"; }; + }) + ]; + + nmt.script = '' + assertFileExists home-files/.config/i3/config + assertFileContent home-files/.config/i3/config \ + ${./i3-fonts-expected.conf} + ''; + }; +} diff --git a/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf b/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf index 38ed84422..2e03d4642 100644 --- a/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border normal 2 default_floating_border normal 2 @@ -77,7 +77,7 @@ bindsym Up resize shrink height 10 px or 10 ppt bar { - font pango:monospace 8 + font pango:monospace 8.000000 mode dock hidden_state hide position bottom diff --git a/tests/modules/services/window-managers/sway/sway-default.conf b/tests/modules/services/window-managers/sway/sway-default.conf index bb0317b7e..1ab327a46 100644 --- a/tests/modules/services/window-managers/sway/sway-default.conf +++ b/tests/modules/services/window-managers/sway/sway-default.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border pixel 2 default_floating_border pixel 2 @@ -84,7 +84,7 @@ bindsym l resize grow width 10 px bar { - font pango:monospace 8 + font pango:monospace 8.000000 mode dock hidden_state hide position bottom diff --git a/tests/modules/services/window-managers/sway/sway-followmouse-expected.conf b/tests/modules/services/window-managers/sway/sway-followmouse-expected.conf index 931a3ec38..f64b05df0 100644 --- a/tests/modules/services/window-managers/sway/sway-followmouse-expected.conf +++ b/tests/modules/services/window-managers/sway/sway-followmouse-expected.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border pixel 2 default_floating_border pixel 2 diff --git a/tests/modules/services/window-managers/sway/sway-followmouse-legacy-expected.conf b/tests/modules/services/window-managers/sway/sway-followmouse-legacy-expected.conf index 3684f1f98..5eb26cddf 100644 --- a/tests/modules/services/window-managers/sway/sway-followmouse-legacy-expected.conf +++ b/tests/modules/services/window-managers/sway/sway-followmouse-legacy-expected.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border pixel 2 default_floating_border pixel 2 diff --git a/tests/modules/services/window-managers/sway/sway-modules.conf b/tests/modules/services/window-managers/sway/sway-modules.conf index cba1c628c..9fbdee0d8 100644 --- a/tests/modules/services/window-managers/sway/sway-modules.conf +++ b/tests/modules/services/window-managers/sway/sway-modules.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border pixel 2 default_floating_border pixel 2 @@ -96,7 +96,7 @@ bindsym l resize grow width 10 px bar { - font pango:monospace 8 + font pango:monospace 8.000000 mode dock hidden_state hide position bottom diff --git a/tests/modules/services/window-managers/sway/sway-null-package.conf b/tests/modules/services/window-managers/sway/sway-null-package.conf index 00cefc617..86c9b98ee 100644 --- a/tests/modules/services/window-managers/sway/sway-null-package.conf +++ b/tests/modules/services/window-managers/sway/sway-null-package.conf @@ -1,4 +1,4 @@ -font pango:monospace 8 +font pango:monospace 8.000000 floating_modifier Mod1 default_border pixel 2 default_floating_border pixel 2 @@ -84,7 +84,7 @@ bindsym l resize grow width 10 px bar { - font pango:monospace 8 + font pango:monospace 8.000000 mode dock hidden_state hide position bottom