|
| 1 | +{ |
| 2 | + lib, |
| 3 | + pkgs, |
| 4 | + serviceName, |
| 5 | +}: |
| 6 | +with lib; let |
| 7 | + capitalizedName = lib.toUpper (builtins.substring 0 1 serviceName) + builtins.substring 1 (-1) serviceName; |
| 8 | + |
| 9 | + defaultDelayProfile = { |
| 10 | + enableUsenet = true; |
| 11 | + enableTorrent = true; |
| 12 | + preferredProtocol = "usenet"; |
| 13 | + usenetDelay = 0; |
| 14 | + torrentDelay = 0; |
| 15 | + bypassIfHighestQuality = true; |
| 16 | + bypassIfAboveCustomFormatScore = false; |
| 17 | + minimumCustomFormatScore = 0; |
| 18 | + order = 2147483647; |
| 19 | + tags = []; |
| 20 | + id = 1; |
| 21 | + }; |
| 22 | +in { |
| 23 | + options = mkOption { |
| 24 | + type = types.listOf (types.submodule { |
| 25 | + options = { |
| 26 | + id = mkOption { |
| 27 | + type = types.int; |
| 28 | + description = "Unique identifier for the delay profile"; |
| 29 | + }; |
| 30 | + enableUsenet = mkOption { |
| 31 | + type = types.bool; |
| 32 | + default = true; |
| 33 | + description = "Enable Usenet protocol for this profile"; |
| 34 | + }; |
| 35 | + enableTorrent = mkOption { |
| 36 | + type = types.bool; |
| 37 | + default = true; |
| 38 | + description = "Enable Torrent protocol for this profile"; |
| 39 | + }; |
| 40 | + preferredProtocol = mkOption { |
| 41 | + type = types.enum ["usenet" "torrent"]; |
| 42 | + default = "usenet"; |
| 43 | + description = "Preferred download protocol when both are available"; |
| 44 | + }; |
| 45 | + usenetDelay = mkOption { |
| 46 | + type = types.int; |
| 47 | + default = 0; |
| 48 | + description = "Delay in minutes before grabbing a Usenet release"; |
| 49 | + }; |
| 50 | + torrentDelay = mkOption { |
| 51 | + type = types.int; |
| 52 | + default = 360; |
| 53 | + description = "Delay in minutes before grabbing a Torrent release"; |
| 54 | + }; |
| 55 | + bypassIfHighestQuality = mkOption { |
| 56 | + type = types.bool; |
| 57 | + default = true; |
| 58 | + description = "Bypass delay if release is the highest quality available"; |
| 59 | + }; |
| 60 | + bypassIfAboveCustomFormatScore = mkOption { |
| 61 | + type = types.bool; |
| 62 | + default = false; |
| 63 | + description = "Bypass delay if custom format score is above minimum"; |
| 64 | + }; |
| 65 | + minimumCustomFormatScore = mkOption { |
| 66 | + type = types.int; |
| 67 | + default = 0; |
| 68 | + description = "Minimum custom format score to bypass delay"; |
| 69 | + }; |
| 70 | + order = mkOption { |
| 71 | + type = types.int; |
| 72 | + default = 50; |
| 73 | + description = "Order/priority of this delay profile (lower values = higher priority)"; |
| 74 | + }; |
| 75 | + tags = mkOption { |
| 76 | + type = types.listOf types.int; |
| 77 | + default = []; |
| 78 | + description = "List of tag IDs this delay profile applies to (empty = applies to all)"; |
| 79 | + }; |
| 80 | + }; |
| 81 | + }); |
| 82 | + default = [defaultDelayProfile]; |
| 83 | + defaultText = literalExpression '' |
| 84 | + [ |
| 85 | + { |
| 86 | + enableUsenet = true; |
| 87 | + enableTorrent = true; |
| 88 | + preferredProtocol = "usenet"; |
| 89 | + usenetDelay = 0; |
| 90 | + torrentDelay = 0; |
| 91 | + bypassIfHighestQuality = true; |
| 92 | + bypassIfAboveCustomFormatScore = false; |
| 93 | + minimumCustomFormatScore = 0; |
| 94 | + order = 2147483647; |
| 95 | + tags = []; |
| 96 | + id = 1; |
| 97 | + }; |
| 98 | + ] |
| 99 | + ''; |
| 100 | + description = '' |
| 101 | + List of delay profiles to configure via the API /delayprofile endpoint. |
| 102 | +
|
| 103 | + Profiles are created/updated in id order. If no profile with id=1 is provided, |
| 104 | + a default profile will be added automatically. |
| 105 | + ''; |
| 106 | + }; |
| 107 | + |
| 108 | + mkService = serviceConfig: { |
| 109 | + description = "Configure ${serviceName} delay profiles via API"; |
| 110 | + after = ["${serviceName}-config.service"]; |
| 111 | + wantedBy = ["multi-user.target"]; |
| 112 | + |
| 113 | + serviceConfig = { |
| 114 | + Type = "oneshot"; |
| 115 | + RemainAfterExit = true; |
| 116 | + }; |
| 117 | + |
| 118 | + script = let |
| 119 | + # Auto-merge default profile (id=1) if not present in user config |
| 120 | + userProfileIds = map (p: p.id) serviceConfig.delayProfiles; |
| 121 | + hasDefaultProfile = elem 1 userProfileIds; |
| 122 | + mergedProfiles = |
| 123 | + if hasDefaultProfile |
| 124 | + then serviceConfig.delayProfiles |
| 125 | + else [defaultDelayProfile] ++ serviceConfig.delayProfiles; |
| 126 | + |
| 127 | + # Sort profiles by id to ensure proper creation order |
| 128 | + sortedProfiles = sort (a: b: a.id < b.id) mergedProfiles; |
| 129 | + in '' |
| 130 | + set -eu |
| 131 | +
|
| 132 | + # Read API key secret |
| 133 | + API_KEY=$(cat ${serviceConfig.apiKeyPath}) |
| 134 | +
|
| 135 | + BASE_URL="http://127.0.0.1:${builtins.toString serviceConfig.hostConfig.port}${serviceConfig.hostConfig.urlBase}/api/${serviceConfig.apiVersion}" |
| 136 | +
|
| 137 | + # Fetch existing delay profiles |
| 138 | + echo "Fetching existing delay profiles..." |
| 139 | + DELAY_PROFILES=$(${pkgs.curl}/bin/curl -sSf -H "X-Api-Key: $API_KEY" "$BASE_URL/delayprofile" 2>/dev/null) |
| 140 | +
|
| 141 | + # Build list of configured profile IDs |
| 142 | + CONFIGURED_IDS=$(cat <<'EOF' |
| 143 | + ${builtins.toJSON (map (p: p.id) sortedProfiles)} |
| 144 | + EOF |
| 145 | + ) |
| 146 | +
|
| 147 | + # Delete delay profiles that are not in the configuration |
| 148 | + echo "Removing delay profiles not in configuration..." |
| 149 | + echo "$DELAY_PROFILES" | ${pkgs.jq}/bin/jq -r '.[] | @json' | while IFS= read -r profile; do |
| 150 | + PROFILE_ID=$(echo "$profile" | ${pkgs.jq}/bin/jq -r '.id') |
| 151 | +
|
| 152 | + if ! echo "$CONFIGURED_IDS" | ${pkgs.jq}/bin/jq -e --argjson id "$PROFILE_ID" 'index($id)' >/dev/null 2>&1; then |
| 153 | + echo "Deleting delay profile not in config (ID: $PROFILE_ID)" |
| 154 | + ${pkgs.curl}/bin/curl -sSf -X DELETE \ |
| 155 | + -H "X-Api-Key: $API_KEY" \ |
| 156 | + "$BASE_URL/delayprofile/$PROFILE_ID" >/dev/null 2>&1 || echo "Warning: Failed to delete delay profile $PROFILE_ID (may be in use)" |
| 157 | + fi |
| 158 | + done |
| 159 | +
|
| 160 | + ${concatMapStringsSep "\n" (profileConfig: let |
| 161 | + profileJson = builtins.toJSON profileConfig; |
| 162 | + profileId = toString profileConfig.id; |
| 163 | + in '' |
| 164 | + echo "Processing delay profile (ID: ${profileId})..." |
| 165 | +
|
| 166 | + EXISTING_PROFILE=$(echo "$DELAY_PROFILES" | ${pkgs.jq}/bin/jq -r '.[] | select(.id == ${profileId}) | @json' || echo "") |
| 167 | +
|
| 168 | + if [ -n "$EXISTING_PROFILE" ]; then |
| 169 | + echo "Delay profile ${profileId} already exists, updating..." |
| 170 | + ${pkgs.curl}/bin/curl -sSf -X PUT \ |
| 171 | + -H "X-Api-Key: $API_KEY" \ |
| 172 | + -H "Content-Type: application/json" \ |
| 173 | + -d '${profileJson}' \ |
| 174 | + "$BASE_URL/delayprofile/${profileId}" > /dev/null |
| 175 | + echo "Delay profile ${profileId} updated" |
| 176 | + else |
| 177 | + echo "Delay profile ${profileId} does not exist, creating..." |
| 178 | + ${pkgs.curl}/bin/curl -sSf -X POST \ |
| 179 | + -H "X-Api-Key: $API_KEY" \ |
| 180 | + -H "Content-Type: application/json" \ |
| 181 | + -d '${profileJson}' \ |
| 182 | + "$BASE_URL/delayprofile" > /dev/null |
| 183 | + echo "Delay profile ${profileId} created" |
| 184 | + fi |
| 185 | + '') |
| 186 | + sortedProfiles} |
| 187 | +
|
| 188 | + echo "${capitalizedName} delay profiles configuration complete" |
| 189 | + ''; |
| 190 | + }; |
| 191 | +} |
0 commit comments