From 55e2a4d341bba9307679b03ac5a7c2d39fc3d67e Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 21 Mar 2022 17:50:43 -0600 Subject: [PATCH] lib.booleans: add yesNo function Closes #2772 --- modules/lib/booleans.nix | 5 ++++ modules/lib/default.nix | 1 + modules/programs/irssi.nix | 9 +++--- modules/programs/kitty.nix | 6 ++-- modules/programs/mbsync.nix | 2 +- modules/programs/mpv.nix | 4 +-- modules/programs/ncmpcpp.nix | 2 +- modules/programs/neomutt.nix | 9 +++--- modules/programs/newsboat.nix | 2 +- modules/programs/offlineimap.nix | 6 ++-- modules/programs/ssh.nix | 12 ++++---- modules/programs/zsh/prezto.nix | 28 +++++++++---------- modules/services/dunst.nix | 2 +- .../services/window-managers/i3-sway/i3.nix | 6 ++-- .../window-managers/i3-sway/lib/functions.nix | 4 +-- .../window-managers/i3-sway/lib/options.nix | 5 +--- .../services/window-managers/i3-sway/sway.nix | 4 +-- 17 files changed, 51 insertions(+), 56 deletions(-) create mode 100644 modules/lib/booleans.nix diff --git a/modules/lib/booleans.nix b/modules/lib/booleans.nix new file mode 100644 index 000000000..f6c2af586 --- /dev/null +++ b/modules/lib/booleans.nix @@ -0,0 +1,5 @@ +{ lib }: { + # Converts a boolean to a yes/no string. This is used in lots of + # configuration formats. + yesNo = value: if value then "yes" else "no"; +} diff --git a/modules/lib/default.nix b/modules/lib/default.nix index c087c8109..580316e09 100644 --- a/modules/lib/default.nix +++ b/modules/lib/default.nix @@ -18,6 +18,7 @@ rec { assertions = import ./assertions.nix { inherit lib; }; + booleans = import ./booleans.nix { inherit lib; }; gvariant = import ./gvariant.nix { inherit lib; }; maintainers = import ./maintainers.nix; strings = import ./strings.nix { inherit lib; }; diff --git a/modules/programs/irssi.nix b/modules/programs/irssi.nix index ea7498633..a9fb3a9d5 100644 --- a/modules/programs/irssi.nix +++ b/modules/programs/irssi.nix @@ -6,7 +6,6 @@ let cfg = config.programs.irssi; - boolStr = b: if b then "yes" else "no"; quoteStr = s: escape [ ''"'' ] s; # Comma followed by newline. @@ -33,9 +32,9 @@ let chatnet = "${k}"; address = "${v.server.address}"; port = "${toString v.server.port}"; - use_ssl = "${boolStr v.server.ssl.enable}"; - ssl_verify = "${boolStr v.server.ssl.verify}"; - autoconnect = "${boolStr v.server.autoConnect}"; + use_ssl = "${lib.hm.booleans.yesNo v.server.ssl.enable}"; + ssl_verify = "${lib.hm.booleans.yesNo v.server.ssl.verify}"; + autoconnect = "${lib.hm.booleans.yesNo v.server.autoConnect}"; ${ lib.optionalString (v.server.ssl.certificateFile != null) '' ssl_cert = "${v.server.ssl.certificateFile}"; @@ -50,7 +49,7 @@ let { chatnet = "${k}"; name = "${c}"; - autojoin = "${boolStr cv.autoJoin}"; + autojoin = "${lib.hm.booleans.yesNo cv.autoJoin}"; } ''))))); diff --git a/modules/programs/kitty.nix b/modules/programs/kitty.nix index 5ebdf05ea..627cc61f6 100644 --- a/modules/programs/kitty.nix +++ b/modules/programs/kitty.nix @@ -14,10 +14,8 @@ let toKittyConfig = generators.toKeyValue { mkKeyValue = key: value: let - value' = if isBool value then - (if value then "yes" else "no") - else - toString value; + value' = + (if isBool value then lib.hm.booleans.yesNo else toString) value; in "${key} ${value'}"; }; diff --git a/modules/programs/mbsync.nix b/modules/programs/mbsync.nix index c5d8f0da6..373828a0b 100644 --- a/modules/programs/mbsync.nix +++ b/modules/programs/mbsync.nix @@ -63,7 +63,7 @@ let if isList v then concatMapStringsSep " " (genValue n) v else if isBool v then - (if v then "yes" else "no") + lib.hm.booleans.yesNo v else if isInt v then toString v else if isString v && hasSpace v then diff --git a/modules/programs/mpv.nix b/modules/programs/mpv.nix index b37a2516a..0d7668cfa 100644 --- a/modules/programs/mpv.nix +++ b/modules/programs/mpv.nix @@ -18,9 +18,7 @@ let rec { int = toString option; float = int; - - bool = if option then "yes" else "no"; - + bool = lib.hm.booleans.yesNo option; string = option; }.${typeOf option}; diff --git a/modules/programs/ncmpcpp.nix b/modules/programs/ncmpcpp.nix index d19118184..e37849519 100644 --- a/modules/programs/ncmpcpp.nix +++ b/modules/programs/ncmpcpp.nix @@ -14,7 +14,7 @@ let renderValue = option: { int = toString option; - bool = if option then "yes" else "no"; + bool = lib.hm.booleans.yesNo option; string = option; }.${builtins.typeOf option}; diff --git a/modules/programs/neomutt.nix b/modules/programs/neomutt.nix index 2a2f79fae..562763bdd 100644 --- a/modules/programs/neomutt.nix +++ b/modules/programs/neomutt.nix @@ -89,7 +89,6 @@ let }; }; - yesno = x: if x then "yes" else "no"; setOption = n: v: if v == null then "unset ${n}" else "set ${n}=${v}"; escape = replaceStrings [ "%" ] [ "%25" ]; @@ -167,7 +166,7 @@ let sidebarSection = '' # Sidebar set sidebar_visible = yes - set sidebar_short_path = ${yesno cfg.sidebar.shortPath} + set sidebar_short_path = ${lib.hm.booleans.yesNo cfg.sidebar.shortPath} set sidebar_width = ${toString cfg.sidebar.width} set sidebar_format = '${cfg.sidebar.format}' ''; @@ -204,8 +203,10 @@ let # GPG section set crypt_use_gpgme = yes - set crypt_autosign = ${yesno (gpg.signByDefault or false)} - set crypt_opportunistic_encrypt = ${yesno (gpg.encryptByDefault or false)} + set crypt_autosign = ${lib.hm.booleans.yesNo (gpg.signByDefault or false)} + set crypt_opportunistic_encrypt = ${ + lib.hm.booleans.yesNo (gpg.encryptByDefault or false) + } set pgp_use_gpg_agent = yes set mbox_type = ${if maildir != null then "Maildir" else "mbox"} set sort = "${cfg.sort}" diff --git a/modules/programs/newsboat.nix b/modules/programs/newsboat.nix index eac4cce8c..ba074a421 100644 --- a/modules/programs/newsboat.nix +++ b/modules/programs/newsboat.nix @@ -24,7 +24,7 @@ let max-items ${toString cfg.maxItems} browser ${cfg.browser} reload-threads ${toString cfg.reloadThreads} - auto-reload ${if cfg.autoReload then "yes" else "no"} + auto-reload ${lib.hm.booleans.yesNo cfg.autoReload} ${optionalString (cfg.reloadTime != null) (toString "reload-time ${toString cfg.reloadTime}")} prepopulate-query-feeds yes diff --git a/modules/programs/offlineimap.nix b/modules/programs/offlineimap.nix index 83a31dd2b..8817e2ba7 100644 --- a/modules/programs/offlineimap.nix +++ b/modules/programs/offlineimap.nix @@ -12,10 +12,8 @@ let toIni = generators.toINI { mkKeyValue = key: value: let - value' = if isBool value then - (if value then "yes" else "no") - else - toString value; + value' = + (if isBool value then lib.hm.booleans.yesNo else toString) value; in "${key} = ${value'}"; }; diff --git a/modules/programs/ssh.nix b/modules/programs/ssh.nix index b51ecedf4..05d16a2c4 100644 --- a/modules/programs/ssh.nix +++ b/modules/programs/ssh.nix @@ -13,8 +13,6 @@ let then " ${entry.address}" else " [${entry.address}]:${toString entry.port}"; - yn = flag: if flag then "yes" else "no"; - unwords = builtins.concatStringsSep " "; bindOptions = { @@ -284,7 +282,7 @@ let matchBlockStr = cf: concatStringsSep "\n" ( ["Host ${cf.host}"] ++ optional (cf.port != null) " Port ${toString cf.port}" - ++ optional (cf.forwardAgent != null) " ForwardAgent ${yn cf.forwardAgent}" + ++ optional (cf.forwardAgent != null) " ForwardAgent ${lib.hm.booleans.yesNo cf.forwardAgent}" ++ optional cf.forwardX11 " ForwardX11 yes" ++ optional cf.forwardX11Trusted " ForwardX11Trusted yes" ++ optional cf.identitiesOnly " IdentitiesOnly yes" @@ -296,7 +294,7 @@ let " ServerAliveInterval ${toString cf.serverAliveInterval}" ++ optional (cf.serverAliveCountMax != 3) " ServerAliveCountMax ${toString cf.serverAliveCountMax}" - ++ optional (cf.compression != null) " Compression ${yn cf.compression}" + ++ optional (cf.compression != null) " Compression ${lib.hm.booleans.yesNo cf.compression}" ++ optional (!cf.checkHostIP) " CheckHostIP no" ++ optional (cf.proxyCommand != null) " ProxyCommand ${cf.proxyCommand}" ++ optional (cf.proxyJump != null) " ProxyJump ${cf.proxyJump}" @@ -498,11 +496,11 @@ in )} Host * - ForwardAgent ${yn cfg.forwardAgent} - Compression ${yn cfg.compression} + ForwardAgent ${lib.hm.booleans.yesNo cfg.forwardAgent} + Compression ${lib.hm.booleans.yesNo cfg.compression} ServerAliveInterval ${toString cfg.serverAliveInterval} ServerAliveCountMax ${toString cfg.serverAliveCountMax} - HashKnownHosts ${yn cfg.hashKnownHosts} + HashKnownHosts ${lib.hm.booleans.yesNo cfg.hashKnownHosts} UserKnownHostsFile ${cfg.userKnownHostsFile} ControlMaster ${cfg.controlMaster} ControlPath ${cfg.controlPath} diff --git a/modules/programs/zsh/prezto.nix b/modules/programs/zsh/prezto.nix index 5c2853e7a..17680ff25 100644 --- a/modules/programs/zsh/prezto.nix +++ b/modules/programs/zsh/prezto.nix @@ -365,11 +365,11 @@ in { # Generated by Nix ${optionalString (cfg.caseSensitive != null) '' zstyle ':prezto:*:*' case-sensitive '${ - if cfg.caseSensitive then "yes" else "no" + lib.hm.booleans.yesNo cfg.caseSensitive }' ''} ${optionalString (cfg.color != null) '' - zstyle ':prezto:*:*' color '${if cfg.color then "yes" else "no"}' + zstyle ':prezto:*:*' color '${lib.hm.booleans.yesNo cfg.color}' ''} ${optionalString (cfg.pmoduleDirs != [ ]) '' zstyle ':prezto:load' pmodule-dirs ${ @@ -410,12 +410,12 @@ in { ''} ${optionalString (cfg.editor.dotExpansion != null) '' zstyle ':prezto:module:editor' dot-expansion '${ - if cfg.editor.dotExpansion then "yes" else "no" + lib.hm.booleans.yesNo cfg.editor.dotExpansion }' ''} ${optionalString (cfg.editor.promptContext != null) '' zstyle ':prezto:module:editor' ps-context '${ - if cfg.editor.promptContext then "yes" else "no" + lib.hm.booleans.yesNo cfg.editor.promptContext }' ''} ${optionalString (cfg.git.submoduleIgnore != null) '' @@ -447,27 +447,27 @@ in { ''} ${optionalString (cfg.python.virtualenvAutoSwitch != null) '' zstyle ':prezto:module:python:virtualenv' auto-switch '${ - if cfg.python.virtualenvAutoSwitch then "yes" else "no" + lib.hm.booleans.yesNo cfg.python.virtualenvAutoSwitch }' ''} ${optionalString (cfg.python.virtualenvInitialize != null) '' zstyle ':prezto:module:python:virtualenv' initialize '${ - if cfg.python.virtualenvInitialize then "yes" else "no" + lib.hm.booleans.yesNo cfg.python.virtualenvInitialize }' ''} ${optionalString (cfg.ruby.chrubyAutoSwitch != null) '' zstyle ':prezto:module:ruby:chruby' auto-switch '${ - if cfg.ruby.chrubyAutoSwitch then "yes" else "no" + lib.hm.booleans.yesNo cfg.ruby.chrubyAutoSwitch }' ''} ${optionalString (cfg.screen.autoStartLocal != null) '' zstyle ':prezto:module:screen:auto-start' local '${ - if cfg.screen.autoStartLocal then "yes" else "no" + lib.hm.booleans.yesNo cfg.screen.autoStartLocal }' ''} ${optionalString (cfg.screen.autoStartRemote != null) '' zstyle ':prezto:module:screen:auto-start' remote '${ - if cfg.screen.autoStartRemote then "yes" else "no" + lib.hm.booleans.yesNo cfg.screen.autoStartRemote }' ''} ${optionalString (cfg.ssh.identities != [ ]) '' @@ -502,7 +502,7 @@ in { ''} ${optionalString (cfg.terminal.autoTitle != null) '' zstyle ':prezto:module:terminal' auto-title '${ - if cfg.terminal.autoTitle then "yes" else "no" + lib.hm.booleans.yesNo cfg.terminal.autoTitle }' ''} ${optionalString (cfg.terminal.windowTitleFormat != null) '' @@ -516,17 +516,17 @@ in { ''} ${optionalString (cfg.tmux.autoStartLocal != null) '' zstyle ':prezto:module:tmux:auto-start' local '${ - if cfg.tmux.autoStartLocal then "yes" else "no" + lib.hm.booleans.yesNo cfg.tmux.autoStartLocal }' ''} ${optionalString (cfg.tmux.autoStartRemote != null) '' zstyle ':prezto:module:tmux:auto-start' remote '${ - if cfg.tmux.autoStartRemote then "yes" else "no" + lib.hm.booleans.yesNo cfg.tmux.autoStartRemote }' ''} ${optionalString (cfg.tmux.itermIntegration != null) '' zstyle ':prezto:module:tmux:iterm' integrate '${ - if cfg.tmux.itermIntegration then "yes" else "no" + lib.hm.booleans.yesNo cfg.tmux.itermIntegration }' ''} ${optionalString (cfg.tmux.defaultSessionName != null) '' @@ -534,7 +534,7 @@ in { ''} ${optionalString (cfg.utility.safeOps != null) '' zstyle ':prezto:module:utility' safe-ops '${ - if cfg.utility.safeOps then "yes" else "no" + lib.hm.booleans.yesNo cfg.utility.safeOps }' ''} ${cfg.extraConfig} diff --git a/modules/services/dunst.nix b/modules/services/dunst.nix index 238abc297..083ff2b3a 100644 --- a/modules/services/dunst.nix +++ b/modules/services/dunst.nix @@ -13,7 +13,7 @@ let mkKeyValue = key: value: let value' = if isBool value then - (if value then "yes" else "no") + (lib.hm.booleans.yesNo value) else if isString value then ''"${value}"'' else diff --git a/modules/services/window-managers/i3-sway/i3.nix b/modules/services/window-managers/i3-sway/i3.nix index 2cb48d9b0..16e82d163 100644 --- a/modules/services/window-managers/i3-sway/i3.nix +++ b/modules/services/window-managers/i3-sway/i3.nix @@ -161,13 +161,13 @@ let "floating_modifier ${floating.modifier}" (windowBorderString window floating) "hide_edge_borders ${window.hideEdgeBorders}" - "force_focus_wrapping ${if focus.forceWrapping then "yes" else "no"}" - "focus_follows_mouse ${if focus.followMouse then "yes" else "no"}" + "force_focus_wrapping ${lib.hm.booleans.yesNo focus.forceWrapping}" + "focus_follows_mouse ${lib.hm.booleans.yesNo focus.followMouse}" "focus_on_window_activation ${focus.newWindow}" "mouse_warping ${if focus.mouseWarping then "output" else "none"}" "workspace_layout ${workspaceLayout}" "workspace_auto_back_and_forth ${ - if workspaceAutoBackAndForth then "yes" else "no" + lib.hm.booleans.yesNo workspaceAutoBackAndForth }" "client.focused ${colorSetStr colors.focused}" "client.focused_inactive ${colorSetStr colors.focusedInactive}" diff --git a/modules/services/window-managers/i3-sway/lib/functions.nix b/modules/services/window-managers/i3-sway/lib/functions.nix index e1b90046e..0092280e4 100644 --- a/modules/services/window-managers/i3-sway/lib/functions.nix +++ b/modules/services/window-managers/i3-sway/lib/functions.nix @@ -87,10 +87,10 @@ rec { "status_command ${statusCommand}") "${moduleName}bar_command ${command}" (optionalString (workspaceButtons != null) - "workspace_buttons ${if workspaceButtons then "yes" else "no"}") + "workspace_buttons ${lib.hm.booleans.yesNo workspaceButtons}") (optionalString (workspaceNumbers != null) "strip_workspace_numbers ${ - if !workspaceNumbers then "yes" else "no" + lib.hm.booleans.yesNo (!workspaceNumbers) }") (optionalString (trayOutput != null) "tray_output ${trayOutput}") (optionals colorsNotNull (indent diff --git a/modules/services/window-managers/i3-sway/lib/options.nix b/modules/services/window-managers/i3-sway/lib/options.nix index c4d1535a7..7027e21d0 100644 --- a/modules/services/window-managers/i3-sway/lib/options.nix +++ b/modules/services/window-managers/i3-sway/lib/options.nix @@ -494,10 +494,7 @@ in { default = if isSway then "yes" else true; description = "Whether focus should follow the mouse."; apply = val: - if (isSway && isBool val) then - (if val then "yes" else "no") - else - val; + if (isSway && isBool val) then (lib.hm.booleans.yesNo val) else val; }; forceWrapping = mkOption { diff --git a/modules/services/window-managers/i3-sway/sway.nix b/modules/services/window-managers/i3-sway/sway.nix index 1c565d017..a3f119ad3 100644 --- a/modules/services/window-managers/i3-sway/sway.nix +++ b/modules/services/window-managers/i3-sway/sway.nix @@ -270,13 +270,13 @@ let "floating_modifier ${floating.modifier}" (windowBorderString window floating) "hide_edge_borders ${window.hideEdgeBorders}" - "focus_wrapping ${if focus.forceWrapping then "yes" else "no"}" + "focus_wrapping ${lib.hm.booleans.yesNo focus.forceWrapping}" "focus_follows_mouse ${focus.followMouse}" "focus_on_window_activation ${focus.newWindow}" "mouse_warping ${if focus.mouseWarping then "output" else "none"}" "workspace_layout ${workspaceLayout}" "workspace_auto_back_and_forth ${ - if workspaceAutoBackAndForth then "yes" else "no" + lib.hm.booleans.yesNo workspaceAutoBackAndForth }" "client.focused ${colorSetStr colors.focused}" "client.focused_inactive ${colorSetStr colors.focusedInactive}"