Skip to content

Commit 33ff4ee

Browse files
committed
add api key configuration
1 parent 73f9abf commit 33ff4ee

4 files changed

Lines changed: 94 additions & 2 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}:
7+
with lib; let
8+
inherit (config) nixflix;
9+
cfg = config.nixflix.jellyfin;
10+
in {
11+
config = mkIf (nixflix.enable && cfg.enable) {
12+
systemd.services.jellyfin-api-keys = {
13+
description = "Jellyfin API Keys Initialization";
14+
after = ["jellyfin.service"];
15+
wants = ["jellyfin.service"];
16+
wantedBy = ["multi-user.target"];
17+
18+
serviceConfig = {
19+
Type = "oneshot";
20+
User = cfg.user;
21+
Group = cfg.group;
22+
RemainAfterExit = true;
23+
};
24+
25+
script = let
26+
dbPath = "${cfg.dataDir}/data/library.db";
27+
in ''
28+
set -eu
29+
30+
# Wait for database to be created by Jellyfin
31+
timeout=60
32+
elapsed=0
33+
while [ ! -f "${dbPath}" ] && [ $elapsed -lt $timeout ]; do
34+
echo "Waiting for Jellyfin database to be created..."
35+
sleep 2
36+
elapsed=$((elapsed + 2))
37+
done
38+
39+
if [ ! -f "${dbPath}" ]; then
40+
echo "ERROR: Jellyfin database not found at ${dbPath} after $timeout seconds"
41+
exit 1
42+
fi
43+
44+
# Create temporary file for SQL commands
45+
dbcmds=$(${pkgs.coreutils}/bin/mktemp)
46+
trap "${pkgs.coreutils}/bin/rm -f $dbcmds" EXIT
47+
48+
echo "BEGIN TRANSACTION;" > "$dbcmds"
49+
50+
${concatStringsSep "\n" (
51+
mapAttrsToList (
52+
appName: keyPath: ''
53+
echo "REPLACE INTO ApiKeys (DateCreated, DateLastActivity, Name, AccessToken) VALUES(strftime('%Y-%m-%d %H:%M:%S', 'now'), strftime('%Y-%m-%d %H:%M:%S', 'now'), '${appName}', '$(${pkgs.coreutils}/bin/cat "${keyPath}")');" >> "$dbcmds"
54+
''
55+
)
56+
cfg.apikeys
57+
)}
58+
59+
echo "COMMIT;" >> "$dbcmds"
60+
61+
# Execute SQL commands
62+
${pkgs.sqlite}/bin/sqlite3 "${dbPath}" < "$dbcmds"
63+
64+
echo "API keys initialized successfully"
65+
'';
66+
};
67+
};
68+
}

modules/jellyfin/default.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ with lib; let
1717
in {
1818
imports = [
1919
./options
20+
./apiKeysService.nix
2021
];
2122

2223
config = mkIf (nixflix.enable && cfg.enable) {
@@ -25,6 +26,10 @@ in {
2526
assertion = cfg.vpn.enable -> config.nixflix.mullvad.enable;
2627
message = "Cannot enable VPN routing for Jellyfin (nixflix.jellyfin.vpn.enable = true) when Mullvad VPN is disabled. Please set nixflix.mullvad.enable = true.";
2728
}
29+
{
30+
assertion = cfg.apikeys ? default;
31+
message = "Jellyfin requires at least a 'default' API key. Please configure nixflix.jellyfin.apikeys.default.";
32+
}
2833
];
2934

3035
users.users.${cfg.user} = {

modules/jellyfin/options/default.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,24 @@ in {
9999
'';
100100
};
101101
};
102+
103+
apikeys = mkOption {
104+
description = ''
105+
API keys for Jellyfin.
106+
Each attribute name is the API key name, and the value is a path to a file containing the API key.
107+
The key must be a random GUID without dashes. To generate one, run:
108+
```uuidgen -r | sed 's/-//g'```
109+
110+
Note: At least the 'default' API key must be configured.
111+
'';
112+
default = {};
113+
type = attrsOf path;
114+
example = literalExpression ''
115+
{
116+
default = config.sops.secrets.jellyfin-default-key.path;
117+
Jellyseerr = config.sops.secrets.jellyfin-jellyseerr-key.path;
118+
}
119+
'';
120+
};
102121
};
103122
}

modules/jellyfin/options/users.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ with lib; let
258258
example = 5;
259259
default = 0;
260260
};
261-
maxParentalAgeRating = mkOption {
261+
maxParentalRatingSubScore = mkOption {
262262
type = with types; nullOr int;
263263
default = null;
264264
};
@@ -371,7 +371,7 @@ in {
371371
example = {
372372
Admin = {
373373
password = "123";
374-
maxParentalAgeRating = 12;
374+
maxParentalRatingSubScore = 12;
375375
permissions = {
376376
isAdministrator = true;
377377
};

0 commit comments

Comments
 (0)