nixos: Replace custom cfg format handling with `pkgs.formats` (#314933)

jopejoe1 dca7e827 94689309

Changed files
+33 -88
nixos
modules
services
databases
development
logging
monitoring
networking
web-servers
+1 -5
nixos/modules/services/databases/influxdb.nix
···
};
} cfg.extraConfig;
-
configFile = pkgs.runCommandLocal "config.toml" { } ''
-
${pkgs.buildPackages.remarshal}/bin/remarshal -if json -of toml \
-
< ${pkgs.writeText "config.json" (builtins.toJSON configOptions)} \
-
> $out
-
'';
+
configFile = (pkgs.formats.toml {}).generate "config.toml" configOptions;
in
{
+4 -6
nixos/modules/services/development/athens.nix
···
}
);
-
configFile = pkgs.runCommandLocal "config.toml" { } ''
-
${pkgs.buildPackages.jq}/bin/jq 'del(..|nulls)' \
-
< ${pkgs.writeText "config.json" (builtins.toJSON athensConfig)} | \
-
${pkgs.buildPackages.remarshal}/bin/remarshal -if json -of toml \
-
> $out
-
'';
+
configFile = lib.pipe athensConfig [
+
(lib.filterAttrsRecursive (_k: v: v != null))
+
((pkgs.formats.toml {}).generate "config.toml")
+
];
in
{
meta = {
+6 -4
nixos/modules/services/logging/promtail.nix
···
let
cfg = config.services.promtail;
-
prettyJSON = conf: pkgs.runCommandLocal "promtail-config.json" {} ''
-
echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq 'del(._module)' > $out
-
'';
+
format = pkgs.formats.json {};
+
prettyJSON = conf: with lib; pipe conf [
+
(flip removeAttrs [ "_module" ])
+
(format.generate "promtail-config.json")
+
];
allowSystemdJournal = cfg.configuration ? scrape_configs && lib.any (v: v ? journal) cfg.configuration.scrape_configs;
···
enable = mkEnableOption "the Promtail ingresser";
configuration = mkOption {
-
type = (pkgs.formats.json {}).type;
+
type = format.type;
description = ''
Specify the configuration for Promtail in Nix.
This option will be ignored if `services.promtail.configFile` is defined.
+8 -12
nixos/modules/services/monitoring/thanos.nix
···
cfg = config.services.thanos;
nullOpt = type: description: mkOption {
-
type = types.nullOr type;
+
type = if type.check null then type else types.nullOr type;
default = null;
description = description;
};
···
};
};
-
toYAML = name: attrs: pkgs.runCommand name {
-
preferLocalBuild = true;
-
json = builtins.toFile "${name}.json" (builtins.toJSON attrs);
-
nativeBuildInputs = [ pkgs.remarshal ];
-
} "json2yaml -i $json -o $out";
+
format = pkgs.formats.yaml {};
thanos = cmd: "${cfg.package}/bin/thanos ${cmd}" +
(let args = cfg.${cmd}.arguments;
···
option = mkOption {
type = with types; nullOr str;
default = if cfg.tracing.config == null then null
-
else toString (toYAML "tracing.yaml" cfg.tracing.config);
+
else toString (format.generate "tracing.yaml" cfg.tracing.config);
defaultText = literalExpression ''
if config.services.thanos.<cmd>.tracing.config == null then null
-
else toString (toYAML "tracing.yaml" config.services.thanos.<cmd>.tracing.config);
+
else toString (format.generate "tracing.yaml" config.services.thanos.<cmd>.tracing.config);
'';
description = ''
Path to YAML file that contains tracing configuration.
···
tracing.config =
{
toArgs = _opt: _attrs: [];
-
option = nullOpt types.attrs ''
+
option = nullOpt format.type ''
Tracing configuration.
When not `null` the attribute set gets converted to
···
option = mkOption {
type = with types; nullOr str;
default = if cfg.objstore.config == null then null
-
else toString (toYAML "objstore.yaml" cfg.objstore.config);
+
else toString (format.generate "objstore.yaml" cfg.objstore.config);
defaultText = literalExpression ''
if config.services.thanos.<cmd>.objstore.config == null then null
-
else toString (toYAML "objstore.yaml" config.services.thanos.<cmd>.objstore.config);
+
else toString (format.generate "objstore.yaml" config.services.thanos.<cmd>.objstore.config);
'';
description = ''
Path to YAML file that contains object store configuration.
···
objstore.config =
{
toArgs = _opt: _attrs: [];
-
option = nullOpt types.attrs ''
+
option = nullOpt format.type ''
Object store configuration.
When not `null` the attribute set gets converted to
+4 -21
nixos/modules/services/networking/ncdns.nix
···
cfg = cfgs.ncdns;
dataDir = "/var/lib/ncdns";
-
username = "ncdns";
-
valueType = with lib.types; oneOf [ int str bool path ]
-
// { description = "setting type (integer, string, bool or path)"; };
-
-
configType = with lib.types; attrsOf (nullOr (either valueType configType))
-
// { description = ''
-
ncdns.conf configuration type. The format consists of an
-
attribute set of settings. Each setting can be either `null`,
-
a value or an attribute set. The allowed values are integers,
-
strings, booleans or paths.
-
'';
-
};
-
-
configFile = pkgs.runCommand "ncdns.conf"
-
{ json = builtins.toJSON cfg.settings;
-
passAsFile = [ "json" ];
-
}
-
"${pkgs.remarshal}/bin/json2toml < $jsonPath > $out";
+
format = pkgs.formats.toml {};
defaultFiles = {
public = "${dataDir}/bit.key";
···
needsKeygen = lib.all lib.id (lib.flip lib.mapAttrsToList cfg.dnssec.keys
(n: v: v == lib.getAttr n defaultFiles));
-
mkDefaultAttrs = lib.mapAttrs (n: v: lib.mkDefault v);
+
mkDefaultAttrs = lib.mapAttrs (_n: v: lib.mkDefault v);
in
···
};
settings = lib.mkOption {
-
type = configType;
+
type = format.type;
default = { };
example = lib.literalExpression ''
{ # enable webserver
···
User = "ncdns";
StateDirectory = "ncdns";
Restart = "on-failure";
-
ExecStart = "${pkgs.ncdns}/bin/ncdns -conf=${configFile}";
+
ExecStart = "${pkgs.ncdns}/bin/ncdns -conf=${format.generate "ncdns.conf" cfg.settings}";
};
preStart = lib.optionalString (cfg.dnssec.enable && needsKeygen) ''
+10 -40
nixos/modules/services/web-servers/traefik.nix
···
let
cfg = config.services.traefik;
-
jsonValue = with types;
-
let
-
valueType = nullOr (oneOf [
-
bool
-
int
-
float
-
str
-
(lazyAttrsOf valueType)
-
(listOf valueType)
-
]) // {
-
description = "JSON value";
-
emptyValue.value = { };
-
};
-
in valueType;
+
+
format = pkgs.formats.toml {};
+
dynamicConfigFile = if cfg.dynamicConfigFile == null then
-
pkgs.runCommand "config.toml" {
-
buildInputs = [ pkgs.remarshal ];
-
preferLocalBuild = true;
-
} ''
-
remarshal -if json -of toml \
-
< ${
-
pkgs.writeText "dynamic_config.json"
-
(builtins.toJSON cfg.dynamicConfigOptions)
-
} \
-
> $out
-
''
+
format.generate "config.toml" cfg.dynamicConfigOptions
else
cfg.dynamicConfigFile;
+
staticConfigFile = if cfg.staticConfigFile == null then
-
pkgs.runCommand "config.toml" {
-
buildInputs = [ pkgs.yj ];
-
preferLocalBuild = true;
-
} ''
-
yj -jt -i \
-
< ${
-
pkgs.writeText "static_config.json" (builtins.toJSON
-
(recursiveUpdate cfg.staticConfigOptions {
-
providers.file.filename = "${dynamicConfigFile}";
-
}))
-
} \
-
> $out
-
''
+
format.generate "config.toml" (recursiveUpdate cfg.staticConfigOptions {
+
providers.file.filename = "${dynamicConfigFile}";
+
})
else
cfg.staticConfigFile;
···
description = ''
Static configuration for Traefik.
'';
-
type = jsonValue;
+
type = format.type;
default = { entryPoints.http.address = ":80"; };
example = {
entryPoints.web.address = ":8080";
···
description = ''
Dynamic configuration for Traefik.
'';
-
type = jsonValue;
+
type = format.type;
default = { };
example = {
http.routers.router1 = {