I am writing a new NixOS module and this particular set of software relies on a toml and json for configuring application. I want to conditionally update the toml file to include the json file if services.new-service.jsonFile is present.
If jsonFile is present, the toml file should look like this:
#/nix/store/....-settings.toml
[other]
a: 1
[schema]
file: file:///nix/store/...-schema.json
Example nix configuration using services.new-service:
# flake.nix
{
...
}:
{
services.new-service = {
settings = { # transformed to settings.toml
a = 1;
b = 2;
};
schema = { # transformed to schema.json
x = "hello";
y = "world";
};
};
}
This is my naive approach so far:
# default.nix
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.new-service;
tFormat = pkgs.formats.toml { };
jFormat = pkgs.formats.json { };
in
imports [ ./new-service.nix ];
options.services.new-service = {
settings = lib.mkOption {
inherit (tFormat) type;
};
schema = lib.mkOption {
inherit (jFormat) type;
};
};
# new-service.nix
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.new-service;
cfgFormat = pkgs.formats.toml { };
schemaFormat = pkgs.formats.json { };
cfgFile = cfgFormat.generate "settings.toml" (cfg.settings // lib.optionalAttrs (cfg.jsonFile != null) { schema = { file = "file://${schemaFormat.generate "schema.json" cfg.jsonFile}"; }; };);
in
...
It appears syntactically correct, but flake rebuild fails to evaluate due to:
...
error:
… while calling the 'seq' builtin
at /nix/store/wzcf74b2g5c3qfg4rcswcz28ngiwzzp0-source/lib/modules.nix:403:18:
402| options = checked options;
403| config = checked (removeAttrs config [ "_module" ]);
| ^
404| _module = checked (config._module);
… while evaluating a branch condition
at /nix/store/wzcf74b2g5c3qfg4rcswcz28ngiwzzp0-source/lib/modules.nix:306:9:
305| checkUnmatched =
306| if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [ ] then
| ^
307| let
(stack trace truncated; use '--show-trace' to show the full, detailed trace)
error: function 'mkOption' called with unexpected argument 'configuration'
at /nix/store/wzcf74b2g5c3qfg4rcswcz28ngiwzzp0-source/lib/options.nix:140:5:
139| mkOption =
140| {
| ^
141| default ? null,
Which I suspect is due to:
cfg.settings // lib.optionalAttrs (cfg.jsonFile != null) ...
Which I suppose makes sense, I am trying to merge the attribute set { schema = { file = "file://${schemaFormat.generate "schema.json" cfg.jsonFile}"; }; } with lib.mkOption. But I am not entirely sure where to go from here.
Appreciate any help here!