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