| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  | let | 
					
						
							|  |  |  |   cfg = config.services.hydron; | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2018-08-14 23:19:31 -05:00
										 |  |  |       default = "weekly"; | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |       example = "06:00"; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2018-08-14 23:19:31 -05:00
										 |  |  |         How often we run hydron import and possibly fetch tags. Runs by default every week. | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         The format is described in | 
					
						
							|  |  |  |         <citerefentry><refentrytitle>systemd.time</refentrytitle> | 
					
						
							|  |  |  |         <manvolnum>7</manvolnum></citerefentry>. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  |     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."; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |     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 { | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  |     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", | 
					
						
							| 
									
										
										
										
											2019-05-25 07:54:59 -04:00
										 |  |  |         "connection": "user=hydron password=${cfg.password} host=/run/postgresql dbname=hydron sslmode=disable" | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  |       } | 
					
						
							|  |  |  |     '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 07:54:59 -04:00
										 |  |  |     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}" | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |     systemd.services.hydron = { | 
					
						
							|  |  |  |       description = "hydron"; | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  |       after = [ "network.target" "postgresql.service" ]; | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |       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"; | 
					
						
							| 
									
										
										
										
											2018-08-14 23:19:31 -05:00
										 |  |  |       after = [ "network.target" "hydron.service" ]; | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |       wantedBy = [ "timers.target" ]; | 
					
						
							| 
									
										
										
										
											2019-05-25 07:54:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 23:19:31 -05:00
										 |  |  |       timerConfig = { | 
					
						
							|  |  |  |         Persistent = true; | 
					
						
							|  |  |  |         OnCalendar = cfg.interval; | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     users = { | 
					
						
							|  |  |  |       groups.hydron.gid = config.ids.gids.hydron; | 
					
						
							| 
									
										
										
										
											2019-05-25 07:54:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |       users.hydron = { | 
					
						
							|  |  |  |         description = "hydron server service user"; | 
					
						
							|  |  |  |         home = cfg.dataDir; | 
					
						
							|  |  |  |         group = "hydron"; | 
					
						
							|  |  |  |         uid = config.ids.uids.hydron; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 10:43:53 -05:00
										 |  |  |   imports = [ | 
					
						
							|  |  |  |     (mkRenamedOptionModule [ "services" "hydron" "baseDir" ] [ "services" "hydron" "dataDir" ]) | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 18:10:02 -05:00
										 |  |  |   meta.maintainers = with maintainers; [ chiiruno ]; | 
					
						
							|  |  |  | } |