nix module!

This commit is contained in:
Reid 2025-05-18 23:17:32 -07:00
parent 1b62e42d49
commit 4893de0d28
Signed by: reidlab
GPG key ID: DAF5EAF6665839FD
3 changed files with 204 additions and 68 deletions

View file

@ -24,9 +24,15 @@ most of the config is talked on in [`config.example.toml`](./config.example.toml
### running ### running
just as easy as running `npm run build` and running the `dist/index.js` file with your javascript engine of choice #### regular
alternatively, use the nix flake, which has the proper `mainProgram` and default package attributes to make it easy. a nixos module is on its way!! don't fret after configuring, it's just as easy as running `npm run build` and running the `dist/index.js` file with your javascript engine of choice
#### nix
a system module is provided for your convenience, and the main output is `nixosModules.default`
after importing this module, the option `services.amdl` will show up, which is documented in [`flake.nix`](./flake.nix) somewhat well. everything under the `config` tree follows the `config.toml` well, along with everything under the `env` tree. defaults are provided for everything that isn't the private keys and client ids inside of the env section. make sure to set those!!
## limitations / the formats ## limitations / the formats

32
flake.lock generated
View file

@ -1,23 +1,5 @@
{ {
"nodes": { "nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1739866667, "lastModified": 1739866667,
@ -36,22 +18,22 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "nixpkgs": "nixpkgs",
"nixpkgs": "nixpkgs" "systems": "systems"
} }
}, },
"systems": { "systems": {
"locked": { "locked": {
"lastModified": 1681028828, "lastModified": 1689347949,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=",
"owner": "nix-systems", "owner": "nix-systems",
"repo": "default", "repo": "default-linux",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-systems", "owner": "nix-systems",
"repo": "default", "repo": "default-linux",
"type": "github" "type": "github"
} }
} }

168
flake.nix
View file

@ -2,21 +2,23 @@
description = "amdl"; description = "amdl";
inputs = { inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default-linux";
}; };
outputs = { self, nixpkgs, flake-utils }: outputs = { self, nixpkgs, systems, ... }:
flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { inherit (nixpkgs) lib;
inherit system; eachSystem = lib.genAttrs (import systems);
}; pkgsFor = eachSystem (system: import nixpkgs { inherit system; });
package = builtins.fromJSON (builtins.readFile ./package.json); package = builtins.fromJSON (builtins.readFile ./package.json);
in { in {
packages = flake-utils.lib.flattenTree rec { packages = eachSystem (system: let
inherit (lib) makeBinPath;
pkgs = pkgsFor.${system};
in rec {
default = amdl; default = amdl;
amdl = pkgs.buildNpmPackage { amdl = pkgs.buildNpmPackage rec {
pname = package.name; pname = package.name;
inherit (package) version; inherit (package) version;
@ -27,12 +29,15 @@
nativeBuildInputs = with pkgs; [ makeWrapper ]; nativeBuildInputs = with pkgs; [ makeWrapper ];
buildInputs = with pkgs; [ ffmpeg yt-dlp ];
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
mkdir -p $out mkdir -p $out
mv node_modules dist views public $out/ mv node_modules dist views public $out/
makeWrapper ${pkgs.nodejs-slim}/bin/node $out/bin/amdl \ makeWrapper ${pkgs.nodejs-slim}/bin/node $out/bin/amdl \
--prefix PATH : ${makeBinPath buildInputs} \
--add-flags "$out/dist/index.js" \ --add-flags "$out/dist/index.js" \
--set VIEWS_DIR $out/views \ --set VIEWS_DIR $out/views \
--set PUBLIC_DIR $out/public --set PUBLIC_DIR $out/public
@ -44,9 +49,12 @@
meta.mainProgram = package.name; meta.mainProgram = package.name;
}; };
}; });
devShells.default = pkgs.mkShell { devShells = eachSystem (system: let
pkgs = pkgsFor.${system};
in {
default = pkgs.mkShell {
packages = with pkgs; [ packages = with pkgs; [
nodejs nodejs
nodePackages.npm nodePackages.npm
@ -55,4 +63,144 @@
]; ];
}; };
}); });
nixosModules = rec {
default = amdl;
amdl = { config, lib, pkgs, ... }: let
inherit (pkgs) system;
inherit (lib) getExe mkEnableOption mkIf mkOption types;
cfg = config.services.amdl;
in {
options = {
services.amdl = {
enable = mkEnableOption "enable amdl";
package = mkOption {
type = types.package;
default = self.packages.${system}.amdl;
};
stateDir = mkOption {
type = types.path;
default = "/var/lib/amdl";
};
user = mkOption {
type = types.str;
default = "amdl";
};
group = mkOption {
type = types.str;
default = "amdl";
};
config = {
server = {
port = mkOption {
type = types.int;
default = 2000;
};
frontend = {
search_count = mkOption {
type = types.int;
default = 5;
};
displayed_codecs = mkOption {
type = types.listOf types.str;
default = ["aac_legacy" "aac_he_legacy"];
};
};
};
downloader = {
ffmpeg_path = mkOption {
type = types.str;
default = "ffmpeg";
};
ytdlp_path = mkOption {
type = types.str;
default = "yt-dlp";
};
cache = {
directory = mkOption {
type = types.str;
default = "cache";
};
ttl = mkOption {
type = types.int;
default = 3600;
};
};
api = {
language = mkOption {
type = types.str;
default = "en-US";
};
};
};
};
env = {
MEDIA_USER_TOKEN = mkOption {
type = types.str;
};
ITUA = mkOption {
type = types.str;
default = "US";
};
WIDEVINE_CLIENT_ID = mkOption {
type = types.str;
};
WIDEVINE_PRIVATE_KEY = mkOption {
type = types.str;
};
# do NOT include views/public directory here
# we set that in the wrapper script of the program, so it wouldn't even do anything
# probably shouldn't change it anyway... bad idea
};
};
};
config = let
toml = pkgs.formats.toml { };
in mkIf cfg.enable {
systemd.services.amdl = {
description = "amdl";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
preStart = ''
config='${cfg.stateDir}/config.toml'
cp -f '${toml.generate "config.toml" cfg.config}' "$config"
''; # TODO: symlink instead of cp, shouldn't matter for reproducibility since its preStart but whatever
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
WorkingDirectory = cfg.stateDir;
ExecStart = "${getExe cfg.package}";
Restart = "always";
RuntimeDirectory = "amdl";
RuntimeDirectoryMode = "0755";
};
environment = {
MEDIA_USER_TOKEN = cfg.env.MEDIA_USER_TOKEN;
ITUA = cfg.env.ITUA;
WIDEVINE_CLIENT_ID = cfg.env.WIDEVINE_CLIENT_ID;
WIDEVINE_PRIVATE_KEY = cfg.env.WIDEVINE_PRIVATE_KEY;
}; # TODO: write some function to do this, also is this safe??
};
users.users = mkIf (cfg.user == "amdl") {
amdl = {
home = cfg.stateDir;
group = cfg.group;
createHome = true;
isSystemUser = true;
useDefaultShell = true;
};
};
users.groups = mkIf (cfg.group == "amdl") {
amdl = { };
};
};
};
};
};
} }