1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.hydron;
5in with lib; {
6 options.services.hydron = {
7 enable = mkEnableOption (lib.mdDoc "hydron");
8
9 dataDir = mkOption {
10 type = types.path;
11 default = "/var/lib/hydron";
12 example = "/home/okina/hydron";
13 description = lib.mdDoc "Location where hydron runs and stores data.";
14 };
15
16 interval = mkOption {
17 type = types.str;
18 default = "weekly";
19 example = "06:00";
20 description = lib.mdDoc ''
21 How often we run hydron import and possibly fetch tags. Runs by default every week.
22
23 The format is described in
24 {manpage}`systemd.time(7)`.
25 '';
26 };
27
28 password = mkOption {
29 type = types.str;
30 default = "hydron";
31 example = "dumbpass";
32 description = lib.mdDoc "Password for the hydron database.";
33 };
34
35 passwordFile = mkOption {
36 type = types.path;
37 default = "/run/keys/hydron-password-file";
38 example = "/home/okina/hydron/keys/pass";
39 description = lib.mdDoc "Password file for the hydron database.";
40 };
41
42 postgresArgs = mkOption {
43 type = types.str;
44 description = lib.mdDoc "Postgresql connection arguments.";
45 example = ''
46 {
47 "driver": "postgres",
48 "connection": "user=hydron password=dumbpass dbname=hydron sslmode=disable"
49 }
50 '';
51 };
52
53 postgresArgsFile = mkOption {
54 type = types.path;
55 default = "/run/keys/hydron-postgres-args";
56 example = "/home/okina/hydron/keys/postgres";
57 description = lib.mdDoc "Postgresql connection arguments file.";
58 };
59
60 listenAddress = mkOption {
61 type = types.nullOr types.str;
62 default = null;
63 example = "127.0.0.1:8010";
64 description = lib.mdDoc "Listen on a specific IP address and port.";
65 };
66
67 importPaths = mkOption {
68 type = types.listOf types.path;
69 default = [];
70 example = [ "/home/okina/Pictures" ];
71 description = lib.mdDoc "Paths that hydron will recursively import.";
72 };
73
74 fetchTags = mkOption {
75 type = types.bool;
76 default = true;
77 description = lib.mdDoc "Fetch tags for imported images and webm from gelbooru.";
78 };
79 };
80
81 config = mkIf cfg.enable {
82 services.hydron.passwordFile = mkDefault (pkgs.writeText "hydron-password-file" cfg.password);
83 services.hydron.postgresArgsFile = mkDefault (pkgs.writeText "hydron-postgres-args" cfg.postgresArgs);
84 services.hydron.postgresArgs = mkDefault ''
85 {
86 "driver": "postgres",
87 "connection": "user=hydron password=${cfg.password} host=/run/postgresql dbname=hydron sslmode=disable"
88 }
89 '';
90
91 services.postgresql = {
92 enable = true;
93 ensureDatabases = [ "hydron" ];
94 ensureUsers = [
95 { name = "hydron";
96 ensurePermissions = { "DATABASE hydron" = "ALL PRIVILEGES"; };
97 }
98 ];
99 };
100
101 systemd.tmpfiles.rules = [
102 "d '${cfg.dataDir}' 0750 hydron hydron - -"
103 "d '${cfg.dataDir}/.hydron' - hydron hydron - -"
104 "d '${cfg.dataDir}/images' - hydron hydron - -"
105 "Z '${cfg.dataDir}' - hydron hydron - -"
106
107 "L+ '${cfg.dataDir}/.hydron/db_conf.json' - - - - ${cfg.postgresArgsFile}"
108 ];
109
110 systemd.services.hydron = {
111 description = "hydron";
112 after = [ "network.target" "postgresql.service" ];
113 wantedBy = [ "multi-user.target" ];
114
115 serviceConfig = {
116 User = "hydron";
117 Group = "hydron";
118 ExecStart = "${pkgs.hydron}/bin/hydron serve"
119 + optionalString (cfg.listenAddress != null) " -a ${cfg.listenAddress}";
120 };
121 };
122
123 systemd.services.hydron-fetch = {
124 description = "Import paths into hydron and possibly fetch tags";
125
126 serviceConfig = {
127 Type = "oneshot";
128 User = "hydron";
129 Group = "hydron";
130 ExecStart = "${pkgs.hydron}/bin/hydron import "
131 + optionalString cfg.fetchTags "-f "
132 + (escapeShellArg cfg.dataDir) + "/images " + (escapeShellArgs cfg.importPaths);
133 };
134 };
135
136 systemd.timers.hydron-fetch = {
137 description = "Automatically import paths into hydron and possibly fetch tags";
138 after = [ "network.target" "hydron.service" ];
139 wantedBy = [ "timers.target" ];
140
141 timerConfig = {
142 Persistent = true;
143 OnCalendar = cfg.interval;
144 };
145 };
146
147 users = {
148 groups.hydron.gid = config.ids.gids.hydron;
149
150 users.hydron = {
151 description = "hydron server service user";
152 home = cfg.dataDir;
153 group = "hydron";
154 uid = config.ids.uids.hydron;
155 };
156 };
157 };
158
159 imports = [
160 (mkRenamedOptionModule [ "services" "hydron" "baseDir" ] [ "services" "hydron" "dataDir" ])
161 ];
162
163 meta.maintainers = with maintainers; [ Madouura ];
164}