1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.gonic;
9 settingsFormat = pkgs.formats.keyValue {
10 mkKeyValue = lib.generators.mkKeyValueDefault { } " ";
11 listsAsDuplicateKeys = true;
12 };
13in
14{
15 options = {
16 services.gonic = {
17
18 enable = lib.mkEnableOption "Gonic music server";
19
20 settings = lib.mkOption rec {
21 type = settingsFormat.type;
22 apply = lib.recursiveUpdate default;
23 default = {
24 listen-addr = "127.0.0.1:4747";
25 cache-path = "/var/cache/gonic";
26 tls-cert = null;
27 tls-key = null;
28 };
29 example = {
30 music-path = [ "/mnt/music" ];
31 podcast-path = "/mnt/podcasts";
32 };
33 description = ''
34 Configuration for Gonic, see <https://github.com/sentriz/gonic#configuration-options> for supported values.
35 '';
36 };
37
38 };
39 };
40
41 config = lib.mkIf cfg.enable {
42 systemd.services.gonic = {
43 description = "Gonic Media Server";
44 after = [ "network.target" ];
45 wantedBy = [ "multi-user.target" ];
46 serviceConfig = {
47 ExecStart =
48 let
49 # these values are null by default but should not appear in the final config
50 filteredSettings = lib.filterAttrs (
51 n: v: !((n == "tls-cert" || n == "tls-key") && v == null)
52 ) cfg.settings;
53 in
54 "${pkgs.gonic}/bin/gonic -config-path ${settingsFormat.generate "gonic" filteredSettings}";
55 DynamicUser = true;
56 StateDirectory = "gonic";
57 CacheDirectory = "gonic";
58 WorkingDirectory = "/var/lib/gonic";
59 RuntimeDirectory = "gonic";
60 RootDirectory = "/run/gonic";
61 ReadWritePaths = "";
62 BindPaths = [
63 cfg.settings.playlists-path
64 cfg.settings.podcast-path
65 ];
66 BindReadOnlyPaths = [
67 # gonic can access scrobbling services
68 "-/etc/resolv.conf"
69 "${config.security.pki.caBundle}:/etc/ssl/certs/ca-certificates.crt"
70 builtins.storeDir
71 ]
72 ++ cfg.settings.music-path
73 ++ lib.optional (cfg.settings.tls-cert != null) cfg.settings.tls-cert
74 ++ lib.optional (cfg.settings.tls-key != null) cfg.settings.tls-key;
75 CapabilityBoundingSet = "";
76 RestrictAddressFamilies = [
77 "AF_UNIX"
78 "AF_INET"
79 "AF_INET6"
80 ];
81 RestrictNamespaces = true;
82 PrivateDevices = true;
83 PrivateUsers = true;
84 ProtectClock = true;
85 ProtectControlGroups = true;
86 ProtectHome = true;
87 ProtectKernelLogs = true;
88 ProtectKernelModules = true;
89 ProtectKernelTunables = true;
90 SystemCallArchitectures = "native";
91 SystemCallFilter = [
92 "@system-service"
93 "~@privileged"
94 ];
95 RestrictRealtime = true;
96 LockPersonality = true;
97 MemoryDenyWriteExecute = true;
98 UMask = "0066";
99 ProtectHostname = true;
100 };
101 };
102 };
103
104 meta.maintainers = [ lib.maintainers.autrimpo ];
105}