summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/hydron.nix
blob: a4a5a435b2e6ba88a4928ce4deceb9a0503aaac2 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{ config, lib, pkgs, ... }:

let
  cfg = config.services.hydron;
in with lib; {
  options.services.hydron = {
    enable = mkEnableOption "hydron";

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/hydron";
      example = "/home/okina/hydron";
      description = "Location where hydron runs and stores data.";
    };

    interval = mkOption {
      type = types.str;
      default = "weekly";
      example = "06:00";
      description = ''
        How often we run hydron import and possibly fetch tags. Runs by default every week.

        The format is described in
        <citerefentry><refentrytitle>systemd.time</refentrytitle>
        <manvolnum>7</manvolnum></citerefentry>.
      '';
    };

    password = mkOption {
      type = types.str;
      default = "hydron";
      example = "dumbpass";
      description = "Password for the hydron database.";
    };

    passwordFile = mkOption {
      type = types.path;
      default = "/run/keys/hydron-password-file";
      example = "/home/okina/hydron/keys/pass";
      description = "Password file for the hydron database.";
    };

    postgresArgs = mkOption {
      type = types.str;
      description = "Postgresql connection arguments.";
      example = ''
        {
          "driver": "postgres",
          "connection": "user=hydron password=dumbpass dbname=hydron sslmode=disable"
        }
      '';
    };

    postgresArgsFile = mkOption {
      type = types.path;
      default = "/run/keys/hydron-postgres-args";
      example = "/home/okina/hydron/keys/postgres";
      description = "Postgresql connection arguments file.";
    };

    listenAddress = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "127.0.0.1:8010";
      description = "Listen on a specific IP address and port.";
    };

    importPaths = mkOption {
      type = types.listOf types.path;
      default = [];
      example = [ "/home/okina/Pictures" ];
      description = "Paths that hydron will recursively import.";
    };

    fetchTags = mkOption {
      type = types.bool;
      default = true;
      description = "Fetch tags for imported images and webm from gelbooru.";
    };
  };

  config = mkIf cfg.enable {
    services.hydron.passwordFile = mkDefault (pkgs.writeText "hydron-password-file" cfg.password);
    services.hydron.postgresArgsFile = mkDefault (pkgs.writeText "hydron-postgres-args" cfg.postgresArgs);
    services.hydron.postgresArgs = mkDefault ''
      {
        "driver": "postgres",
        "connection": "user=hydron password=${cfg.password} host=/run/postgresql dbname=hydron sslmode=disable"
      }
    '';

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

    systemd.tmpfiles.rules = [
      "d '${cfg.dataDir}' 0750 hydron hydron - -"
      "d '${cfg.dataDir}/.hydron' - hydron hydron - -"
      "d '${cfg.dataDir}/images' - hydron hydron - -"
      "Z '${cfg.dataDir}' - hydron hydron - -"

      "L+ '${cfg.dataDir}/.hydron/db_conf.json' - - - - ${cfg.postgresArgsFile}"
    ];

    systemd.services.hydron = {
      description = "hydron";
      after = [ "network.target" "postgresql.service" ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        User = "hydron";
        Group = "hydron";
        ExecStart = "${pkgs.hydron}/bin/hydron serve"
        + optionalString (cfg.listenAddress != null) " -a ${cfg.listenAddress}";
      };
    };

    systemd.services.hydron-fetch = {
      description = "Import paths into hydron and possibly fetch tags";

      serviceConfig = {
        Type = "oneshot";
        User = "hydron";
        Group = "hydron";
        ExecStart = "${pkgs.hydron}/bin/hydron import "
        + optionalString cfg.fetchTags "-f "
        + (escapeShellArg cfg.dataDir) + "/images " + (escapeShellArgs cfg.importPaths);
      };
    };

    systemd.timers.hydron-fetch = {
      description = "Automatically import paths into hydron and possibly fetch tags";
      after = [ "network.target" "hydron.service" ];
      wantedBy = [ "timers.target" ];

      timerConfig = {
        Persistent = true;
        OnCalendar = cfg.interval;
      };
    };

    users = {
      groups.hydron.gid = config.ids.gids.hydron;

      users.hydron = {
        description = "hydron server service user";
        home = cfg.dataDir;
        group = "hydron";
        uid = config.ids.uids.hydron;
      };
    };
  };

  imports = [
    (mkRenamedOptionModule [ "services" "hydron" "baseDir" ] [ "services" "hydron" "dataDir" ])
  ];

  meta.maintainers = with maintainers; [ chiiruno ];
}