|
9 | 9 |
|
10 | 10 | enabledServices = lib.attrValues cfg.services; |
11 | 11 |
|
| 12 | + # Helper to convert header/query names to valid bash variable names |
| 13 | + # Converts to uppercase and replaces hyphens with underscores |
| 14 | + sanitizeVarName = name: lib.replaceStrings ["-"] ["_"] (lib.toUpper name); |
| 15 | + |
12 | 16 | restishWrapper = pkgs.writeShellScriptBin "restish" '' |
13 | 17 | set -euo pipefail |
14 | 18 |
|
|
20 | 24 | mkdir -p "$RESTISH_CONFIG_DIR" |
21 | 25 |
|
22 | 26 | ${lib.optionalString ((lib.length enabledServices) > 0) '' |
| 27 | + # Helper function to read value from file if it exists, otherwise use as-is |
| 28 | + read_value() { |
| 29 | + local value="$1" |
| 30 | + if [ -f "$value" ]; then |
| 31 | + cat "$value" |
| 32 | + else |
| 33 | + echo "$value" |
| 34 | + fi |
| 35 | + } |
| 36 | +
|
| 37 | + # Build JSON config using jq |
| 38 | + CONFIG_FILE="$RESTISH_CONFIG_DIR/apis.json" |
23 | 39 | ${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: svc: '' |
24 | | - if [ -f "${svc.apiKeyPath}" ]; then |
25 | | - export ${lib.toUpper name}_API_KEY=$(cat "${svc.apiKeyPath}") |
26 | | - else |
27 | | - echo "Warning: API key file not found: ${svc.apiKeyPath}" >&2 |
28 | | - fi |
| 40 | + ${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: '' |
| 41 | + export HEADER_${sanitizeVarName name}_${sanitizeVarName k}=$(read_value "${v}") |
| 42 | + '') |
| 43 | + svc.headers)} |
| 44 | + ${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: '' |
| 45 | + export QUERY_${sanitizeVarName name}_${sanitizeVarName k}=$(read_value "${v}") |
| 46 | + '') |
| 47 | + svc.query)} |
29 | 48 | '') |
30 | 49 | cfg.services)} |
31 | 50 |
|
32 | | - CONFIG_FILE="$RESTISH_CONFIG_DIR/apis.json" |
33 | 51 | cat > "$CONFIG_FILE" <<'EOF' |
34 | 52 | { |
35 | | - ${lib.concatStringsSep ",\n " (lib.mapAttrsToList (name: svc: '' |
36 | | - "${name}": { |
37 | | - "base": "${svc.baseUrl}", |
38 | | - "profiles": { |
39 | | - "default": { |
40 | | - "headers": { |
41 | | - "X-Api-Key": "''${${lib.toUpper name}_API_KEY}" |
| 53 | + ${lib.concatStringsSep ",\n" (lib.mapAttrsToList (name: svc: let |
| 54 | + headerPairs = lib.mapAttrsToList (k: v: ''"${k}": "$HEADER_${sanitizeVarName name}_${sanitizeVarName k}"'') svc.headers; |
| 55 | + queryPairs = lib.mapAttrsToList (k: v: ''"${k}": "$QUERY_${sanitizeVarName name}_${sanitizeVarName k}"'') svc.query; |
| 56 | + profileParts = |
| 57 | + (lib.optionals (svc.headers != {}) [''"headers": { ${lib.concatStringsSep ", " headerPairs} }'']) |
| 58 | + ++ (lib.optionals (svc.query != {}) [''"query": { ${lib.concatStringsSep ", " queryPairs} }'']); |
| 59 | + in '' |
| 60 | + "${name}": { |
| 61 | + "base": "${svc.baseUrl}", |
| 62 | + "profiles": { |
| 63 | + "default": { |
| 64 | + ${lib.concatStringsSep ",\n" profileParts} |
42 | 65 | } |
43 | 66 | } |
44 | | - } |
45 | | - }'') |
| 67 | + }'') |
46 | 68 | cfg.services)} |
47 | 69 | } |
48 | 70 | EOF |
49 | 71 |
|
| 72 | + # Substitute environment variables |
50 | 73 | TEMP_CONFIG=$(mktemp) |
51 | 74 | ${pkgs.envsubst}/bin/envsubst < "$CONFIG_FILE" > "$TEMP_CONFIG" |
52 | 75 | mv "$TEMP_CONFIG" "$CONFIG_FILE" |
|
65 | 88 | example = "http://127.0.0.1:8989/api/v3"; |
66 | 89 | }; |
67 | 90 |
|
68 | | - apiKeyPath = mkOption { |
69 | | - type = types.path; |
70 | | - description = "Path to the API key file"; |
| 91 | + headers = mkOption { |
| 92 | + type = types.attrsOf types.str; |
| 93 | + default = {}; |
| 94 | + description = '' |
| 95 | + HTTP headers to send with requests. |
| 96 | + Values can be either plain strings or file paths. |
| 97 | + If a value is a valid file path, it will be read at runtime. |
| 98 | + ''; |
| 99 | + example = { |
| 100 | + "X-Api-Key" = "/run/secrets/sonarr/api_key"; |
| 101 | + "User-Agent" = "nixflix"; |
| 102 | + }; |
| 103 | + }; |
| 104 | + |
| 105 | + query = mkOption { |
| 106 | + type = types.attrsOf types.str; |
| 107 | + default = {}; |
| 108 | + description = '' |
| 109 | + Query parameters to include in requests. |
| 110 | + Values can be either plain strings or file paths. |
| 111 | + If a value is a valid file path, it will be read at runtime. |
| 112 | + ''; |
| 113 | + example = { |
| 114 | + "apikey" = "/run/secrets/sabnzbd/api_key"; |
| 115 | + "output" = "json"; |
| 116 | + }; |
71 | 117 | }; |
72 | 118 | }; |
73 | 119 | }); |
|
0 commit comments