From 134deb46abd5d0889d913b8509413f6f38b0811e Mon Sep 17 00:00:00 2001 From: Leah Amelia Chen Date: Thu, 23 Nov 2023 00:16:58 +0100 Subject: [PATCH] bat: support boolean flags in config Previously, users cannot enable boolean flags like `--show-all` in bat's config since all options were expected to be either a string, or a list of strings. With this commit boolean flags are simply appended to the end of the config if they are set to `true`, and discarded otherwise. For example, the config { theme = "TwoDark"; show-all = true; lessopen = false; } would produce a config file that looks like --theme='TwoDark' --show-all Fixes #4657 --- modules/programs/bat.nix | 20 +++++++++++++++----- tests/modules/programs/bat/bat.nix | 5 +++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/programs/bat.nix b/modules/programs/bat.nix index a5bf3481b..b3951c33b 100644 --- a/modules/programs/bat.nix +++ b/modules/programs/bat.nix @@ -8,10 +8,20 @@ let package = pkgs.bat; - toConfigFile = generators.toKeyValue { - mkKeyValue = k: v: "--${k}=${lib.escapeShellArg v}"; - listsAsDuplicateKeys = true; - }; + toConfigFile = attrs: + let + inherit (builtins) isBool attrNames; + nonBoolFlags = filterAttrs (_: v: !(isBool v)) attrs; + enabledBoolFlags = filterAttrs (_: v: isBool v && v) attrs; + + keyValuePairs = generators.toKeyValue { + mkKeyValue = k: v: "--${k}=${lib.escapeShellArg v}"; + listsAsDuplicateKeys = true; + } nonBoolFlags; + switches = concatMapStrings (k: '' + --${k} + '') (attrNames enabledBoolFlags); + in keyValuePairs + switches; in { meta.maintainers = [ ]; @@ -20,7 +30,7 @@ in { enable = mkEnableOption "bat, a cat clone with wings"; config = mkOption { - type = with types; attrsOf (either str (listOf str)); + type = with types; attrsOf (oneOf [ str (listOf str) bool ]); default = { }; example = { theme = "TwoDark"; diff --git a/tests/modules/programs/bat/bat.nix b/tests/modules/programs/bat/bat.nix index d6b2374c1..cd169ffb7 100644 --- a/tests/modules/programs/bat/bat.nix +++ b/tests/modules/programs/bat/bat.nix @@ -11,6 +11,10 @@ with lib; theme = "TwoDark"; pager = "less -FR"; map-syntax = [ "*.jenkinsfile:Groovy" "*.props:Java Properties" ]; + show-all = true; + + # False boolean options should not appear in the config + lessopen = false; }; themes.testtheme.src = pkgs.writeText "testtheme.tmTheme" '' @@ -32,6 +36,7 @@ with lib; --map-syntax='*.props:Java Properties' --pager='less -FR' --theme='TwoDark' + --show-all '' }