{ ... }:
{
  _class = "clan.service";
  manifest.name = "nextcloud";
  manifest.description = "Nextcloud server, a safe home for all your data";
  manifest.readme = "Nextcloud server, a safe home for all your data";
  manifest.categories = [ "System" ];

  roles.default = {
    description = "a default server role";

    perInstance.nixosModule =
      {
        config,
        pkgs,
        lib,
        ...
      }:
      {
        clan.core.vars.generators.nextcloud = {
          files = {
            adminpassFile = {
              owner = "nextcloud";
              group = "nextcloud";
              secret = true;
            };
          };
          script = ''
            xkcdpass --numwords 4 --delimiter - --count 1 | tr -d "\n" > "$out"/adminpassFile
          '';
          runtimeInputs = [
            pkgs.xkcdpass
          ];
        };

        services.nextcloud = {
          enable = lib.mkDefault true;
          hostName = lib.mkDefault "localhost";
          database.createLocally = lib.mkDefault true;
          config = {
            dbtype = lib.mkDefault "pgsql";
            dbhost = lib.mkDefault "/run/postgresql";
            dbuser = lib.mkDefault "nextcloud";
            dbname = lib.mkDefault "nextcloud";
            adminuser = lib.mkDefault "admin";
            adminpassFile = lib.mkDefault config.clan.core.vars.generators.nextcloud.files.adminpassFile.path;
          };

        };

        clan.core.state.nextcloud.folders = [
          config.services.nextcloud.home
        ]
        ++ (
          if config.services.nextcloud.home != config.services.nextcloud.datadir then
            [ config.services.nextcloud.datadir ]
          else
            [ ]
        );
      };
  };
}
