all repos — nixfiles @ 7d991814c484157f92dc8f202afe9f86d1074f6e

System and user configuration, managed by nix and home-manager

system/roles/opia/archive.nix (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{
  config,
  lib,
  pkgs,
  ...
}:
let
  domain = "opia.ovh";
  subdomain = "archive";

  pkg = pkgs.tg-archive;
  format = pkgs.formats.yaml { };

  groups = {
    main = "Opia Community Group Chat";
  };

  # see https://github.com/knadh/tg-archive/blob/master/tgarchive/example/config.yaml
  genCfg = name: group: {
    inherit group;

    download_media = true;
    download_avatars = true;
    avatar_size = [
      64
      64
    ];
    media_dir = "media";

    media_mime_types = [ ];

    proxy = {
      enable = false;
    };

    fetch_batch_size = 2000;
    fetch_wait = 5;
    fetch_limit = 0;

    publish_dir = "${config.users.users.tg-archive.home}/sites/${name}";
    static_dir = "static";
    per_page = 500;
    show_day_index = true;

    telegram_url = "https://t.me/{id}";
    show_sender_fullname = false;
    timezone = ""; # https://github.com/knadh/tg-archive/issues/130

    publish_rss_feed = false;
    rss_feed_entries = 100;

    site_url = "https://${subdomain}.${domain}";
    site_name = "@{group} - Telegram group archive";
    site_description = "Public archive of Telegram messages.";
    meta_description = "@{group} {date} - Telegram message archive.";
    page_title = "Page {page} - {date} @{group} Telegram message archive.";
  };

  genScript =
    name: group:
    let
      cfgFile = format.generate "tg-archive.yaml" (genCfg name group);
      dir = "data/${name}";
    in
    ''
      if test ! -d "${dir}"
      then
          ${lib.getExe pkg} --config ${cfgFile} --new --path ${dir}
      fi
      cd "${dir}"

      export API_ID API_HASH

      ${lib.getExe pkg} --config ${cfgFile} --sync

      ${lib.getExe pkg} --config ${cfgFile} --build
    '';
in
{
  age.secrets.tg-archive.file = ../../../secrets/tg-archive.age;

  users.groups.tg-archive = {
    members = [ "caddy" ];
  };
  users.users.tg-archive = {
    home = "/var/lib/tg-archive";
    homeMode = "750";
    createHome = true;
    isSystemUser = true;
    group = "tg-archive";
  };

  environment.systemPackages = [ pkgs.tg-archive ];

  systemd.services.tg-archive = {
    description = "Telegram Archive";
    serviceConfig = {
      Type = "oneshot";
      WorkingDirectory = config.users.users.tg-archive.home;
      EnvironmentFile = [ config.age.secrets.tg-archive.path ];
      User = "tg-archive";
      Group = "tg-archive";
    };

    preStart = ''
      if test ! -d data
      then
        mkdir data
      fi
      if test ! -d sites
      then
        mkdir sites
      fi
    '';
    script = lib.concatMapAttrsStringSep "\n" genScript groups;
  };

  services.caddy.virtualHosts = {
    "${subdomain}.${domain}" = {
      extraConfig = ''
        import tg_auth
        tls {
          dns_challenge_override_domain _acme-challenge.${subdomain}.opia.${config.networking.domain}
        }
        encode
        root ${config.users.users.tg-archive.home}/sites/
        file_server browse
      '';
    };
  };
}