{
  pkgs,
  config,
  lib,
  ...
}:
let cfg = config.cos.trilium; in
{
  options.cos.trilium = {
    enable = lib.mkEnableOption "trilium-server";
    hostname = lib.mkOption {
      type = lib.types.str;
    };
    port = lib.mkOption {
      type = lib.types.port;
    };
    dataDir = lib.mkOption {
      type = lib.types.str;
    };
  };

  config = lib.mkIf cfg.enable {
    services.trilium-server = {
      enable = true;
      package = pkgs.trilium-next-server;
      port = cfg.port;
      dataDir = cfg.dataDir;
    };

    services.cloudflared.tunnels."4fc85af1-38f7-4c96-856c-7c797c5e3bf8".ingress.${cfg.hostname} = "http://${config.services.trilium-server.host}:${builtins.toString cfg.port}";
  };
}
