From 9cc26fef84b2deec8ec2e16ce712798662cac524 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 13 Apr 2025 19:39:22 +0100 Subject: [PATCH] nixos/cross-seed: add 'useGenConfigDefaults' This to me is the cleanest way of setting the default value and track changes to the values from `cross-seed gen-config` with low churn. --- nixos/modules/services/torrent/cross-seed.nix | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/torrent/cross-seed.nix b/nixos/modules/services/torrent/cross-seed.nix index b860a0fff550..3db8b84071c3 100644 --- a/nixos/modules/services/torrent/cross-seed.nix +++ b/nixos/modules/services/torrent/cross-seed.nix @@ -14,6 +14,15 @@ let types ; settingsFormat = pkgs.formats.json { }; + + generatedConfig = + pkgs.runCommand "cross-seed-gen-config" { nativeBuildInputs = [ pkgs.cross-seed ]; } + '' + export HOME=$(mktemp -d) + cross-seed gen-config + mkdir $out + cp -r $HOME/.cross-seed/config.js $out/ + ''; in { options.services.cross-seed = { @@ -40,6 +49,22 @@ in description = "Cross-seed config directory"; }; + useGenConfigDefaults = mkOption { + type = types.bool; + default = false; + description = '' + Whether to use the option defaults from the configuration generated by + {command}`cross-seed gen-config`. + + Those are the settings recommended by the project, and can be inspected + from their [template file](https://github.com/cross-seed/cross-seed/blob/master/src/config.template.cjs). + + Settings set in {option}`services.cross-seed.settings` and + {option}`services.cross-seed.settingsFile` will override the ones from + this option. + ''; + }; + settings = mkOption { default = { }; type = types.submodule { @@ -120,6 +145,13 @@ in let jsonSettingsFile = settingsFormat.generate "settings.json" cfg.settings; + genConfigSegment = + lib.optionalString cfg.useGenConfigDefaults # js + '' + const gen_config_js = "${generatedConfig}/config.js"; + Object.assign(loaded_settings, require(gen_config_js)); + ''; + # Since cross-seed uses a javascript config file, we can use node's # ability to parse JSON directly to avoid having to do any conversion. # This also means we don't need to use any external programs to merge the @@ -138,7 +170,9 @@ in "use strict"; const fs = require("fs"); const settings_json = "${jsonSettingsFile}"; - let loaded_settings = JSON.parse(fs.readFileSync(settings_json, "utf8")); + let loaded_settings = {}; + ${genConfigSegment} + Object.assign(loaded_settings, JSON.parse(fs.readFileSync(settings_json, "utf8"))); ${secretSettingsSegment} module.exports = loaded_settings; '';