anchor.diff:      };
     };
     configFile = format.generate "syncstorage.toml" (lib.recursiveUpdate settings cfg.settings);
  +  setupScript = pkgs.writeShellScript "firefox-syncserver-setup" ''
  +        set -euo pipefail
  +        shopt -s inherit_errexit
  +
  +        schema_configured() {
  +          mysql ${cfg.database.name} -Ne 'SHOW TABLES' | grep -q services
  +        }
  +
  +        update_config() {
  +          mysql ${cfg.database.name} <<"EOF"
  +            BEGIN;
  +
  +            INSERT INTO `services` (`id`, `service`, `pattern`)
  +              VALUES (1, 'sync-1.5', '{node}/1.5/{uid}')
  +              ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}';
  +            INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`,
  +                                 `capacity`, `downed`, `backoff`)
  +              VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity},
  +              0, ${toString cfg.singleNode.capacity}, 0, 0)
  +              ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity};
  +
  +            COMMIT;
  +        EOF
  +        }
  +
  +
  +        for (( try = 0; try < 60; try++ )); do
  +          if ! schema_configured; then
  +            sleep 2
  +          else
  +            update_config
  +            exit 0
  +          fi
  +        done
  +
  +        echo "Single-node setup failed"
  +        exit 1
  +      '';
   in
   
   {
  
positive.code:       wantedBy = [ "multi-user.target" ];
        requires = lib.mkIf dbIsLocal [ "mysql.service" ];
        after = lib.mkIf dbIsLocal [ "mysql.service" ];
        environment.RUST_LOG = cfg.logLevel;
        serviceConfig = {
          User = defaultUser;
  
negative.code:       requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service";
        after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service";
        path = [ config.services.mysql.package ];
        serviceConfig.ExecStart = [ "${setupScript}" ];
      };
  
      services.nginx.virtualHosts = lib.mkIf cfg.singleNode.enableNginx {
  