-
-
Notifications
You must be signed in to change notification settings - Fork 17.8k
nixos/qui: init service #472934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+251
−5
Merged
nixos/qui: init service #472934
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| { | ||
| config, | ||
| lib, | ||
| pkgs, | ||
| ... | ||
| }: | ||
|
|
||
| let | ||
| inherit (lib) | ||
| getExe | ||
| maintainers | ||
| mkEnableOption | ||
| mkIf | ||
| mkOption | ||
| mkPackageOption | ||
| ; | ||
| inherit (lib.types) | ||
| bool | ||
| path | ||
| port | ||
| str | ||
| submodule | ||
| ; | ||
| cfg = config.services.qui; | ||
|
|
||
| stateDir = "/var/lib/qui"; | ||
| configFormat = pkgs.formats.toml { }; | ||
| configFile = configFormat.generate "qui.toml" cfg.settings; | ||
| in | ||
| { | ||
| options = { | ||
| services.qui = { | ||
| enable = mkEnableOption "qui"; | ||
|
|
||
| package = mkPackageOption pkgs "qui" { }; | ||
|
|
||
| user = mkOption { | ||
| type = str; | ||
| default = "qui"; | ||
| description = "User to run qui as."; | ||
| }; | ||
|
|
||
| group = mkOption { | ||
| type = str; | ||
| default = "qui"; | ||
| example = "torrents"; | ||
| description = "Group to run qui as."; | ||
| }; | ||
|
|
||
| openFirewall = mkOption { | ||
| type = bool; | ||
| default = false; | ||
| description = "Whether or not to open ports in the firewall for qui."; | ||
| }; | ||
|
|
||
| secretFile = mkOption { | ||
| type = path; | ||
| example = "/run/secrets/qui-session.txt"; | ||
| description = '' | ||
| Path to a file that contains the session secret. The session secret | ||
| can be generated with `openssl rand -hex 32`. | ||
| ''; | ||
| }; | ||
|
|
||
| settings = mkOption { | ||
| default = { }; | ||
| example = { | ||
| port = 7777; | ||
| logLevel = "DEBUG"; | ||
| metricsEnabled = true; | ||
| }; | ||
| type = submodule { | ||
| freeformType = configFormat.type; | ||
| options = { | ||
| host = mkOption { | ||
| type = str; | ||
| default = "127.0.0.1"; | ||
| description = "The host address qui listens on."; | ||
| }; | ||
|
|
||
| port = mkOption { | ||
| type = port; | ||
| default = 7476; | ||
| description = "The port qui listens on."; | ||
| }; | ||
| }; | ||
| }; | ||
| description = '' | ||
| qui configuration options. | ||
|
|
||
| Refer to the [template config](https://github.com/autobrr/qui/blob/main/internal/config/config.go) | ||
| in the source code for the available options. | ||
| The documentation contains the available [environment variables](https://getqui.com/docs/configuration/environment/), | ||
| this can be used to get an overview. | ||
| ''; | ||
| }; | ||
|
|
||
| }; | ||
| }; | ||
|
|
||
| config = mkIf cfg.enable { | ||
| assertions = [ | ||
| { | ||
| assertion = !(cfg.settings ? sessionSecret); | ||
| message = '' | ||
| Session secrets should not be passed via settings, as | ||
| these are stored in the world-readable nix store. | ||
|
|
||
| Use the secretFile option instead.''; | ||
| } | ||
| ]; | ||
|
|
||
| systemd.services.qui = { | ||
| description = "qui: alternative qBittorrent webUI"; | ||
| after = [ "network-online.target" ]; | ||
| wants = [ "network-online.target" ]; | ||
| wantedBy = [ "multi-user.target" ]; | ||
|
|
||
| serviceConfig = { | ||
| Type = "simple"; | ||
| User = cfg.user; | ||
| Group = cfg.group; | ||
|
|
||
| LoadCredential = "sessionSecret:${cfg.secretFile}"; | ||
| Environment = [ "QUI__SESSION_SECRET_FILE=%d/sessionSecret" ]; | ||
| StateDirectory = "qui"; | ||
|
|
||
| ExecStartPre = '' | ||
| ${pkgs.coreutils}/bin/install -m 600 '${configFile}' '%S/qui/config.toml' | ||
| ''; | ||
| ExecStart = "${getExe cfg.package} serve --config-dir %S/qui"; | ||
| Restart = "on-failure"; | ||
|
|
||
| # Based on qbittorrent and nemorosa hardening settings | ||
| # Similar to what systemd hardening helper suggests | ||
| CapabilityBoundingSet = ""; | ||
| LockPersonality = true; | ||
| MemoryDenyWriteExecute = true; | ||
| NoNewPrivileges = true; | ||
| PrivateDevices = true; | ||
| PrivateNetwork = false; | ||
| PrivateTmp = true; | ||
| PrivateUsers = true; | ||
| ProcSubset = "pid"; | ||
| ProtectClock = true; | ||
| ProtectControlGroups = true; | ||
| ProtectHome = "yes"; | ||
| ProtectHostname = true; | ||
| ProtectKernelLogs = true; | ||
| ProtectKernelModules = true; | ||
| ProtectKernelTunables = true; | ||
| ProtectProc = "invisible"; | ||
| # This should allow for hardlinking to torrent client files | ||
| ProtectSystem = "full"; | ||
| RemoveIPC = true; | ||
| RestrictAddressFamilies = [ | ||
| "AF_INET" | ||
| "AF_INET6" | ||
| "AF_NETLINK" | ||
| "AF_UNIX" | ||
| ]; | ||
| RestrictNamespaces = true; | ||
| RestrictRealtime = true; | ||
| RestrictSUIDSGID = true; | ||
| SystemCallArchitectures = "native"; | ||
| SystemCallFilter = [ "@system-service" ]; | ||
| }; | ||
| }; | ||
|
|
||
| networking.firewall = mkIf cfg.openFirewall { | ||
| allowedTCPPorts = [ cfg.settings.port ]; | ||
| }; | ||
|
|
||
| users = { | ||
| users = mkIf (cfg.user == "qui") { | ||
| qui = { | ||
| group = cfg.group; | ||
| description = "qui user"; | ||
| isSystemUser = true; | ||
| home = stateDir; | ||
| }; | ||
| }; | ||
|
|
||
| groups = mkIf (cfg.group == "qui") { | ||
| qui = { }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| meta.maintainers = with maintainers; [ undefined-landmark ]; | ||
| } | ||
undefined-landmark marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { lib, ... }: | ||
|
|
||
| { | ||
| name = "qui"; | ||
| meta.maintainers = with lib.maintainers; [ undefined-landmark ]; | ||
|
|
||
| nodes.machine = | ||
| { pkgs, ... }: | ||
| let | ||
| # We create this secret in the Nix store (making it readable by everyone). | ||
| # DO NOT DO THIS OUTSIDE OF TESTS!! | ||
| testSecretFile = pkgs.writeText "session_secret" "not-secret"; | ||
| in | ||
| { | ||
| services.qui = { | ||
| enable = true; | ||
| secretFile = testSecretFile; | ||
| }; | ||
|
|
||
| # Use port other than default to test if settings options work. | ||
| specialisation.settingsPort.configuration = { | ||
| services.qui = { | ||
| enable = true; | ||
| secretFile = testSecretFile; | ||
| settings.port = 7777; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| testScript = | ||
| { nodes, ... }: | ||
| let | ||
| settingsPort = "${nodes.machine.system.build.toplevel}/specialisation/settingsPort"; | ||
| in | ||
| # python | ||
| '' | ||
| def test_webui(port): | ||
| machine.wait_for_unit("qui.service") | ||
| machine.wait_for_open_port(port) | ||
| machine.wait_until_succeeds(f"curl --fail http://localhost:{port}") | ||
|
|
||
| test_webui(7476) | ||
|
|
||
| machine.succeed("${settingsPort}/bin/switch-to-configuration test") | ||
| test_webui(7777) | ||
| ''; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.