lib.booleans: add yesNo function

Closes #2772
This commit is contained in:
Sumner Evans 2022-03-21 17:50:43 -06:00
parent 80b4360678
commit 55e2a4d341
No known key found for this signature in database
GPG key ID: 8904527AB50022FD
17 changed files with 51 additions and 56 deletions

5
modules/lib/booleans.nix Normal file
View file

@ -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";
}

View file

@ -18,6 +18,7 @@ rec {
assertions = import ./assertions.nix { inherit lib; }; assertions = import ./assertions.nix { inherit lib; };
booleans = import ./booleans.nix { inherit lib; };
gvariant = import ./gvariant.nix { inherit lib; }; gvariant = import ./gvariant.nix { inherit lib; };
maintainers = import ./maintainers.nix; maintainers = import ./maintainers.nix;
strings = import ./strings.nix { inherit lib; }; strings = import ./strings.nix { inherit lib; };

View file

@ -6,7 +6,6 @@ let
cfg = config.programs.irssi; cfg = config.programs.irssi;
boolStr = b: if b then "yes" else "no";
quoteStr = s: escape [ ''"'' ] s; quoteStr = s: escape [ ''"'' ] s;
# Comma followed by newline. # Comma followed by newline.
@ -33,9 +32,9 @@ let
chatnet = "${k}"; chatnet = "${k}";
address = "${v.server.address}"; address = "${v.server.address}";
port = "${toString v.server.port}"; port = "${toString v.server.port}";
use_ssl = "${boolStr v.server.ssl.enable}"; use_ssl = "${lib.hm.booleans.yesNo v.server.ssl.enable}";
ssl_verify = "${boolStr v.server.ssl.verify}"; ssl_verify = "${lib.hm.booleans.yesNo v.server.ssl.verify}";
autoconnect = "${boolStr v.server.autoConnect}"; autoconnect = "${lib.hm.booleans.yesNo v.server.autoConnect}";
${ ${
lib.optionalString (v.server.ssl.certificateFile != null) '' lib.optionalString (v.server.ssl.certificateFile != null) ''
ssl_cert = "${v.server.ssl.certificateFile}"; ssl_cert = "${v.server.ssl.certificateFile}";
@ -50,7 +49,7 @@ let
{ {
chatnet = "${k}"; chatnet = "${k}";
name = "${c}"; name = "${c}";
autojoin = "${boolStr cv.autoJoin}"; autojoin = "${lib.hm.booleans.yesNo cv.autoJoin}";
} }
''))))); '')))));

View file

@ -14,10 +14,8 @@ let
toKittyConfig = generators.toKeyValue { toKittyConfig = generators.toKeyValue {
mkKeyValue = key: value: mkKeyValue = key: value:
let let
value' = if isBool value then value' =
(if value then "yes" else "no") (if isBool value then lib.hm.booleans.yesNo else toString) value;
else
toString value;
in "${key} ${value'}"; in "${key} ${value'}";
}; };

View file

@ -63,7 +63,7 @@ let
if isList v then if isList v then
concatMapStringsSep " " (genValue n) v concatMapStringsSep " " (genValue n) v
else if isBool v then else if isBool v then
(if v then "yes" else "no") lib.hm.booleans.yesNo v
else if isInt v then else if isInt v then
toString v toString v
else if isString v && hasSpace v then else if isString v && hasSpace v then

View file

@ -18,9 +18,7 @@ let
rec { rec {
int = toString option; int = toString option;
float = int; float = int;
bool = lib.hm.booleans.yesNo option;
bool = if option then "yes" else "no";
string = option; string = option;
}.${typeOf option}; }.${typeOf option};

View file

@ -14,7 +14,7 @@ let
renderValue = option: renderValue = option:
{ {
int = toString option; int = toString option;
bool = if option then "yes" else "no"; bool = lib.hm.booleans.yesNo option;
string = option; string = option;
}.${builtins.typeOf option}; }.${builtins.typeOf option};

View file

@ -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}"; setOption = n: v: if v == null then "unset ${n}" else "set ${n}=${v}";
escape = replaceStrings [ "%" ] [ "%25" ]; escape = replaceStrings [ "%" ] [ "%25" ];
@ -167,7 +166,7 @@ let
sidebarSection = '' sidebarSection = ''
# Sidebar # Sidebar
set sidebar_visible = yes 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_width = ${toString cfg.sidebar.width}
set sidebar_format = '${cfg.sidebar.format}' set sidebar_format = '${cfg.sidebar.format}'
''; '';
@ -204,8 +203,10 @@ let
# GPG section # GPG section
set crypt_use_gpgme = yes set crypt_use_gpgme = yes
set crypt_autosign = ${yesno (gpg.signByDefault or false)} set crypt_autosign = ${lib.hm.booleans.yesNo (gpg.signByDefault or false)}
set crypt_opportunistic_encrypt = ${yesno (gpg.encryptByDefault or false)} set crypt_opportunistic_encrypt = ${
lib.hm.booleans.yesNo (gpg.encryptByDefault or false)
}
set pgp_use_gpg_agent = yes set pgp_use_gpg_agent = yes
set mbox_type = ${if maildir != null then "Maildir" else "mbox"} set mbox_type = ${if maildir != null then "Maildir" else "mbox"}
set sort = "${cfg.sort}" set sort = "${cfg.sort}"

View file

@ -24,7 +24,7 @@ let
max-items ${toString cfg.maxItems} max-items ${toString cfg.maxItems}
browser ${cfg.browser} browser ${cfg.browser}
reload-threads ${toString cfg.reloadThreads} 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) ${optionalString (cfg.reloadTime != null)
(toString "reload-time ${toString cfg.reloadTime}")} (toString "reload-time ${toString cfg.reloadTime}")}
prepopulate-query-feeds yes prepopulate-query-feeds yes

View file

@ -12,10 +12,8 @@ let
toIni = generators.toINI { toIni = generators.toINI {
mkKeyValue = key: value: mkKeyValue = key: value:
let let
value' = if isBool value then value' =
(if value then "yes" else "no") (if isBool value then lib.hm.booleans.yesNo else toString) value;
else
toString value;
in "${key} = ${value'}"; in "${key} = ${value'}";
}; };

View file

@ -13,8 +13,6 @@ let
then " ${entry.address}" then " ${entry.address}"
else " [${entry.address}]:${toString entry.port}"; else " [${entry.address}]:${toString entry.port}";
yn = flag: if flag then "yes" else "no";
unwords = builtins.concatStringsSep " "; unwords = builtins.concatStringsSep " ";
bindOptions = { bindOptions = {
@ -284,7 +282,7 @@ let
matchBlockStr = cf: concatStringsSep "\n" ( matchBlockStr = cf: concatStringsSep "\n" (
["Host ${cf.host}"] ["Host ${cf.host}"]
++ optional (cf.port != null) " Port ${toString cf.port}" ++ 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.forwardX11 " ForwardX11 yes"
++ optional cf.forwardX11Trusted " ForwardX11Trusted yes" ++ optional cf.forwardX11Trusted " ForwardX11Trusted yes"
++ optional cf.identitiesOnly " IdentitiesOnly yes" ++ optional cf.identitiesOnly " IdentitiesOnly yes"
@ -296,7 +294,7 @@ let
" ServerAliveInterval ${toString cf.serverAliveInterval}" " ServerAliveInterval ${toString cf.serverAliveInterval}"
++ optional (cf.serverAliveCountMax != 3) ++ optional (cf.serverAliveCountMax != 3)
" ServerAliveCountMax ${toString cf.serverAliveCountMax}" " 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.checkHostIP) " CheckHostIP no"
++ optional (cf.proxyCommand != null) " ProxyCommand ${cf.proxyCommand}" ++ optional (cf.proxyCommand != null) " ProxyCommand ${cf.proxyCommand}"
++ optional (cf.proxyJump != null) " ProxyJump ${cf.proxyJump}" ++ optional (cf.proxyJump != null) " ProxyJump ${cf.proxyJump}"
@ -498,11 +496,11 @@ in
)} )}
Host * Host *
ForwardAgent ${yn cfg.forwardAgent} ForwardAgent ${lib.hm.booleans.yesNo cfg.forwardAgent}
Compression ${yn cfg.compression} Compression ${lib.hm.booleans.yesNo cfg.compression}
ServerAliveInterval ${toString cfg.serverAliveInterval} ServerAliveInterval ${toString cfg.serverAliveInterval}
ServerAliveCountMax ${toString cfg.serverAliveCountMax} ServerAliveCountMax ${toString cfg.serverAliveCountMax}
HashKnownHosts ${yn cfg.hashKnownHosts} HashKnownHosts ${lib.hm.booleans.yesNo cfg.hashKnownHosts}
UserKnownHostsFile ${cfg.userKnownHostsFile} UserKnownHostsFile ${cfg.userKnownHostsFile}
ControlMaster ${cfg.controlMaster} ControlMaster ${cfg.controlMaster}
ControlPath ${cfg.controlPath} ControlPath ${cfg.controlPath}

View file

@ -365,11 +365,11 @@ in {
# Generated by Nix # Generated by Nix
${optionalString (cfg.caseSensitive != null) '' ${optionalString (cfg.caseSensitive != null) ''
zstyle ':prezto:*:*' case-sensitive '${ zstyle ':prezto:*:*' case-sensitive '${
if cfg.caseSensitive then "yes" else "no" lib.hm.booleans.yesNo cfg.caseSensitive
}' }'
''} ''}
${optionalString (cfg.color != null) '' ${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 != [ ]) '' ${optionalString (cfg.pmoduleDirs != [ ]) ''
zstyle ':prezto:load' pmodule-dirs ${ zstyle ':prezto:load' pmodule-dirs ${
@ -410,12 +410,12 @@ in {
''} ''}
${optionalString (cfg.editor.dotExpansion != null) '' ${optionalString (cfg.editor.dotExpansion != null) ''
zstyle ':prezto:module:editor' dot-expansion '${ 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) '' ${optionalString (cfg.editor.promptContext != null) ''
zstyle ':prezto:module:editor' ps-context '${ 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) '' ${optionalString (cfg.git.submoduleIgnore != null) ''
@ -447,27 +447,27 @@ in {
''} ''}
${optionalString (cfg.python.virtualenvAutoSwitch != null) '' ${optionalString (cfg.python.virtualenvAutoSwitch != null) ''
zstyle ':prezto:module:python:virtualenv' auto-switch '${ 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) '' ${optionalString (cfg.python.virtualenvInitialize != null) ''
zstyle ':prezto:module:python:virtualenv' initialize '${ 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) '' ${optionalString (cfg.ruby.chrubyAutoSwitch != null) ''
zstyle ':prezto:module:ruby:chruby' auto-switch '${ 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) '' ${optionalString (cfg.screen.autoStartLocal != null) ''
zstyle ':prezto:module:screen:auto-start' local '${ 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) '' ${optionalString (cfg.screen.autoStartRemote != null) ''
zstyle ':prezto:module:screen:auto-start' remote '${ 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 != [ ]) '' ${optionalString (cfg.ssh.identities != [ ]) ''
@ -502,7 +502,7 @@ in {
''} ''}
${optionalString (cfg.terminal.autoTitle != null) '' ${optionalString (cfg.terminal.autoTitle != null) ''
zstyle ':prezto:module:terminal' auto-title '${ 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) '' ${optionalString (cfg.terminal.windowTitleFormat != null) ''
@ -516,17 +516,17 @@ in {
''} ''}
${optionalString (cfg.tmux.autoStartLocal != null) '' ${optionalString (cfg.tmux.autoStartLocal != null) ''
zstyle ':prezto:module:tmux:auto-start' local '${ 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) '' ${optionalString (cfg.tmux.autoStartRemote != null) ''
zstyle ':prezto:module:tmux:auto-start' remote '${ 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) '' ${optionalString (cfg.tmux.itermIntegration != null) ''
zstyle ':prezto:module:tmux:iterm' integrate '${ 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) '' ${optionalString (cfg.tmux.defaultSessionName != null) ''
@ -534,7 +534,7 @@ in {
''} ''}
${optionalString (cfg.utility.safeOps != null) '' ${optionalString (cfg.utility.safeOps != null) ''
zstyle ':prezto:module:utility' safe-ops '${ zstyle ':prezto:module:utility' safe-ops '${
if cfg.utility.safeOps then "yes" else "no" lib.hm.booleans.yesNo cfg.utility.safeOps
}' }'
''} ''}
${cfg.extraConfig} ${cfg.extraConfig}

View file

@ -13,7 +13,7 @@ let
mkKeyValue = key: value: mkKeyValue = key: value:
let let
value' = if isBool value then value' = if isBool value then
(if value then "yes" else "no") (lib.hm.booleans.yesNo value)
else if isString value then else if isString value then
''"${value}"'' ''"${value}"''
else else

View file

@ -161,13 +161,13 @@ let
"floating_modifier ${floating.modifier}" "floating_modifier ${floating.modifier}"
(windowBorderString window floating) (windowBorderString window floating)
"hide_edge_borders ${window.hideEdgeBorders}" "hide_edge_borders ${window.hideEdgeBorders}"
"force_focus_wrapping ${if focus.forceWrapping then "yes" else "no"}" "force_focus_wrapping ${lib.hm.booleans.yesNo focus.forceWrapping}"
"focus_follows_mouse ${if focus.followMouse then "yes" else "no"}" "focus_follows_mouse ${lib.hm.booleans.yesNo focus.followMouse}"
"focus_on_window_activation ${focus.newWindow}" "focus_on_window_activation ${focus.newWindow}"
"mouse_warping ${if focus.mouseWarping then "output" else "none"}" "mouse_warping ${if focus.mouseWarping then "output" else "none"}"
"workspace_layout ${workspaceLayout}" "workspace_layout ${workspaceLayout}"
"workspace_auto_back_and_forth ${ "workspace_auto_back_and_forth ${
if workspaceAutoBackAndForth then "yes" else "no" lib.hm.booleans.yesNo workspaceAutoBackAndForth
}" }"
"client.focused ${colorSetStr colors.focused}" "client.focused ${colorSetStr colors.focused}"
"client.focused_inactive ${colorSetStr colors.focusedInactive}" "client.focused_inactive ${colorSetStr colors.focusedInactive}"

View file

@ -87,10 +87,10 @@ rec {
"status_command ${statusCommand}") "status_command ${statusCommand}")
"${moduleName}bar_command ${command}" "${moduleName}bar_command ${command}"
(optionalString (workspaceButtons != null) (optionalString (workspaceButtons != null)
"workspace_buttons ${if workspaceButtons then "yes" else "no"}") "workspace_buttons ${lib.hm.booleans.yesNo workspaceButtons}")
(optionalString (workspaceNumbers != null) (optionalString (workspaceNumbers != null)
"strip_workspace_numbers ${ "strip_workspace_numbers ${
if !workspaceNumbers then "yes" else "no" lib.hm.booleans.yesNo (!workspaceNumbers)
}") }")
(optionalString (trayOutput != null) "tray_output ${trayOutput}") (optionalString (trayOutput != null) "tray_output ${trayOutput}")
(optionals colorsNotNull (indent (optionals colorsNotNull (indent

View file

@ -494,10 +494,7 @@ in {
default = if isSway then "yes" else true; default = if isSway then "yes" else true;
description = "Whether focus should follow the mouse."; description = "Whether focus should follow the mouse.";
apply = val: apply = val:
if (isSway && isBool val) then if (isSway && isBool val) then (lib.hm.booleans.yesNo val) else val;
(if val then "yes" else "no")
else
val;
}; };
forceWrapping = mkOption { forceWrapping = mkOption {

View file

@ -270,13 +270,13 @@ let
"floating_modifier ${floating.modifier}" "floating_modifier ${floating.modifier}"
(windowBorderString window floating) (windowBorderString window floating)
"hide_edge_borders ${window.hideEdgeBorders}" "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_follows_mouse ${focus.followMouse}"
"focus_on_window_activation ${focus.newWindow}" "focus_on_window_activation ${focus.newWindow}"
"mouse_warping ${if focus.mouseWarping then "output" else "none"}" "mouse_warping ${if focus.mouseWarping then "output" else "none"}"
"workspace_layout ${workspaceLayout}" "workspace_layout ${workspaceLayout}"
"workspace_auto_back_and_forth ${ "workspace_auto_back_and_forth ${
if workspaceAutoBackAndForth then "yes" else "no" lib.hm.booleans.yesNo workspaceAutoBackAndForth
}" }"
"client.focused ${colorSetStr colors.focused}" "client.focused ${colorSetStr colors.focused}"
"client.focused_inactive ${colorSetStr colors.focusedInactive}" "client.focused_inactive ${colorSetStr colors.focusedInactive}"