221 lines
7.4 KiB
Nix
221 lines
7.4 KiB
Nix
{
|
|
description = "NodeJS application with mpv, yt-dlp, and pulseaudio dependencies";
|
|
|
|
inputs = {
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
};
|
|
|
|
outputs = { self, nixpkgs, flake-utils }:
|
|
let
|
|
# Define the NixOS module for the systemd service
|
|
nixosModule = { config, lib, pkgs, ... }:
|
|
let
|
|
cfg = config.services.queuecube;
|
|
in {
|
|
options.services.queuecube = {
|
|
enable = lib.mkEnableOption "QueueCube media player service";
|
|
|
|
port = lib.mkOption {
|
|
type = lib.types.port;
|
|
default = 3000;
|
|
description = "Port on which QueueCube will listen";
|
|
};
|
|
|
|
enable_video = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = false;
|
|
description = "Enable video playback";
|
|
};
|
|
|
|
store_path = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "/var/tmp/queuecube";
|
|
description = "Path to the store for QueueCube";
|
|
};
|
|
|
|
invidious = lib.mkOption {
|
|
type = lib.types.submodule {
|
|
options = {
|
|
enable = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = false;
|
|
description = "Enable Invidious";
|
|
};
|
|
|
|
url = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "http://invidious.nor";
|
|
description = "URL of the Invidious instance to use";
|
|
};
|
|
};
|
|
};
|
|
|
|
default = {
|
|
enable = false;
|
|
url = "http://invidious.nor";
|
|
};
|
|
};
|
|
|
|
user = lib.mkOption {
|
|
type = lib.types.str;
|
|
description = "User account under which QueueCube runs (required)";
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
users.users.${cfg.user} = {
|
|
packages = [ self.packages.${pkgs.system}.queuecube ];
|
|
};
|
|
|
|
systemd.user.services.queuecube = {
|
|
description = "QueueCube media player service";
|
|
wantedBy = [ "default.target" ];
|
|
after = [ "pipewire.service" "pipewire-pulse.service" ];
|
|
|
|
serviceConfig = {
|
|
ExecStart = "${self.packages.${pkgs.system}.queuecube}/bin/queuecube";
|
|
Restart = "on-failure";
|
|
RestartSec = 5;
|
|
|
|
# Remove all resource limits for mpv to function properly
|
|
LimitNOFILE = "infinity"; # No limit on file descriptors
|
|
LimitMEMLOCK = "infinity"; # No limit on locked memory (for real-time audio)
|
|
LimitNPROC = "infinity"; # No limit on number of processes
|
|
LimitAS = "infinity"; # No limit on address space
|
|
LimitRSS = "infinity"; # No limit on resident set size
|
|
LimitCORE = "infinity"; # Allow core dumps for debugging
|
|
LimitDATA = "infinity"; # No limit on data segment
|
|
LimitSTACK = "infinity"; # No limit on stack size
|
|
LimitCPU = "infinity"; # No limit on CPU time
|
|
LimitRTPRIO = "99"; # Allow real-time priority
|
|
LimitRTTIME = "infinity"; # No limit on real-time scheduling
|
|
|
|
# Nice level for better performance
|
|
Nice = "-10";
|
|
|
|
# Allow access to necessary devices and features
|
|
PrivateDevices = false;
|
|
ProtectHome = false;
|
|
ProtectSystem = false;
|
|
NoNewPrivileges = false;
|
|
|
|
# Environment for X11 and runtime directories
|
|
Environment = [
|
|
"DISPLAY=:0"
|
|
];
|
|
};
|
|
|
|
environment = {
|
|
PORT = toString cfg.port;
|
|
ENABLE_VIDEO = if cfg.enable_video then "1" else "0";
|
|
USE_INVIDIOUS = if cfg.invidious.enable then "1" else "0";
|
|
INVIDIOUS_BASE_URL = cfg.invidious.url;
|
|
STORE_PATH = cfg.store_path;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
in
|
|
flake-utils.lib.eachDefaultSystem (system:
|
|
let
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
# Define the package using buildNpmPackage
|
|
queuecube = pkgs.buildNpmPackage {
|
|
pname = "queuecube";
|
|
version = "0.1.0";
|
|
|
|
src = ./.;
|
|
|
|
# Skip the standard buildPhase and provide our own
|
|
dontNpmBuild = true;
|
|
buildPhase = ''
|
|
# First install all dependencies
|
|
npm install
|
|
|
|
# Then run the build with workspaces flag
|
|
npm run build --workspaces
|
|
'';
|
|
|
|
# Runtime dependencies
|
|
buildInputs = with pkgs; [
|
|
mpv
|
|
yt-dlp
|
|
pulseaudio
|
|
];
|
|
|
|
# Create a wrapper script to ensure runtime deps are available
|
|
postInstall = ''
|
|
# Create the necessary directories
|
|
mkdir -p $out/lib/node_modules/queuecube
|
|
|
|
# Copy the entire project with built files
|
|
cp -r . $out/lib/node_modules/queuecube
|
|
|
|
# Install the frontend build to the backend dist directory
|
|
mkdir -p $out/lib/node_modules/queuecube/backend/dist/
|
|
cp -r frontend/dist $out/lib/node_modules/queuecube/backend/dist/frontend
|
|
|
|
# Create bin directory if it doesn't exist
|
|
mkdir -p $out/bin
|
|
|
|
# Create executable script
|
|
cat > $out/bin/queuecube <<EOF
|
|
#!/bin/sh
|
|
exec ${pkgs.nodejs}/bin/node $out/lib/node_modules/queuecube/backend/build/server.js
|
|
EOF
|
|
|
|
# Make it executable
|
|
chmod +x $out/bin/queuecube
|
|
|
|
# Wrap the program to include runtime deps in PATH
|
|
wrapProgram $out/bin/queuecube \
|
|
--prefix PATH : ${pkgs.lib.makeBinPath [
|
|
pkgs.mpv
|
|
pkgs.yt-dlp
|
|
pkgs.pulseaudio
|
|
]}
|
|
'';
|
|
|
|
# Let buildNpmPackage handle npm package hash
|
|
npmDepsHash = "sha256-kwbWqNqji0EcBeRuc/sqQUuGQkE+P8puLTfpAyRRzgY=";
|
|
|
|
meta = with pkgs.lib; {
|
|
description = "NodeJS application with media playback capabilities";
|
|
platforms = platforms.linux;
|
|
};
|
|
};
|
|
|
|
in {
|
|
packages = {
|
|
default = queuecube;
|
|
queuecube = queuecube;
|
|
};
|
|
|
|
apps.default = {
|
|
type = "app";
|
|
program = "${queuecube}/bin/queuecube";
|
|
};
|
|
|
|
# Development environment
|
|
devShells.default = pkgs.mkShell {
|
|
buildInputs = with pkgs; [
|
|
nodejs_20
|
|
nodePackages.npm
|
|
mpv
|
|
yt-dlp
|
|
pulseaudio
|
|
];
|
|
};
|
|
|
|
# Add a basic check to verify the package builds
|
|
checks.queuecube = queuecube;
|
|
}
|
|
) // {
|
|
# Export the NixOS module
|
|
nixosModules.default = nixosModule;
|
|
nixosModules.queuecube = nixosModule;
|
|
};
|
|
}
|