1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 name = "headphones";
8
9 cfg = config.services.headphones;
10
11in
12
13{
14
15 ###### interface
16
17 options = {
18 services.headphones = {
19 enable = mkOption {
20 type = types.bool;
21 default = false;
22 description = "Whether to enable the headphones server.";
23 };
24 dataDir = mkOption {
25 type = types.path;
26 default = "/var/lib/${name}";
27 description = "Path where to store data files.";
28 };
29 configFile = mkOption {
30 type = types.path;
31 default = "${cfg.dataDir}/config.ini";
32 description = "Path to config file.";
33 };
34 host = mkOption {
35 type = types.str;
36 default = "localhost";
37 description = "Host to listen on.";
38 };
39 port = mkOption {
40 type = types.ints.u16;
41 default = 8181;
42 description = "Port to bind to.";
43 };
44 user = mkOption {
45 type = types.str;
46 default = name;
47 description = "User to run the service as";
48 };
49 group = mkOption {
50 type = types.str;
51 default = name;
52 description = "Group to run the service as";
53 };
54 };
55 };
56
57
58 ###### implementation
59
60 config = mkIf cfg.enable {
61
62 users.users = optionalAttrs (cfg.user == name) {
63 ${name} = {
64 uid = config.ids.uids.headphones;
65 group = cfg.group;
66 description = "headphones user";
67 home = cfg.dataDir;
68 createHome = true;
69 };
70 };
71
72 users.groups = optionalAttrs (cfg.group == name) {
73 ${name}.gid = config.ids.gids.headphones;
74 };
75
76 systemd.services.headphones = {
77 description = "Headphones Server";
78 wantedBy = [ "multi-user.target" ];
79 after = [ "network.target" ];
80 serviceConfig = {
81 User = cfg.user;
82 Group = cfg.group;
83 ExecStart = "${pkgs.headphones}/bin/headphones --datadir ${cfg.dataDir} --config ${cfg.configFile} --host ${cfg.host} --port ${toString cfg.port}";
84 };
85 };
86 };
87}