0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

treewide: add defaultText for options with simple cfg.* expression defaults

adds defaultText for options with defaults that use only literals, full config.*
paths, and the cfg shortcut binding.
This commit is contained in:
pennae 2021-12-05 21:45:35 +01:00
parent fb0e5be843
commit ed673a69db
18 changed files with 103 additions and 19 deletions

View file

@ -1,9 +1,10 @@
{ config, pkgs, lib, ... }: { config, options, pkgs, lib, ... }:
with lib; with lib;
let let
cfg = config.services.kubernetes.addons.dashboard; cfg = config.services.kubernetes.addons.dashboard;
opt = options.services.kubernetes.addons.dashboard;
in { in {
imports = [ imports = [
(mkRenamedOptionModule [ "services" "kubernetes" "addons" "dashboard" "enableRBAC" ] [ "services" "kubernetes" "addons" "dashboard" "rbac" "enable" ]) (mkRenamedOptionModule [ "services" "kubernetes" "addons" "dashboard" "enableRBAC" ] [ "services" "kubernetes" "addons" "dashboard" "rbac" "enable" ])
@ -28,6 +29,9 @@ in {
description = "Whether to enable role based access control is enabled for kubernetes dashboard"; description = "Whether to enable role based access control is enabled for kubernetes dashboard";
type = types.bool; type = types.bool;
default = elem "RBAC" config.services.kubernetes.apiserver.authorizationMode; default = elem "RBAC" config.services.kubernetes.apiserver.authorizationMode;
defaultText = literalExpression ''
elem "RBAC" config.${options.services.kubernetes.apiserver.authorizationMode}
'';
}; };
clusterAdmin = mkOption { clusterAdmin = mkOption {
@ -54,6 +58,14 @@ in {
finalImageTag = cfg.version; finalImageTag = cfg.version;
sha256 = "01xrr4pwgr2hcjrjsi3d14ifpzdfbxzqpzxbk2fkbjb9zkv38zxy"; sha256 = "01xrr4pwgr2hcjrjsi3d14ifpzdfbxzqpzxbk2fkbjb9zkv38zxy";
}; };
defaultText = literalExpression ''
{
imageName = "k8s.gcr.io/kubernetes-dashboard-amd64";
imageDigest = "sha256:0ae6b69432e78069c5ce2bcde0fe409c5c4d6f0f4d9cd50a17974fea38898747";
finalImageTag = config.${opt.version};
sha256 = "01xrr4pwgr2hcjrjsi3d14ifpzdfbxzqpzxbk2fkbjb9zkv38zxy";
};
'';
}; };
}; };

View file

@ -310,6 +310,7 @@ in {
backup.path = mkOption { backup.path = mkOption {
type = types.str; type = types.str;
default = cfg.statePath + "/backup"; default = cfg.statePath + "/backup";
defaultText = literalExpression ''config.${opt.statePath} + "/backup"'';
description = "GitLab path for backups."; description = "GitLab path for backups.";
}; };

View file

@ -1,8 +1,9 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
pkg = pkgs.moonraker; pkg = pkgs.moonraker;
cfg = config.services.moonraker; cfg = config.services.moonraker;
opt = options.services.moonraker;
format = pkgs.formats.ini { format = pkgs.formats.ini {
# https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996 # https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996
listToValue = l: listToValue = l:
@ -30,6 +31,7 @@ in {
configDir = mkOption { configDir = mkOption {
type = types.path; type = types.path;
default = cfg.stateDir + "/config"; default = cfg.stateDir + "/config";
defaultText = literalExpression ''config.${opt.stateDir} + "/config"'';
description = '' description = ''
The directory containing client-writable configuration files. The directory containing client-writable configuration files.

View file

@ -1,7 +1,8 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
let let
cfg = config.services.parsedmarc; cfg = config.services.parsedmarc;
opt = options.services.parsedmarc;
ini = pkgs.formats.ini {}; ini = pkgs.formats.ini {};
in in
{ {
@ -80,6 +81,9 @@ in
datasource = lib.mkOption { datasource = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = cfg.provision.elasticsearch && config.services.grafana.enable; default = cfg.provision.elasticsearch && config.services.grafana.enable;
defaultText = lib.literalExpression ''
config.${opt.provision.elasticsearch} && config.${options.services.grafana.enable}
'';
apply = x: x && cfg.provision.elasticsearch; apply = x: x && cfg.provision.elasticsearch;
description = '' description = ''
Whether the automatically provisioned Elasticsearch Whether the automatically provisioned Elasticsearch

View file

@ -1,8 +1,9 @@
{ config, pkgs, lib, ... }: { config, options, pkgs, lib, ... }:
let let
inherit (lib) mkOption mkEnableOption mkIf mkMerge types optional; inherit (lib) literalExpression mkOption mkEnableOption mkIf mkMerge types optional;
cfg = config.services.uptime; cfg = config.services.uptime;
opt = options.services.uptime;
configDir = pkgs.runCommand "config" { preferLocalBuild = true; } configDir = pkgs.runCommand "config" { preferLocalBuild = true; }
(if cfg.configFile != null then '' (if cfg.configFile != null then ''
@ -52,7 +53,10 @@ in {
enableWebService = mkEnableOption "the uptime monitoring program web service"; enableWebService = mkEnableOption "the uptime monitoring program web service";
enableSeparateMonitoringService = mkEnableOption "the uptime monitoring service" // { default = cfg.enableWebService; }; enableSeparateMonitoringService = mkEnableOption "the uptime monitoring service" // {
default = cfg.enableWebService;
defaultText = literalExpression "config.${opt.enableWebService}";
};
nodeEnv = mkOption { nodeEnv = mkOption {
description = "The node environment to run in (development, production, etc.)"; description = "The node environment to run in (development, production, etc.)";

View file

@ -1,10 +1,11 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
cfg = config.networking.wireguard; cfg = config.networking.wireguard;
opt = options.networking.wireguard;
kernel = config.boot.kernelPackages; kernel = config.boot.kernelPackages;
@ -438,6 +439,7 @@ in
type = types.bool; type = types.bool;
# 2019-05-25: Backwards compatibility. # 2019-05-25: Backwards compatibility.
default = cfg.interfaces != {}; default = cfg.interfaces != {};
defaultText = literalExpression "config.${opt.interfaces} != { }";
example = true; example = true;
}; };

View file

@ -1,4 +1,4 @@
{ config, lib, pkgs, utils, ... }: { config, lib, options, pkgs, utils, ... }:
with lib; with lib;
@ -8,6 +8,7 @@ let
else pkgs.wpa_supplicant; else pkgs.wpa_supplicant;
cfg = config.networking.wireless; cfg = config.networking.wireless;
opt = options.networking.wireless;
# Content of wpa_supplicant.conf # Content of wpa_supplicant.conf
generatedConfig = concatStringsSep "\n" ( generatedConfig = concatStringsSep "\n" (
@ -421,6 +422,7 @@ in {
dbusControlled = mkOption { dbusControlled = mkOption {
type = types.bool; type = types.bool;
default = lib.length cfg.interfaces < 2; default = lib.length cfg.interfaces < 2;
defaultText = literalExpression "length config.${opt.interfaces} < 2";
description = '' description = ''
Whether to enable the DBus control interface. Whether to enable the DBus control interface.
This is only needed when using NetworkManager or connman. This is only needed when using NetworkManager or connman.

View file

@ -1,9 +1,10 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.kibana; cfg = config.services.kibana;
opt = options.services.kibana;
ge7 = builtins.compareVersions cfg.package.version "7" >= 0; ge7 = builtins.compareVersions cfg.package.version "7" >= 0;
lt6_6 = builtins.compareVersions cfg.package.version "6.6" < 0; lt6_6 = builtins.compareVersions cfg.package.version "6.6" < 0;
@ -130,6 +131,9 @@ in {
This defaults to the singleton list [ca] when the <option>ca</option> option is defined. This defaults to the singleton list [ca] when the <option>ca</option> option is defined.
''; '';
default = if cfg.elasticsearch.ca == null then [] else [ca]; default = if cfg.elasticsearch.ca == null then [] else [ca];
defaultText = literalExpression ''
if config.${opt.elasticsearch.ca} == null then [ ] else [ ca ]
'';
type = types.listOf types.path; type = types.listOf types.path;
}; };

View file

@ -1,10 +1,11 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with builtins; with builtins;
with lib; with lib;
let let
cfg = config.services.tor; cfg = config.services.tor;
opt = options.services.tor;
stateDir = "/var/lib/tor"; stateDir = "/var/lib/tor";
runDir = "/run/tor"; runDir = "/run/tor";
descriptionGeneric = option: '' descriptionGeneric = option: ''
@ -799,6 +800,11 @@ in
options.SOCKSPort = mkOption { options.SOCKSPort = mkOption {
description = descriptionGeneric "SOCKSPort"; description = descriptionGeneric "SOCKSPort";
default = if cfg.settings.HiddenServiceNonAnonymousMode == true then [{port = 0;}] else []; default = if cfg.settings.HiddenServiceNonAnonymousMode == true then [{port = 0;}] else [];
defaultText = literalExpression ''
if config.${opt.settings}.HiddenServiceNonAnonymousMode == true
then [ { port = 0; } ]
else [ ]
'';
example = [{port = 9090;}]; example = [{port = 9090;}];
type = types.listOf (optionSOCKSPort true); type = types.listOf (optionSOCKSPort true);
}; };

View file

@ -1,9 +1,10 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.vault; cfg = config.services.vault;
opt = options.services.vault;
configFile = pkgs.writeText "vault.hcl" '' configFile = pkgs.writeText "vault.hcl" ''
listener "tcp" { listener "tcp" {
@ -83,6 +84,11 @@ in
storagePath = mkOption { storagePath = mkOption {
type = types.nullOr types.path; type = types.nullOr types.path;
default = if cfg.storageBackend == "file" then "/var/lib/vault" else null; default = if cfg.storageBackend == "file" then "/var/lib/vault" else null;
defaultText = literalExpression ''
if config.${opt.storageBackend} == "file"
then "/var/lib/vault"
else null
'';
description = "Data directory for file backend"; description = "Data directory for file backend";
}; };

View file

@ -73,6 +73,7 @@ in
socketioPort = mkOption { socketioPort = mkOption {
type = types.port; type = types.port;
default = cfg.port + 1; default = cfg.port + 1;
defaultText = literalExpression "config.${opt.port} + 1";
description = '' description = ''
Socket.io port for EPGStation to listen on. Socket.io port for EPGStation to listen on.
''; '';

View file

@ -328,6 +328,7 @@ in
useSSL = lib.mkOption { useSSL = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = cfg.redis.host != "localhost"; default = cfg.redis.host != "localhost";
defaultText = lib.literalExpression ''config.${opt.redis.host} != "localhost"'';
description = '' description = ''
Connect to Redis with SSL. Connect to Redis with SSL.
''; '';

View file

@ -1,7 +1,8 @@
{ config, pkgs, lib, ... }: { config, options, pkgs, lib, ... }:
let let
cfg = config.services.keycloak; cfg = config.services.keycloak;
opt = options.services.keycloak;
in in
{ {
options.services.keycloak = { options.services.keycloak = {
@ -147,6 +148,7 @@ in
useSSL = lib.mkOption { useSSL = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = cfg.database.host != "localhost"; default = cfg.database.host != "localhost";
defaultText = lib.literalExpression ''config.${opt.database.host} != "localhost"'';
description = '' description = ''
Whether the database connection should be secured by SSL / Whether the database connection should be secured by SSL /
TLS. TLS.

View file

@ -154,6 +154,11 @@ in {
host = lib.mkOption { host = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = if cfg.database.createLocally then "/run/postgresql" else null; default = if cfg.database.createLocally then "/run/postgresql" else null;
defaultText = lib.literalExpression ''
if config.${opt.database.createLocally}
then "/run/postgresql"
else null
'';
example = "192.168.15.47"; example = "192.168.15.47";
description = "Database host address or unix socket."; description = "Database host address or unix socket.";
}; };
@ -194,12 +199,22 @@ in {
host = lib.mkOption { host = lib.mkOption {
type = lib.types.nullOr lib.types.str; type = lib.types.nullOr lib.types.str;
default = if cfg.redis.createLocally && !cfg.redis.enableUnixSocket then "127.0.0.1" else null; default = if cfg.redis.createLocally && !cfg.redis.enableUnixSocket then "127.0.0.1" else null;
defaultText = lib.literalExpression ''
if config.${opt.redis.createLocally} && !config.${opt.redis.enableUnixSocket}
then "127.0.0.1"
else null
'';
description = "Redis host."; description = "Redis host.";
}; };
port = lib.mkOption { port = lib.mkOption {
type = lib.types.nullOr lib.types.port; type = lib.types.nullOr lib.types.port;
default = if cfg.redis.createLocally && cfg.redis.enableUnixSocket then null else 6379; default = if cfg.redis.createLocally && cfg.redis.enableUnixSocket then null else 6379;
defaultText = lib.literalExpression ''
if config.${opt.redis.createLocally} && config.${opt.redis.enableUnixSocket}
then null
else 6379
'';
description = "Redis port."; description = "Redis port.";
}; };

View file

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
let let
@ -6,6 +6,7 @@ let
inherit (lib) literalExpression mapAttrs optionalString versionAtLeast; inherit (lib) literalExpression mapAttrs optionalString versionAtLeast;
cfg = config.services.zabbixWeb; cfg = config.services.zabbixWeb;
opt = options.services.zabbixWeb;
fpm = config.services.phpfpm.pools.zabbix; fpm = config.services.phpfpm.pools.zabbix;
user = "zabbix"; user = "zabbix";
@ -82,6 +83,11 @@ in
if cfg.database.type == "mysql" then config.services.mysql.port if cfg.database.type == "mysql" then config.services.mysql.port
else if cfg.database.type == "pgsql" then config.services.postgresql.port else if cfg.database.type == "pgsql" then config.services.postgresql.port
else 1521; else 1521;
defaultText = literalExpression ''
if config.${opt.database.type} == "mysql" then config.${options.services.mysql.port}
else if config.${opt.database.type} == "pgsql" then config.${options.services.postgresql.port}
else 1521
'';
description = "Database host port."; description = "Database host port.";
}; };

View file

@ -7,13 +7,14 @@
# (e.g., KDE, Gnome or a plain xterm), and optionally the *window # (e.g., KDE, Gnome or a plain xterm), and optionally the *window
# manager* (e.g. kwin or twm). # manager* (e.g. kwin or twm).
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.xserver; cfg = config.services.xserver;
opt = options.services.xserver;
xorg = pkgs.xorg; xorg = pkgs.xorg;
fontconfig = config.fonts.fontconfig; fontconfig = config.fonts.fontconfig;
@ -337,11 +338,12 @@ in
# Configuration for automatic login. Common for all DM. # Configuration for automatic login. Common for all DM.
autoLogin = mkOption { autoLogin = mkOption {
type = types.submodule { type = types.submodule ({ config, options, ... }: {
options = { options = {
enable = mkOption { enable = mkOption {
type = types.bool; type = types.bool;
default = cfg.displayManager.autoLogin.user != null; default = config.user != null;
defaultText = literalExpression "config.${options.user} != null";
description = '' description = ''
Automatically log in as <option>autoLogin.user</option>. Automatically log in as <option>autoLogin.user</option>.
''; '';
@ -355,7 +357,7 @@ in
''; '';
}; };
}; };
}; });
default = {}; default = {};
description = '' description = ''

View file

@ -1,10 +1,11 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.picom; cfg = config.services.picom;
opt = options.services.picom;
pairOf = x: with types; pairOf = x: with types;
addCheck (listOf x) (y: length y == 2) addCheck (listOf x) (y: length y == 2)
@ -178,7 +179,16 @@ in {
wintypes = mkOption { wintypes = mkOption {
type = types.attrs; type = types.attrs;
default = { popup_menu = { opacity = cfg.menuOpacity; }; dropdown_menu = { opacity = cfg.menuOpacity; }; }; default = {
popup_menu = { opacity = cfg.menuOpacity; };
dropdown_menu = { opacity = cfg.menuOpacity; };
};
defaultText = literalExpression ''
{
popup_menu = { opacity = config.${opt.menuOpacity}; };
dropdown_menu = { opacity = config.${opt.menuOpacity}; };
}
'';
example = {}; example = {};
description = '' description = ''
Rules for specific window types. Rules for specific window types.

View file

@ -6,6 +6,7 @@ with utils;
let let
cfg = config.networking; cfg = config.networking;
opt = options.networking;
interfaces = attrValues cfg.interfaces; interfaces = attrValues cfg.interfaces;
hasVirtuals = any (i: i.virtual) interfaces; hasVirtuals = any (i: i.virtual) interfaces;
hasSits = cfg.sits != { }; hasSits = cfg.sits != { };
@ -1169,6 +1170,9 @@ in
networking.tempAddresses = mkOption { networking.tempAddresses = mkOption {
default = if cfg.enableIPv6 then "default" else "disabled"; default = if cfg.enableIPv6 then "default" else "disabled";
defaultText = literalExpression ''
if ''${config.${opt.enableIPv6}} then "default" else "disabled"
'';
type = types.enum (lib.attrNames tempaddrValues); type = types.enum (lib.attrNames tempaddrValues);
description = '' description = ''
Whether to enable IPv6 Privacy Extensions for interfaces not Whether to enable IPv6 Privacy Extensions for interfaces not