Unverified Commit e9b01c5c authored by Naïm Favier's avatar Naïm Favier
Browse files

nixos/syncthing: move declarative options to the top level

parent a66d9f9b
Loading
Loading
Loading
Loading
+276 −276
Original line number Diff line number Diff line
@@ -9,11 +9,11 @@ let
  devices = mapAttrsToList (name: device: {
    deviceID = device.id;
    inherit (device) name addresses introducer;
  }) cfg.declarative.devices;
  }) cfg.devices;

  folders = mapAttrsToList ( _: folder: {
    inherit (folder) path id label type;
    devices = map (device: { deviceId = cfg.declarative.devices.${device}.id; }) folder.devices;
    devices = map (device: { deviceId = cfg.devices.${device}.id; }) folder.devices;
    rescanIntervalS = folder.rescanInterval;
    fsWatcherEnabled = folder.watch;
    fsWatcherDelayS = folder.watchDelay;
@@ -23,7 +23,7 @@ let
  }) (filterAttrs (
    _: folder:
    folder.enable
  ) cfg.declarative.folders);
  ) cfg.folders);

  updateConfig = pkgs.writers.writeDash "merge-syncthing-config" ''
    set -efu
@@ -50,9 +50,9 @@ let

    # generate the new config by merging with the NixOS config options
    new_cfg=$(echo "$old_cfg" | ${pkgs.jq}/bin/jq -c '. * {
        "devices": (${builtins.toJSON devices}${optionalString (! cfg.declarative.overrideDevices) " + .devices"}),
        "folders": (${builtins.toJSON folders}${optionalString (! cfg.declarative.overrideFolders) " + .folders"})
    } * ${builtins.toJSON cfg.declarative.extraOptions}')
        "devices": (${builtins.toJSON devices}${optionalString (! cfg.overrideDevices) " + .devices"}),
        "folders": (${builtins.toJSON folders}${optionalString (! cfg.overrideFolders) " + .folders"})
    } * ${builtins.toJSON cfg.extraOptions}')

    # send the new config
    curl -X PUT -d "$new_cfg" ${cfg.guiAddress}/rest/config
@@ -74,7 +74,6 @@ in {
        available on http://127.0.0.1:8384/.
      '';

      declarative = {
      cert = mkOption {
        type = types.nullOr types.str;
        default = null;
@@ -98,7 +97,7 @@ in {
        default = true;
        description = ''
          Whether to delete the devices which are not configured via the
            <literal>declarative.devices</literal> option.
          <literal>devices</literal> option.
          If set to false, devices added via the webinterface will
          persist but will have to be deleted manually.
        '';
@@ -161,7 +160,7 @@ in {
        default = true;
        description = ''
          Whether to delete the folders which are not configured via the
            <literal>declarative.folders</literal> option.
          <literal>folders</literal> option.
          If set to false, folders added via the webinterface will persist
          but will have to be deleted manually.
        '';
@@ -222,7 +221,7 @@ in {
              default = [];
              description = ''
                The devices this folder should be shared with. Must be defined
                  in the <literal>declarative.devices</literal> attribute.
                in the <literal>devices</literal> attribute.
              '';
            };

@@ -352,7 +351,6 @@ in {
          gui.theme = "black";
        };
      };
      };

      guiAddress = mkOption {
        type = types.str;
@@ -447,7 +445,9 @@ in {
      This option was removed because Syncthing now has the inotify functionality included under the name "fswatcher".
      It can be enabled on a per-folder basis through the webinterface.
    '')
  ];
  ] ++ map (o:
    mkRenamedOptionModule [ "services" "syncthing" "declarative" o ] [ "services" "syncthing" o ]
  ) [ "cert" "key" "devices" "folders" "overrideDevices" "overrideFolders" "extraOptions"];

  ###### implementation

@@ -491,14 +491,14 @@ in {
          RestartForceExitStatus="3 4";
          User = cfg.user;
          Group = cfg.group;
          ExecStartPre = mkIf (cfg.declarative.cert != null || cfg.declarative.key != null)
          ExecStartPre = mkIf (cfg.cert != null || cfg.key != null)
            "+${pkgs.writers.writeBash "syncthing-copy-keys" ''
              install -dm700 -o ${cfg.user} -g ${cfg.group} ${cfg.configDir}
              ${optionalString (cfg.declarative.cert != null) ''
                install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.declarative.cert} ${cfg.configDir}/cert.pem
              ${optionalString (cfg.cert != null) ''
                install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.cert} ${cfg.configDir}/cert.pem
              ''}
              ${optionalString (cfg.declarative.key != null) ''
                install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.declarative.key} ${cfg.configDir}/key.pem
              ${optionalString (cfg.key != null) ''
                install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.key} ${cfg.configDir}/key.pem
              ''}
            ''}"
          ;
@@ -529,7 +529,7 @@ in {
        };
      };
      syncthing-init = mkIf (
        cfg.declarative.devices != {} || cfg.declarative.folders != {} || cfg.declarative.extraOptions != {}
        cfg.devices != {} || cfg.folders != {} || cfg.extraOptions != {}
      ) {
        description = "Syncthing configuration updater";
        after = [ "syncthing.service" ];
+7 −9
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ in {
  machine = {
    services.syncthing = {
      enable = true;
      declarative = {
      devices.testDevice = {
        id = testId;
      };
@@ -20,7 +19,6 @@ in {
      extraOptions.gui.user = "guiUser";
    };
  };
  };

  testScript = ''
    machine.wait_for_unit("syncthing-init.service")