|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | + |
| 8 | +let |
| 9 | + inherit (lib) |
| 10 | + getExe |
| 11 | + maintainers |
| 12 | + mkEnableOption |
| 13 | + mkIf |
| 14 | + mkOption |
| 15 | + mkPackageOption |
| 16 | + ; |
| 17 | + inherit (lib.types) |
| 18 | + bool |
| 19 | + path |
| 20 | + port |
| 21 | + str |
| 22 | + submodule |
| 23 | + ; |
| 24 | + cfg = config.services.qui; |
| 25 | + |
| 26 | + stateDir = "/var/lib/qui"; |
| 27 | + configFormat = pkgs.formats.toml { }; |
| 28 | + configFile = configFormat.generate "qui.toml" cfg.settings; |
| 29 | +in |
| 30 | +{ |
| 31 | + options = { |
| 32 | + services.qui = { |
| 33 | + enable = mkEnableOption "qui"; |
| 34 | + |
| 35 | + package = mkPackageOption pkgs "qui" { }; |
| 36 | + |
| 37 | + user = mkOption { |
| 38 | + type = str; |
| 39 | + default = "qui"; |
| 40 | + description = "User to run qui as."; |
| 41 | + }; |
| 42 | + |
| 43 | + group = mkOption { |
| 44 | + type = str; |
| 45 | + default = "qui"; |
| 46 | + example = "torrents"; |
| 47 | + description = "Group to run qui as."; |
| 48 | + }; |
| 49 | + |
| 50 | + openFirewall = mkOption { |
| 51 | + type = bool; |
| 52 | + default = false; |
| 53 | + description = "Whether or not to open ports in the firewall for qui."; |
| 54 | + }; |
| 55 | + |
| 56 | + secretFile = mkOption { |
| 57 | + type = path; |
| 58 | + example = "/run/secrets/qui-session.txt"; |
| 59 | + description = '' |
| 60 | + Path to a file that contains the session secret. The session secret |
| 61 | + can be generated with `openssl rand -hex 32`. |
| 62 | + ''; |
| 63 | + }; |
| 64 | + |
| 65 | + settings = mkOption { |
| 66 | + default = { }; |
| 67 | + example = { |
| 68 | + port = 7777; |
| 69 | + logLevel = "DEBUG"; |
| 70 | + metricsEnabled = true; |
| 71 | + }; |
| 72 | + type = submodule { |
| 73 | + freeformType = configFormat.type; |
| 74 | + options = { |
| 75 | + host = mkOption { |
| 76 | + type = str; |
| 77 | + default = "127.0.0.1"; |
| 78 | + description = "The host address qui listens on."; |
| 79 | + }; |
| 80 | + |
| 81 | + port = mkOption { |
| 82 | + type = port; |
| 83 | + default = 7476; |
| 84 | + description = "The port qui listens on."; |
| 85 | + }; |
| 86 | + }; |
| 87 | + }; |
| 88 | + description = '' |
| 89 | + qui configuration options. |
| 90 | +
|
| 91 | + Refer to the [template config](https://github.com/autobrr/qui/blob/main/internal/config/config.go) |
| 92 | + in the source code for the available options. |
| 93 | + The documentation contains the available [environment variables](https://getqui.com/docs/configuration/environment/), |
| 94 | + this can be used to get an overview. |
| 95 | + ''; |
| 96 | + }; |
| 97 | + |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
| 101 | + config = mkIf cfg.enable { |
| 102 | + assertions = [ |
| 103 | + { |
| 104 | + assertion = !(cfg.settings ? sessionSecret); |
| 105 | + message = '' |
| 106 | + Session secrets should not be passed via settings, as |
| 107 | + these are stored in the world-readable nix store. |
| 108 | +
|
| 109 | + Use the secretFile option instead.''; |
| 110 | + } |
| 111 | + ]; |
| 112 | + |
| 113 | + systemd.services.qui = { |
| 114 | + description = "qui: alternative qBittorrent webUI"; |
| 115 | + after = [ "network-online.target" ]; |
| 116 | + wants = [ "network-online.target" ]; |
| 117 | + wantedBy = [ "multi-user.target" ]; |
| 118 | + |
| 119 | + serviceConfig = { |
| 120 | + Type = "simple"; |
| 121 | + User = cfg.user; |
| 122 | + Group = cfg.group; |
| 123 | + |
| 124 | + LoadCredential = "sessionSecret:${cfg.secretFile}"; |
| 125 | + Environment = [ "QUI__SESSION_SECRET_FILE=%d/sessionSecret" ]; |
| 126 | + StateDirectory = "qui"; |
| 127 | + |
| 128 | + ExecStartPre = '' |
| 129 | + ${pkgs.coreutils}/bin/install -m 600 '${configFile}' '%S/qui/config.toml' |
| 130 | + ''; |
| 131 | + ExecStart = "${getExe cfg.package} serve --config-dir %S/qui"; |
| 132 | + Restart = "on-failure"; |
| 133 | + |
| 134 | + # Based on qbittorrent and nemorosa hardening settings |
| 135 | + # Similar to what systemd hardening helper suggests |
| 136 | + CapabilityBoundingSet = ""; |
| 137 | + LockPersonality = true; |
| 138 | + MemoryDenyWriteExecute = true; |
| 139 | + NoNewPrivileges = true; |
| 140 | + PrivateDevices = true; |
| 141 | + PrivateNetwork = false; |
| 142 | + PrivateTmp = true; |
| 143 | + PrivateUsers = true; |
| 144 | + ProcSubset = "pid"; |
| 145 | + ProtectClock = true; |
| 146 | + ProtectControlGroups = true; |
| 147 | + ProtectHome = "yes"; |
| 148 | + ProtectHostname = true; |
| 149 | + ProtectKernelLogs = true; |
| 150 | + ProtectKernelModules = true; |
| 151 | + ProtectKernelTunables = true; |
| 152 | + ProtectProc = "invisible"; |
| 153 | + # This should allow for hardlinking to torrent client files |
| 154 | + ProtectSystem = "full"; |
| 155 | + RemoveIPC = true; |
| 156 | + RestrictAddressFamilies = [ |
| 157 | + "AF_INET" |
| 158 | + "AF_INET6" |
| 159 | + "AF_NETLINK" |
| 160 | + "AF_UNIX" |
| 161 | + ]; |
| 162 | + RestrictNamespaces = true; |
| 163 | + RestrictRealtime = true; |
| 164 | + RestrictSUIDSGID = true; |
| 165 | + SystemCallArchitectures = "native"; |
| 166 | + SystemCallFilter = [ "@system-service" ]; |
| 167 | + }; |
| 168 | + }; |
| 169 | + |
| 170 | + networking.firewall = mkIf cfg.openFirewall { |
| 171 | + allowedTCPPorts = [ cfg.settings.port ]; |
| 172 | + }; |
| 173 | + |
| 174 | + users = { |
| 175 | + users = mkIf (cfg.user == "qui") { |
| 176 | + qui = { |
| 177 | + group = cfg.group; |
| 178 | + description = "qui user"; |
| 179 | + isSystemUser = true; |
| 180 | + home = stateDir; |
| 181 | + }; |
| 182 | + }; |
| 183 | + |
| 184 | + groups = mkIf (cfg.group == "qui") { |
| 185 | + qui = { }; |
| 186 | + }; |
| 187 | + }; |
| 188 | + }; |
| 189 | + |
| 190 | + meta.maintainers = with maintainers; [ undefined-landmark ]; |
| 191 | +} |
0 commit comments