@@ -0,0 +1,81 @@
1+{ config, pkgs, lib, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.plexpy;
7+in
8+{
9+ options = {
10+ services.plexpy = {
11+ enable = mkEnableOption "PlexPy Plex Monitor";
12+
13+ dataDir = mkOption {
14+ type = types.str;
15+ default = "/var/lib/plexpy";
16+ description = "The directory where PlexPy stores its data files.";
17+ };
18+
19+ configFile = mkOption {
20+ type = types.str;
21+ default = "/var/lib/plexpy/config.ini";
22+ description = "The location of PlexPy's config file.";
23+ };
24+
25+ port = mkOption {
26+ type = types.int;
27+ default = 8181;
28+ description = "TCP port where PlexPy listens.";
29+ };
30+
31+ user = mkOption {
32+ type = types.str;
33+ default = "plexpy";
34+ description = "User account under which PlexPy runs.";
35+ };
36+
37+ group = mkOption {
38+ type = types.str;
39+ default = "nogroup";
40+ description = "Group under which PlexPy runs.";
41+ };
42+
43+ package = mkOption {
44+ type = types.package;
45+ default = pkgs.plexpy;
46+ defaultText = "pkgs.plexpy";
47+ description = ''
48+ The PlexPy package to use.
49+ '';
50+ };
51+ };
52+ };
53+
54+ config = mkIf cfg.enable {
55+ systemd.services.plexpy = {
56+ description = "PlexPy Plex Monitor";
57+ after = [ "network.target" ];
58+ wantedBy = [ "multi-user.target" ];
59+ preStart = ''
60+ test -d "${cfg.dataDir}" || {
61+ echo "Creating initial PlexPy data directory in \"${cfg.dataDir}\"."
62+ mkdir -p "${cfg.dataDir}"
63+ chown ${cfg.user}:${cfg.group} "${cfg.dataDir}"
64+ }
65+ '';
66+ serviceConfig = {
67+ Type = "simple";
68+ User = cfg.user;
69+ Group = cfg.group;
70+ PermissionsStartOnly = "true";
71+ GuessMainPID = "false";
72+ ExecStart = "${cfg.package}/bin/plexpy --datadir ${cfg.dataDir} --config ${cfg.configFile} --port ${toString cfg.port} --pidfile ${cfg.dataDir}/plexpy.pid --nolaunch";
73+ Restart = "on-failure";
74+ };
75+ };
76+
77+ users.extraUsers = mkIf (cfg.user == "plexpy") {
78+ plexpy = { group = cfg.group; uid = config.ids.uids.plexpy; };
79+ };
80+ };
81+}