Skip to content

Commit

Permalink
Fixed cross-seed
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus-kirk committed Mar 3, 2024
1 parent d396bd3 commit cc45972
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 118 deletions.
20 changes: 3 additions & 17 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,22 @@
flake-root.flakeModule
treefmt-nix.flakeModule
devshell.flakeModule
#vpnconfinement.nixosModules.default
];
systems = [
"x86_64-linux"
];

flake = {
nixosModules = rec {
#vpnconfinement = vpnconfinement.nixosModules.default;
nixarr = (import ./nixarr vpnconfinement);
#imports = [ vpnconfinement.nixosModules.default ];
#nixarr.imports = [ vpnconfinement ];
default = nixarr;
};
};

perSystem = {
config,
pkgs,
lib,
...
}: {
treefmt.config = {
Expand Down
2 changes: 1 addition & 1 deletion mkDocs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
config._module.check = false;
}
./nixarr
./nixarr/nixarr.nix
];
};
optionsDocNixos = nixosOptionsDoc {
Expand Down
7 changes: 4 additions & 3 deletions nixarr/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vpnconfinement: {
vpnconfinement:
{
config,
lib,
pkgs,
Expand Down Expand Up @@ -66,7 +67,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "/data/.state";
default = "/data/.state/nixarr";
description = ''
The location of the state directory for the services.
'';
Expand Down Expand Up @@ -223,7 +224,7 @@ in {
./dnsleaktest.sh
'' + (if cfg.vpn.vpnTestService.port != null then ''
echo "starting netcat on port ${builtins.toString cfg.vpn.vpnTestService.port}:"
nc -vnlp ${builtins.toString cfg.vpn.vpnTestService.port}
nc -vnlpu ${builtins.toString cfg.vpn.vpnTestService.port}
'' else "");
};
in "${vpn-test}/bin/vpn-test";
Expand Down
2 changes: 1 addition & 1 deletion nixarr/jellyfin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ in with lib; {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/jellyfin";
default = "${nixarr.stateDir}/jellyfin";
description = "The state directory for Jellyfin.";
};

Expand Down
2 changes: 1 addition & 1 deletion nixarr/lidarr/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/lidarr";
default = "${nixarr.stateDir}/lidarr";
description = "The state directory for Lidarr";
};

Expand Down
4 changes: 1 addition & 3 deletions nixarr/prowlarr/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/prowlarr";
default = "${nixarr.stateDir}/prowlarr";
description = "The state directory for Prowlarr.";
};

Expand Down Expand Up @@ -48,8 +48,6 @@ in {
"d '${cfg.stateDir}' 0700 prowlarr root - -"
];

users.groups.prowlarr = {};

util-nixarr.services.prowlarr = {
enable = true;
dataDir = cfg.stateDir;
Expand Down
4 changes: 3 additions & 1 deletion nixarr/prowlarr/prowlarr-module/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ in {
};
};

users.groups = mkIf (cfg.group == "prowlarr") {};
users.groups = mkIf (cfg.group == "prowlarr") {
prowlarr = { };
};
};
}
2 changes: 1 addition & 1 deletion nixarr/radarr/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/radarr";
default = "${nixarr.stateDir}/radarr";
description = "The state directory for radarr.";
};

Expand Down
2 changes: 1 addition & 1 deletion nixarr/readarr/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/readarr";
default = "${nixarr.stateDir}/readarr";
description = "The state directory for Readarr";
};

Expand Down
54 changes: 50 additions & 4 deletions nixarr/transmission/cross-seed/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,37 @@ with lib; let
cfg = config.util-nixarr.services.cross-seed;
settingsFormat = pkgs.formats.json {};
settingsFile = settingsFormat.generate "settings.json" cfg.settings;
cross-seedPkg = import ../../../pkgs/cross-seed { inherit (pkgs) stdenv lib fetchFromGitHub; };
cross-seedPkg = pkgs.callPackage ../../../pkgs/cross-seed {};
configJs = pkgs.writeText "config.js" ''
// Loads a json.config
"use strict";
const fs = require('fs');
const jsonPath = '${cfg.dataDir}/config.json'
// Synchronously read the JSON-configuration file
const configFileContent = fs.readFileSync(jsonPath, { encoding: 'utf8' });
// Parse the JSON content into a JavaScript object
let config = JSON.parse(configFileContent);
// Function to recursively replace null values with undefined
/*
function replaceNullWithUndefined(obj) {
Object.keys(obj).forEach(key => {
if (obj[key] === null) {
obj[key] = undefined;
} else if (typeof obj[key] === 'object') {
replaceNullWithUndefined(obj[key]);
}
});
}
replaceNullWithUndefined(config);
*/
// Export the configuration object
module.exports = config;
'';
in {
options = {
util-nixarr.services.cross-seed = {
Expand Down Expand Up @@ -52,9 +82,23 @@ in {
};

config = mkIf cfg.enable {
assertions = [
{
assertion = cfg.enable -> cfg.settings.outputDir != null;
message = ''
The settings.outputDir must be set if cross-seed is enabled.
'';
}
];

systemd.tmpfiles.rules = [
"L+ '${cfg.dataDir}'/config.js - - - - ${configJs}"
"d '${cfg.dataDir}' 0700 ${cfg.user} ${cfg.group} - -"
];
] ++ (
if cfg.settings.outputDir != null then
[ "d '${cfg.settings.outputDir}' 0755 ${cfg.user} ${cfg.group} - -" ]
else []
);

systemd.services.cross-seed = {
description = "cross-seed";
Expand All @@ -73,7 +117,7 @@ in {
Type = "simple";
User = cfg.user;
Group = cfg.group;
ExecStart = "${getExe cross-seedPkg} daemon";
ExecStart = "${cross-seedPkg}/bin/cross-seed daemon";
Restart = "on-failure";
};
};
Expand All @@ -85,6 +129,8 @@ in {
};
};

users.groups = mkIf (cfg.group == "cross-seed") {};
users.groups = mkIf (cfg.group == "cross-seed") {
cross-seed = { };
};
};
}
49 changes: 36 additions & 13 deletions nixarr/transmission/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ with lib; let
nixarr = config.nixarr;
cfg-cross-seed = config.nixarr.transmission.privateTrackers.cross-seed;
transmissionCrossSeedScript = with builtins; pkgs.writeShellApplication {
name = "mk-cross-seed-credentials";
name = "transmission-cross-seed-script";

runtimeInputs = with pkgs; [ curl ];

text = ''
curl -XPOST http://localhost:2468/api/webhook?apikey=YOUR_API_KEY --data-urlencode "infoHash=$TR_TORRENT_HASH"
PROWLARR_API_KEY=$(cat prowlarr-api-key)
curl -XPOST http://localhost:2468/api/webhook?apikey="$PROWLARR_API_KEY" --data-urlencode "infoHash=$TR_TORRENT_HASH"
'';
};
importProwlarrApi = with builtins; pkgs.writeShellApplication {
name = "import-prowlarr-api";

runtimeInputs = with pkgs; [ yq ];

text = ''
touch ${cfg.stateDir}/prowlarr-api-key
chmod 400 ${cfg.stateDir}/prowlarr-api-key
chown torrenter ${cfg.stateDir}/prowlarr-api-key
xq -r '.Config.ApiKey' "${nixarr.prowlarr.stateDir}/config.xml" > "${cfg.stateDir}/prowlarr-api-key"
'';
};
mkCrossSeedCredentials = with builtins; pkgs.writeShellApplication {
Expand All @@ -24,14 +37,17 @@ with lib; let

text =
"INDEX_LINKS=("
+ strings.concatMapStringsSep " " toString cfg.privateTrackers.cross-seed.indexIds
+ (strings.concatMapStringsSep " " toString cfg.privateTrackers.cross-seed.indexIds)
+ ")"
''
+ "\n"
+ ''
TMP_JSON=$(mktemp)
CRED_FILE="/run/secrets/cross-seed/credentialsFile.json"
PROWLARR_API_KEY=$(xq '.Config.ApiKey' "${nixarr.prowlarr.stateDir}/config.xml")
CRED_DIR=$(dirname "$filePath")
PROWLARR_API_KEY=$(xq -r '.Config.ApiKey' "${nixarr.prowlarr.stateDir}/config.xml")
# shellcheck disable=SC2034
CRED_DIR=$(dirname "$CRED_FILE")
mkdir -p "$CRED_DIR"
echo '{}' > "$CRED_FILE"
chmod 400 "$CRED_FILE"
chown "${config.util-nixarr.services.cross-seed.user}" "$CRED_FILE"
Expand All @@ -49,7 +65,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/transmission";
default = "${nixarr.stateDir}/transmission";
description = ''
The state directory for Transmission.
'';
Expand Down Expand Up @@ -103,7 +119,7 @@ in {

stateDir = mkOption {
type = types.path;
default = "${nixarr.stateDir}/nixarr/cross-seed";
default = "${nixarr.stateDir}/cross-seed";
description = ''
The state directory for Transmission.
'';
Expand Down Expand Up @@ -212,8 +228,8 @@ in {
#group = "media";
settings = {
torrentDir = "${nixarr.mediaDir}/torrents";
outputDir = "${nixarr.mediaDir}/torrents/cross-seed";
transmissionRpcUrl = "http://transmission:${builtins.toString cfg.uiPort}/transmission/rpc";
outputDir = "${nixarr.mediaDir}/torrents/.cross-seed";
transmissionRpcUrl = "http://localhost:${builtins.toString cfg.uiPort}/transmission/rpc";
rssCadence = "20 minutes";

# Enable infrequent periodic searches
Expand All @@ -224,11 +240,16 @@ in {
};
# Run as root in case that the cfg.credentialsFile is not readable by cross-seed
systemd.services.cross-seed.serviceConfig = mkIf cfg-cross-seed.enable {
ExecStartPre = [(mkBefore
("+" + (getExe mkCrossSeedCredentials))
ExecStartPre = mkBefore [(
"+" + "${mkCrossSeedCredentials}/bin/mk-cross-seed-credentials"
)];
};

systemd.services.transmission.serviceConfig = mkIf cfg-cross-seed.enable {
ExecStartPre = mkBefore [(
"+" + "${importProwlarrApi}/bin/import-prowlarr-api"
)];
};
services.transmission = {
enable = true;
user = "torrenter";
Expand Down Expand Up @@ -270,7 +291,9 @@ in {
anti-brute-force-threshold = 10;

script-torrent-done-enabled = cfg-cross-seed.enable;
script-torrent-done-filename = if cfg-cross-seed.enable then transmissionCrossSeedScript else null;
script-torrent-done-filename = if cfg-cross-seed.enable then
"${transmissionCrossSeedScript}/bin/transmission-cross-seed-script"
else null;

message-level =
if cfg.messageLevel == "none"
Expand Down
Loading

0 comments on commit cc45972

Please sign in to comment.