diff --git a/configurations/hp500281.nix b/configurations/hp500281.nix index a6bc9e9f..9d54ac74 100644 --- a/configurations/hp500281.nix +++ b/configurations/hp500281.nix @@ -8,6 +8,7 @@ in ../profiles/graphical ../profiles/misc ../profiles/misc/plex.nix + ../profiles/misc/torrent.nix ]; boot.initrd.availableKernelModules = [ diff --git a/modules/services/torrent/qbittorrent.nix b/modules/services/torrent/qbittorrent.nix new file mode 100644 index 00000000..4c3d1ad5 --- /dev/null +++ b/modules/services/torrent/qbittorrent.nix @@ -0,0 +1,106 @@ +{ config, lib, pkgs, ... }: +with lib; +let + cfg = config.services.qbittorrent; + configDir = "${cfg.dataDir}/.config"; + openFilesLimit = 4096; +in +{ + options.services.qbittorrent = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + Run qBittorrent headlessly as systemwide daemon + ''; + }; + + dataDir = mkOption { + type = types.path; + default = "/var/lib/qbittorrent"; + description = '' + The directory where qBittorrent will create files. + ''; + }; + + user = mkOption { + type = types.str; + default = "qbittorrent"; + description = '' + User account under which qBittorrent runs. + ''; + }; + + group = mkOption { + type = types.str; + default = "qbittorrent"; + description = '' + Group under which qBittorrent runs. + ''; + }; + + port = mkOption { + type = types.port; + default = 8080; + description = '' + qBittorrent web UI port. + ''; + }; + + openFilesLimit = mkOption { + default = openFilesLimit; + description = '' + Number of files to allow qBittorrent to open. + ''; + }; + }; + + config = mkIf cfg.enable { + + environment.systemPackages = [ pkgs.qbittorrent ]; + + nixpkgs.overlays = [ + ( + self: super: { + qbittorrent = super.qbittorrent.override { guiSupport = false; }; + } + ) + ]; + + systemd.services.qbittorrent = { + after = [ "network.target" ]; + description = "qBittorrent Daemon"; + wantedBy = [ "multi-user.target" ]; + path = [ pkgs.qbittorrent ]; + serviceConfig = { + ExecStart = '' + ${pkgs.qbittorrent}/bin/qbittorrent-nox \ + --profile=${configDir} \ + --webui-port=${toString cfg.port} + ''; + # To prevent "Quit & shutdown daemon" from working; we want systemd to + # manage it! + Restart = "on-success"; + User = cfg.user; + Group = cfg.group; + UMask = "0002"; + LimitNOFILE = cfg.openFilesLimit; + }; + }; + + users.users = mkIf (cfg.user == "qbittorrent") { + qbittorrent = { + group = cfg.group; + home = cfg.dataDir; + createHome = true; + description = "qBittorrent Daemon user"; + }; + }; + + users.groups = mkIf (cfg.group == "qbittorrent") { + qbittorrent = { + gid = null; + }; + }; + }; +} diff --git a/profiles/misc/torrent.nix b/profiles/misc/torrent.nix new file mode 100644 index 00000000..594adc66 --- /dev/null +++ b/profiles/misc/torrent.nix @@ -0,0 +1,21 @@ +{ config, lib, ... }: +let + inherit (config.services.qbittorrent) port; + inherit (lib) mkAfter; +in +{ + imports = [ ../../modules/services/torrent/qbittorrent.nix ]; + services.qbittorrent.enable = true; + users.users.nrd.extraGroups = [ "qbittorrent" ]; + + + environment.etc."xdg/qutebrowser/config.py".text = mkAfter '' + c.url.searchengines['to'] = 'https://torrentz2.eu/search?f={}' + + config.bind(',t', """hint all spawn curl -X POST\ + -F "urls={hint-url}"\ + -F "sequentialDownload=true"\ + http://localhost:${toString port}/api/v2/torrents/add""" + ) + ''; +}