summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/audio/mympd.nix
blob: 5f0b027d33bf8b60ead73f68b4c3730645309cfe (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
  pkgs,
  config,
  lib,
  ...
}:

let
  cfg = config.services.mympd;
in
{
  options = {

    services.mympd = {

      enable = lib.mkEnableOption "MyMPD server";

      package = lib.mkPackageOption pkgs "mympd" { };

      openFirewall = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Open ports needed for the functionality of the program.
        '';
      };

      extraGroups = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
        example = [ "music" ];
        description = ''
          Additional groups for the systemd service.
        '';
      };

      settings = lib.mkOption {
        type = lib.types.submodule {
          freeformType =
            with lib.types;
            attrsOf (
              nullOr (oneOf [
                str
                bool
                int
              ])
            );
          options = {
            http_port = lib.mkOption {
              type = lib.types.port;
              description = ''
                The HTTP port where mympd's web interface will be available.

                The HTTPS/SSL port can be configured via {option}`config`.
              '';
              example = "8080";
            };

            ssl = lib.mkOption {
              type = lib.types.bool;
              description = ''
                Whether to enable listening on the SSL port.

                Refer to <https://jcorporation.github.io/myMPD/020-configuration/configuration-files#ssl-options>
                for more information.
              '';
              default = false;
            };
          };
        };
        description = ''
          Manages the configuration files declaratively. For all the configuration
          options, see <https://jcorporation.github.io/myMPD/020-configuration/configuration-files>.

          Each key represents the "File" column from the upstream configuration table, and the
          value is the content of that file.
        '';
      };
    };

  };

  config = lib.mkIf cfg.enable {
    systemd.services.mympd = {
      # upstream service config: https://github.com/jcorporation/myMPD/blob/master/contrib/initscripts/mympd.service.in
      after = [ "mpd.service" ];
      wantedBy = [ "multi-user.target" ];
      preStart = with lib; ''
        config_dir="/var/lib/mympd/config"
        mkdir -p "$config_dir"

        ${pipe cfg.settings [
          (mapAttrsToList (
            name: value: ''
              echo -n "${if isBool value then boolToString value else toString value}" > "$config_dir/${name}"
            ''
          ))
          (concatStringsSep "\n")
        ]}
      '';
      unitConfig = {
        Description = "myMPD server daemon";
        Documentation = "man:mympd(1)";
      };
      serviceConfig = {
        AmbientCapabilities = "CAP_NET_BIND_SERVICE";
        CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
        DynamicUser = true;
        ExecStart = lib.getExe cfg.package;
        LockPersonality = true;
        MemoryDenyWriteExecute = false; # pcre2 jit
        PrivateDevices = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        RestrictRealtime = true;
        StateDirectory = "mympd";
        CacheDirectory = "mympd";
        RestrictAddressFamilies = "AF_INET AF_INET6 AF_NETLINK AF_UNIX";
        RestrictNamespaces = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = "@system-service";
        SupplementaryGroups = cfg.extraGroups;
      };
    };

    networking.firewall = lib.mkMerge [
      (lib.mkIf cfg.openFirewall {
        allowedTCPPorts = [ cfg.settings.http_port ];
      })
      (lib.mkIf (cfg.openFirewall && cfg.settings.ssl && cfg.settings.ssl_port != null) {
        allowedTCPPorts = [ cfg.settings.ssl_port ];
      })
    ];

  };

  meta.maintainers = [ lib.maintainers.eliandoran ];

}