summary refs log tree commit diff
path: root/nixos/modules/services/misc/headphones.nix
blob: 31bd61cb4c20009ec0140b244c0eb8fe70dc6173 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{ config, lib, options, pkgs, ... }:

with lib;

let

  name = "headphones";

  cfg = config.services.headphones;
  opt = options.services.headphones;

in

{

  ###### interface

  options = {
    services.headphones = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable the headphones server.";
      };
      dataDir = mkOption {
        type = types.path;
        default = "/var/lib/${name}";
        description = "Path where to store data files.";
      };
      configFile = mkOption {
        type = types.path;
        default = "${cfg.dataDir}/config.ini";
        defaultText = literalExpression ''"''${config.${opt.dataDir}}/config.ini"'';
        description = "Path to config file.";
      };
      host = mkOption {
        type = types.str;
        default = "localhost";
        description = "Host to listen on.";
      };
      port = mkOption {
        type = types.ints.u16;
        default = 8181;
        description = "Port to bind to.";
      };
      user = mkOption {
        type = types.str;
        default = name;
        description = "User to run the service as";
      };
      group = mkOption {
        type = types.str;
        default = name;
        description = "Group to run the service as";
      };
    };
  };


  ###### implementation

  config = mkIf cfg.enable {

    users.users = optionalAttrs (cfg.user == name) {
      ${name} = {
        uid = config.ids.uids.headphones;
        group = cfg.group;
        description = "headphones user";
        home = cfg.dataDir;
        createHome = true;
      };
    };

    users.groups = optionalAttrs (cfg.group == name) {
      ${name}.gid = config.ids.gids.headphones;
    };

    systemd.services.headphones = {
        description = "Headphones Server";
        wantedBy    = [ "multi-user.target" ];
        after = [ "network.target" ];
        serviceConfig = {
          User = cfg.user;
          Group = cfg.group;
          ExecStart = "${pkgs.headphones}/bin/headphones --datadir ${cfg.dataDir} --config ${cfg.configFile} --host ${cfg.host} --port ${toString cfg.port}";
        };
    };
  };
}