treewide: use optional instead of 'then []'

This commit is contained in:
Felix Buehler
2023-06-25 11:47:43 +02:00
committed by Anderson Torres
parent 175e9d1f00
commit 933a41a73f
17 changed files with 45 additions and 65 deletions

View File

@@ -22,12 +22,11 @@ with lib;
config = mkIf config.security.lockKernelModules { config = mkIf config.security.lockKernelModules {
boot.kernelModules = concatMap (x: boot.kernelModules = concatMap (x:
if x.device != null optionals (x.device != null) (
then
if x.fsType == "vfat" if x.fsType == "vfat"
then [ "vfat" "nls-cp437" "nls-iso8859-1" ] then [ "vfat" "nls-cp437" "nls-iso8859-1" ]
else [ x.fsType ] else [ x.fsType ])
else []) config.system.build.fileSystems; ) config.system.build.fileSystems;
systemd.services.disable-kernel-module-loading = { systemd.services.disable-kernel-module-loading = {
description = "Disable kernel module loading"; description = "Disable kernel module loading";

View File

@@ -216,10 +216,10 @@ in
${concatStringsSep "\n" ( ${concatStringsSep "\n" (
lists.flatten ( lists.flatten (
map ( map (
rule: if (length rule.commands != 0) then [ rule: optionals (length rule.commands != 0) [
(map (user: "${toUserString user} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.users) (map (user: "${toUserString user} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.users)
(map (group: "${toGroupString group} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.groups) (map (group: "${toGroupString group} ${rule.host}=(${rule.runAs}) ${toCommandsString rule.commands}") rule.groups)
] else [] ]
) cfg.extraRules ) cfg.extraRules
) )
)} )}

View File

@@ -298,7 +298,7 @@ in
let let
extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions; extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
resticCmd = "${backup.package}/bin/restic${extraOptions}"; resticCmd = "${backup.package}/bin/restic${extraOptions}";
excludeFlags = if (backup.exclude != []) then ["--exclude-file=${pkgs.writeText "exclude-patterns" (concatStringsSep "\n" backup.exclude)}"] else []; excludeFlags = optional (backup.exclude != []) "--exclude-file=${pkgs.writeText "exclude-patterns" (concatStringsSep "\n" backup.exclude)}";
filesFromTmpFile = "/run/restic-backups-${name}/includes"; filesFromTmpFile = "/run/restic-backups-${name}/includes";
backupPaths = backupPaths =
if (backup.dynamicFilesFrom == null) if (backup.dynamicFilesFrom == null)

View File

@@ -294,6 +294,6 @@ in
}; };
}; };
networking.firewall.allowedUDPPorts = if cfg.openFirewall then [ cfg.port ] else []; networking.firewall.allowedUDPPorts = optional cfg.openFirewall cfg.port;
}; };
} }

View File

@@ -16,7 +16,7 @@ let
generate = name: value: generate = name: value:
let mkParam = k: v: let mkParam = k: v:
if v == null then [] if v == null then []
else if isBool v then if v then [("--"+k)] else [] else if isBool v then optional v ("--"+k)
else [("--"+k) v]; else [("--"+k) v];
mkParams = k: v: map (mkParam k) (if isList v then v else [v]); mkParams = k: v: map (mkParam k) (if isList v then v else [v]);
in escapeShellArgs (concatLists (concatLists (mapAttrsToList mkParams value))); in escapeShellArgs (concatLists (concatLists (mapAttrsToList mkParams value)));

View File

@@ -15,11 +15,11 @@ let
rm -f /var/lib/glusterd/secure-access rm -f /var/lib/glusterd/secure-access
''; '';
restartTriggers = if (cfg.tlsSettings != null) then [ restartTriggers = optionals (cfg.tlsSettings != null) [
config.environment.etc."ssl/glusterfs.pem".source config.environment.etc."ssl/glusterfs.pem".source
config.environment.etc."ssl/glusterfs.key".source config.environment.etc."ssl/glusterfs.key".source
config.environment.etc."ssl/glusterfs.ca".source config.environment.etc."ssl/glusterfs.ca".source
] else []; ];
cfg = config.services.glusterfs; cfg = config.services.glusterfs;

View File

@@ -48,7 +48,7 @@ with lib;
(mkChangedOptionModule [ "services" "ddclient" "domain" ] [ "services" "ddclient" "domains" ] (mkChangedOptionModule [ "services" "ddclient" "domain" ] [ "services" "ddclient" "domains" ]
(config: (config:
let value = getAttrFromPath [ "services" "ddclient" "domain" ] config; let value = getAttrFromPath [ "services" "ddclient" "domain" ] config;
in if value != "" then [ value ] else [])) in optional (value != "") value))
(mkRemovedOptionModule [ "services" "ddclient" "homeDir" ] "") (mkRemovedOptionModule [ "services" "ddclient" "homeDir" ] "")
(mkRemovedOptionModule [ "services" "ddclient" "password" ] "Use services.ddclient.passwordFile instead.") (mkRemovedOptionModule [ "services" "ddclient" "password" ] "Use services.ddclient.passwordFile instead.")
(mkRemovedOptionModule [ "services" "ddclient" "ipv6" ] "") (mkRemovedOptionModule [ "services" "ddclient" "ipv6" ] "")

View File

@@ -169,15 +169,15 @@ let
(boolOpt "enabled" proto.enable) (boolOpt "enabled" proto.enable)
(strOpt "address" proto.address) (strOpt "address" proto.address)
(intOpt "port" proto.port) (intOpt "port" proto.port)
] ++ (if proto ? keys then optionalNullString "keys" proto.keys else []) ] ++ (optionals (proto ? keys) (optionalNullString "keys" proto.keys))
++ (if proto ? auth then optionalNullBool "auth" proto.auth else []) ++ (optionals (proto ? auth) (optionalNullBool "auth" proto.auth))
++ (if proto ? user then optionalNullString "user" proto.user else []) ++ (optionals (proto ? user) (optionalNullString "user" proto.user))
++ (if proto ? pass then optionalNullString "pass" proto.pass else []) ++ (optionals (proto ? pass) (optionalNullString "pass" proto.pass))
++ (if proto ? strictHeaders then optionalNullBool "strictheaders" proto.strictHeaders else []) ++ (optionals (proto ? strictHeaders) (optionalNullBool "strictheaders" proto.strictHeaders))
++ (if proto ? hostname then optionalNullString "hostname" proto.hostname else []) ++ (optionals (proto ? hostname) (optionalNullString "hostname" proto.hostname))
++ (if proto ? outproxy then optionalNullString "outproxy" proto.outproxy else []) ++ (optionals (proto ? outproxy) (optionalNullString "outproxy" proto.outproxy))
++ (if proto ? outproxyPort then optionalNullInt "outproxyport" proto.outproxyPort else []) ++ (optionals (proto ? outproxyPort) (optionalNullInt "outproxyport" proto.outproxyPort))
++ (if proto ? outproxyEnable then optionalNullBool "outproxy.enabled" proto.outproxyEnable else []); ++ (optionals (proto ? outproxyEnable) (optionalNullBool "outproxy.enabled" proto.outproxyEnable));
in (concatStringsSep "\n" protoOpts) in (concatStringsSep "\n" protoOpts)
)); ));
in in
@@ -192,21 +192,14 @@ let
"type = client" "type = client"
(intOpt "port" tun.port) (intOpt "port" tun.port)
(strOpt "destination" tun.destination) (strOpt "destination" tun.destination)
] ++ (if tun ? destinationPort then optionalNullInt "destinationport" tun.destinationPort else []) ] ++ (optionals (tun ? destinationPort) (optionalNullInt "destinationport" tun.destinationPort))
++ (if tun ? keys then ++ (optionals (tun ? keys) (optionalNullString "keys" tun.keys))
optionalNullString "keys" tun.keys else []) ++ (optionals (tun ? address) (optionalNullString "address" tun.address))
++ (if tun ? address then ++ (optionals (tun ? inbound.length) (optionalNullInt "inbound.length" tun.inbound.length))
optionalNullString "address" tun.address else []) ++ (optionals (tun ? inbound.quantity) (optionalNullInt "inbound.quantity" tun.inbound.quantity))
++ (if tun ? inbound.length then ++ (optionals (tun ? outbound.length) (optionalNullInt "outbound.length" tun.outbound.length))
optionalNullInt "inbound.length" tun.inbound.length else []) ++ (optionals (tun ? outbound.quantity) (optionalNullInt "outbound.quantity" tun.outbound.quantity))
++ (if tun ? inbound.quantity then ++ (optionals (tun ? crypto.tagsToSend) (optionalNullInt "crypto.tagstosend" tun.crypto.tagsToSend));
optionalNullInt "inbound.quantity" tun.inbound.quantity else [])
++ (if tun ? outbound.length then
optionalNullInt "outbound.length" tun.outbound.length else [])
++ (if tun ? outbound.quantity then
optionalNullInt "outbound.quantity" tun.outbound.quantity else [])
++ (if tun ? crypto.tagsToSend then
optionalNullInt "crypto.tagstosend" tun.crypto.tagsToSend else []);
in concatStringsSep "\n" outTunOpts)) in concatStringsSep "\n" outTunOpts))
(flip map (flip map
(collect (tun: tun ? port && tun ? address) cfg.inTunnels) (collect (tun: tun ? port && tun ? address) cfg.inTunnels)
@@ -215,14 +208,10 @@ let
"type = server" "type = server"
(intOpt "port" tun.port) (intOpt "port" tun.port)
(strOpt "host" tun.address) (strOpt "host" tun.address)
] ++ (if tun ? destination then ] ++ (optionals (tun ? destination) (optionalNullString "destination" tun.destination))
optionalNullString "destination" tun.destination else []) ++ (optionals (tun ? keys) (optionalNullString "keys" tun.keys))
++ (if tun ? keys then ++ (optionals (tun ? inPort) (optionalNullInt "inport" tun.inPort))
optionalNullString "keys" tun.keys else []) ++ (optionals (tun ? accessList) (optionalEmptyList "accesslist" tun.accessList));
++ (if tun ? inPort then
optionalNullInt "inport" tun.inPort else [])
++ (if tun ? accessList then
optionalEmptyList "accesslist" tun.accessList else []);
in concatStringsSep "\n" inTunOpts))]; in concatStringsSep "\n" inTunOpts))];
in pkgs.writeText "i2pd-tunnels.conf" opts; in pkgs.writeText "i2pd-tunnels.conf" opts;

View File

@@ -757,9 +757,8 @@ in
environment.etc."prosody/prosody.cfg.lua".text = environment.etc."prosody/prosody.cfg.lua".text =
let let
httpDiscoItems = if (cfg.uploadHttp != null) httpDiscoItems = optionals (cfg.uploadHttp != null)
then [{ url = cfg.uploadHttp.domain; description = "HTTP upload endpoint";}] [{ url = cfg.uploadHttp.domain; description = "HTTP upload endpoint";}];
else [];
mucDiscoItems = builtins.foldl' mucDiscoItems = builtins.foldl'
(acc: muc: [{ url = muc.domain; description = "${muc.domain} MUC endpoint";}] ++ acc) (acc: muc: [{ url = muc.domain; description = "${muc.domain} MUC endpoint";}] ++ acc)
[] []

View File

@@ -528,7 +528,7 @@ in
}; };
networking.firewall.allowedTCPPorts = if cfg.openFirewall then cfg.ports else []; networking.firewall.allowedTCPPorts = optionals cfg.openFirewall cfg.ports;
security.pam.services.sshd = security.pam.services.sshd =
{ startSession = true; { startSession = true;

View File

@@ -305,7 +305,7 @@ in
# If you really have to access root via FTP use mkOverride or userlistDeny # If you really have to access root via FTP use mkOverride or userlistDeny
# = false and whitelist root # = false and whitelist root
services.vsftpd.userlist = if cfg.userlistDeny then ["root"] else []; services.vsftpd.userlist = optional cfg.userlistDeny "root";
systemd = { systemd = {
tmpfiles.rules = optional cfg.anonymousUser tmpfiles.rules = optional cfg.anonymousUser

View File

@@ -769,7 +769,7 @@ in
}; };
options.SOCKSPort = mkOption { options.SOCKSPort = mkOption {
description = lib.mdDoc (descriptionGeneric "SOCKSPort"); description = lib.mdDoc (descriptionGeneric "SOCKSPort");
default = if cfg.settings.HiddenServiceNonAnonymousMode == true then [{port = 0;}] else []; default = lib.optionals cfg.settings.HiddenServiceNonAnonymousMode [{port = 0;}];
defaultText = literalExpression '' defaultText = literalExpression ''
if config.${opt.settings}.HiddenServiceNonAnonymousMode == true if config.${opt.settings}.HiddenServiceNonAnonymousMode == true
then [ { port = 0; } ] then [ { port = 0; } ]
@@ -897,8 +897,7 @@ in
allowedTCPPorts = allowedTCPPorts =
concatMap (o: concatMap (o:
if isInt o && o > 0 then [o] if isInt o && o > 0 then [o]
else if o ? "port" && isInt o.port && o.port > 0 then [o.port] else optionals (o ? "port" && isInt o.port && o.port > 0) [o.port]
else []
) (flatten [ ) (flatten [
cfg.settings.ORPort cfg.settings.ORPort
cfg.settings.DirPort cfg.settings.DirPort

View File

@@ -6,7 +6,7 @@ let
cfg = config.services.dex; cfg = config.services.dex;
fixClient = client: if client ? secretFile then ((builtins.removeAttrs client [ "secretFile" ]) // { secret = client.secretFile; }) else client; fixClient = client: if client ? secretFile then ((builtins.removeAttrs client [ "secretFile" ]) // { secret = client.secretFile; }) else client;
filteredSettings = mapAttrs (n: v: if n == "staticClients" then (builtins.map fixClient v) else v) cfg.settings; filteredSettings = mapAttrs (n: v: if n == "staticClients" then (builtins.map fixClient v) else v) cfg.settings;
secretFiles = flatten (builtins.map (c: if c ? secretFile then [ c.secretFile ] else []) (cfg.settings.staticClients or [])); secretFiles = flatten (builtins.map (c: optional (c ? secretFile) c.secretFile) (cfg.settings.staticClients or []));
settingsFormat = pkgs.formats.yaml {}; settingsFormat = pkgs.formats.yaml {};
configFile = settingsFormat.generate "config.yaml" filteredSettings; configFile = settingsFormat.generate "config.yaml" filteredSettings;

View File

@@ -91,9 +91,7 @@ let
envFile = pkgs.writeText "mastodon.env" (lib.concatMapStrings (s: s + "\n") ( envFile = pkgs.writeText "mastodon.env" (lib.concatMapStrings (s: s + "\n") (
(lib.concatLists (lib.mapAttrsToList (name: value: (lib.concatLists (lib.mapAttrsToList (name: value:
if value != null then [ lib.optional (value != null) ''${name}="${toString value}"''
"${name}=\"${toString value}\""
] else []
) env)))); ) env))));
mastodonTootctl = let mastodonTootctl = let

View File

@@ -13,9 +13,7 @@ let
envFile = pkgs.writeText "nifi.env" (lib.concatMapStrings (s: s + "\n") ( envFile = pkgs.writeText "nifi.env" (lib.concatMapStrings (s: s + "\n") (
(lib.concatLists (lib.mapAttrsToList (name: value: (lib.concatLists (lib.mapAttrsToList (name: value:
if value != null then [ lib.optional (value != null) ''${name}="${toString value}"''
"${name}=\"${toString value}\""
] else []
) env)))); ) env))));
nifiEnv = pkgs.writeShellScriptBin "nifi-env" '' nifiEnv = pkgs.writeShellScriptBin "nifi-env" ''

View File

@@ -52,9 +52,7 @@ let
envFile = pkgs.writeText "peertube.env" (lib.concatMapStrings (s: s + "\n") ( envFile = pkgs.writeText "peertube.env" (lib.concatMapStrings (s: s + "\n") (
(lib.concatLists (lib.mapAttrsToList (name: value: (lib.concatLists (lib.mapAttrsToList (name: value:
if value != null then [ lib.optional (value != null) ''${name}="${toString value}"''
"${name}=\"${toString value}\""
] else []
) env)))); ) env))));
peertubeEnv = pkgs.writeShellScriptBin "peertube-env" '' peertubeEnv = pkgs.writeShellScriptBin "peertube-env" ''

View File

@@ -980,7 +980,7 @@ in
++ luks.cryptoModules ++ luks.cryptoModules
# workaround until https://marc.info/?l=linux-crypto-vger&m=148783562211457&w=4 is merged # workaround until https://marc.info/?l=linux-crypto-vger&m=148783562211457&w=4 is merged
# remove once 'modprobe --show-depends xts' shows ecb as a dependency # remove once 'modprobe --show-depends xts' shows ecb as a dependency
++ (if builtins.elem "xts" luks.cryptoModules then ["ecb"] else []); ++ (optional (builtins.elem "xts" luks.cryptoModules) "ecb");
# copy the cryptsetup binary and it's dependencies # copy the cryptsetup binary and it's dependencies
boot.initrd.extraUtilsCommands = let boot.initrd.extraUtilsCommands = let