From 87e2ec341bfda373d50ab58529b4bbb0eb9eb9a0 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 9 Feb 2021 11:59:29 -0300 Subject: [PATCH] rofi: support top-level clauses in rasi (#1788) See https://github.com/nix-community/home-manager/pull/1748#issuecomment-774995577 for details. Related documentation: https://github.com/davatorium/rofi/wiki/Configuring-Rofi/920de75c4bfc64697dd1700c26f850d01508e957#splitting-configuration-over-multiple-files --- modules/programs/rofi.nix | 38 +++++++++++-------- tests/modules/programs/rofi/custom-theme.nix | 2 + tests/modules/programs/rofi/custom-theme.rasi | 2 + 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/modules/programs/rofi.nix b/modules/programs/rofi.nix index 8167c8af3..042b0b211 100644 --- a/modules/programs/rofi.nix +++ b/modules/programs/rofi.nix @@ -117,23 +117,27 @@ let else abort "Unhandled value type ${builtins.typeOf value}"; - mkKeyValue = name: value: "${name}: ${mkValueString value};"; + mkKeyValue = { sep ? ": ", end ? ";" }: + name: value: + "${name}${sep}${mkValueString value}${end}"; - mkRasiSection = section: config: - let - toRasiKeyValue = generators.toKeyValue { inherit mkKeyValue; }; - # Remove null values so the resulting config does not have empty lines - configStr = toRasiKeyValue (filterAttrs (_: v: v != null) config); - in '' - ${section} { - ${configStr}} - ''; + mkRasiSection = name: value: + if isAttrs value then + let + toRasiKeyValue = generators.toKeyValue { mkKeyValue = mkKeyValue { }; }; + # Remove null values so the resulting config does not have empty lines + configStr = toRasiKeyValue (filterAttrs (_: v: v != null) value); + in '' + ${name} { + ${configStr}} + '' + else + mkKeyValue { + sep = " "; + end = ""; + } name value; - toRasi = attrsOfAttrs: - let - mkSection = mkRasiSection; - sections = mapAttrsToList mkSection attrsOfAttrs; - in concatStringsSep "\n" sections; + toRasi = attrs: concatStringsSep "\n" (mapAttrsToList mkRasiSection attrs); locationsMap = { center = 0; @@ -149,7 +153,9 @@ let primitive = with types; (oneOf [ str int bool rasiLiteral ]); - configType = with types; attrsOf (either primitive (listOf primitive)); + # Either a `section { foo: "bar"; }` or a `@import/@theme "some-text"` + configType = with types; + (either (attrsOf (either primitive (listOf primitive))) str); rasiLiteral = types.submodule { options = { diff --git a/tests/modules/programs/rofi/custom-theme.nix b/tests/modules/programs/rofi/custom-theme.nix index c787242d8..2d318a4e4 100644 --- a/tests/modules/programs/rofi/custom-theme.nix +++ b/tests/modules/programs/rofi/custom-theme.nix @@ -9,6 +9,8 @@ with lib; theme = let inherit (config.lib.formats.rasi) mkLiteral; in { + "@import" = "~/.cache/wal/colors-rofi-dark"; + "*" = { background-color = mkLiteral "#000000"; foreground-color = mkLiteral "rgba ( 250, 251, 252, 100 % )"; diff --git a/tests/modules/programs/rofi/custom-theme.rasi b/tests/modules/programs/rofi/custom-theme.rasi index bf9bb9cc6..b018843fe 100644 --- a/tests/modules/programs/rofi/custom-theme.rasi +++ b/tests/modules/programs/rofi/custom-theme.rasi @@ -15,3 +15,5 @@ border-color: #FFFFFF; foreground-color: rgba ( 250, 251, 252, 100 % ); width: 512; } + +@import "~/.cache/wal/colors-rofi-dark" \ No newline at end of file