{ pkgs, ... }:
{
  services.nextcloud = {
    enable = true;
    config = {
      dbtype = "pgsql";
      dbuser = "nextcloud";
      dbhost = "/run/postgresql"; # nextcloud will add /.s.PGSQL.5432 by itself
      dbname = "nextcloud";
      adminpassFile = "${../secret-data/nextcloud-admin-pass}";
      adminuser = "root";
    };
    package = pkgs.nextcloud25;
    extraApps = with pkgs.nextcloud25Packages.apps; { 
      inherit bookmarks calendar contacts news tasks;
    };
    extraAppsEnable = true;
    hostName = "wolke.schrottkatze.de";
    https = true;
  };

  services.postgresql = {
    enable = true;
    ensureDatabases = [ "nextcloud" ];
    ensureUsers = [
      { 
        name = "nextcloud";
        ensurePermissions."DATABASE nextcloud" = "ALL PRIVILEGES";
      }
    ];
  };

  # ensure that postgres is running *before* running the setup
  systemd.services."nextcloud-setup" = {
    requires = ["postgresql.service"];
    after = ["postgresql.service"];
  };
}
