1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8with lib;
9
10let
11 cfg = config.services.audiobookshelf;
12in
13{
14 options = {
15 services.audiobookshelf = {
16 enable = mkEnableOption "Audiobookshelf, self-hosted audiobook and podcast server";
17
18 package = mkPackageOption pkgs "audiobookshelf" { };
19
20 dataDir = mkOption {
21 description = "Path to Audiobookshelf config and metadata inside of /var/lib.";
22 default = "audiobookshelf";
23 type = types.str;
24 };
25
26 host = mkOption {
27 description = "The host Audiobookshelf binds to.";
28 default = "127.0.0.1";
29 example = "0.0.0.0";
30 type = types.str;
31 };
32
33 port = mkOption {
34 description = "The TCP port Audiobookshelf will listen on.";
35 default = 8000;
36 type = types.port;
37 };
38
39 user = mkOption {
40 description = "User account under which Audiobookshelf runs.";
41 default = "audiobookshelf";
42 type = types.str;
43 };
44
45 group = mkOption {
46 description = "Group under which Audiobookshelf runs.";
47 default = "audiobookshelf";
48 type = types.str;
49 };
50
51 openFirewall = mkOption {
52 description = "Open ports in the firewall for the Audiobookshelf web interface.";
53 default = false;
54 type = types.bool;
55 };
56 };
57 };
58
59 config = mkIf cfg.enable {
60 systemd.services.audiobookshelf = {
61 description = "Audiobookshelf is a self-hosted audiobook and podcast server";
62
63 after = [ "network.target" ];
64 wantedBy = [ "multi-user.target" ];
65
66 serviceConfig = {
67 Type = "simple";
68 User = cfg.user;
69 Group = cfg.group;
70 StateDirectory = cfg.dataDir;
71 WorkingDirectory = "/var/lib/${cfg.dataDir}";
72 ExecStart = "${cfg.package}/bin/audiobookshelf --host ${cfg.host} --port ${toString cfg.port}";
73 Restart = "on-failure";
74 };
75 };
76
77 users.users = mkIf (cfg.user == "audiobookshelf") {
78 audiobookshelf = {
79 isSystemUser = true;
80 group = cfg.group;
81 home = "/var/lib/${cfg.dataDir}";
82 };
83 };
84
85 users.groups = mkIf (cfg.group == "audiobookshelf") {
86 audiobookshelf = { };
87 };
88
89 networking.firewall = mkIf cfg.openFirewall {
90 allowedTCPPorts = [ cfg.port ];
91 };
92 };
93
94 meta.maintainers = with maintainers; [ wietsedv ];
95}