Unverified Commit 9296b914 authored by Bruno BELANYI's avatar Bruno BELANYI Committed by GitHub
Browse files

nixos/cross-seed: add 'useGenConfigDefaults' (#398420)

parents 7c05a7f7 9cc26fef
Loading
Loading
Loading
Loading
+35 −1
Original line number Diff line number Diff line
@@ -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;
          '';