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

This commit is contained in:
jopejoe1 2025-03-14 21:08:14 +01:00 committed by GitHub
commit dca7e827b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 33 additions and 88 deletions

View file

@ -93,11 +93,7 @@ let
};
} 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
{

View file

@ -140,12 +140,10 @@ let
}
);
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 = {

View file

@ -2,9 +2,11 @@
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;
@ -20,7 +22,7 @@ in {
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.

View file

@ -28,7 +28,7 @@ let
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;
};
@ -85,11 +85,7 @@ let
};
};
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;
@ -144,10 +140,10 @@ let
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.
@ -160,7 +156,7 @@ let
tracing.config =
{
toArgs = _opt: _attrs: [];
option = nullOpt types.attrs ''
option = nullOpt format.type ''
Tracing configuration.
When not `null` the attribute set gets converted to
@ -209,10 +205,10 @@ let
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.
@ -225,7 +221,7 @@ let
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

View file

@ -4,25 +4,8 @@ let
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";
@ -35,7 +18,7 @@ let
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
@ -160,7 +143,7 @@ in
};
settings = lib.mkOption {
type = configType;
type = format.type;
default = { };
example = lib.literalExpression ''
{ # enable webserver
@ -257,7 +240,7 @@ in
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) ''

View file

@ -4,48 +4,18 @@ with lib;
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;
@ -71,7 +41,7 @@ in {
description = ''
Static configuration for Traefik.
'';
type = jsonValue;
type = format.type;
default = { entryPoints.http.address = ":80"; };
example = {
entryPoints.web.address = ":8080";
@ -95,7 +65,7 @@ in {
description = ''
Dynamic configuration for Traefik.
'';
type = jsonValue;
type = format.type;
default = { };
example = {
http.routers.router1 = {