Files
NixOS/modules/servers/synctube.nix
2026-02-21 20:30:16 -06:00

175 lines
4.7 KiB
Nix

{
lib,
config,
inputs,
pkgs,
...
}:
let
setup = import ../factories/mkserver.nix { inherit lib config; };
cfg = config.my.servers.synctube;
synctubeSrc = inputs.synctube;
withCommas = lib.replaceStrings [ "." ] [ "," ];
mkHaxeLib =
{
libname,
src,
version ? "git",
}:
pkgs.stdenvNoCC.mkDerivation {
name = "${libname}-${version}";
inherit src;
installPhase = ''
runHook preInstall
mkdir -p "$out/lib/haxe/${withCommas libname}/${withCommas version}"
echo -n "${version}" > "$out/lib/haxe/${withCommas libname}/.current"
cp -dpR . "$out/lib/haxe/${withCommas libname}/${withCommas version}/"
runHook postInstall
'';
};
mkHaxeGitLib =
{
libname,
owner,
repo,
rev,
hash,
}:
mkHaxeLib {
inherit libname;
src = pkgs.fetchFromGitHub {
inherit
owner
repo
rev
hash
;
};
};
haxeLibs = pkgs.symlinkJoin {
name = "synctube-haxelibs";
paths = [
(mkHaxeGitLib {
libname = "hxnodejs";
owner = "HaxeFoundation";
repo = "hxnodejs";
rev = "ba7d9a566a79b3b36b4e1f58bc8e66306983e9e4";
hash = "sha256-RZ+GoRlTvcalVsexd8Fw3/P4XZYh5IZQJNzyFkEOgAc=";
})
(mkHaxeGitLib {
libname = "hxnodejs-ws";
owner = "haxe-externs";
repo = "hxnodejs-ws";
rev = "2d0e770489abdb8d095fc4694353eaa9d6743562";
hash = "sha256-Gls4OeOCSU9Ld3rZ76086BH2TMxRvPj658sKWDP4pds=";
})
(mkHaxeGitLib {
libname = "json2object";
owner = "RblSb";
repo = "json2object";
rev = "8d949c12a93bdae010603af955a453458603cd47";
hash = "sha256-lD1IDg6xULRwisAtSZIyBju8yIa7nmM5eSezz05udaw=";
})
(mkHaxeGitLib {
libname = "hxjsonast";
owner = "elnabo";
repo = "hxjsonast";
rev = "9a9544378c9517e5cf4c5fa3c092e77bec125e64";
hash = "sha256-AvcxbksuLCKGRCHAxFYHdF7e32efBvPdXLTbHbd+q/c=";
})
(mkHaxeGitLib {
libname = "ytdlp-nodejs";
owner = "haxe-externs";
repo = "ytdlp-nodejs-externs";
rev = "dbd476ce53a0c38db36e430ded28b2740001e8aa";
hash = "sha256-mitC7wzZdJxt4B0UhMrnShhMd9Gup5G9EpSbUSJj8dA=";
})
(mkHaxeGitLib {
libname = "youtubeIFramePlayer";
owner = "haxe-externs";
repo = "youtubeIFramePlayer-externs";
rev = "8d47d71a4e3b8030a83a2e11672f120ca9a086d4";
hash = "sha256-ov0nbhEazVCcWHcxplCNdN0KRhxBKqsvFHjmGzvTvPI=";
})
(mkHaxeGitLib {
libname = "hls.js-extern";
owner = "zoldesi-andor";
repo = "hls.js-haxe-extern";
rev = "86448a1dad21e72126ce8cc078062648de8bc2c5";
hash = "sha256-MnT71PwoQd66DOzHMp8X3Yt8XulUkA1tX4dgUv7JLio=";
})
(mkHaxeGitLib {
libname = "utest";
owner = "haxe-utest";
repo = "utest";
rev = "654d6a32cc84c81972a5d3768e400d7317fff6e5";
hash = "sha256-3tdiEE1tXx5Jht+amAH3e5mKa5kAihJlklFaJLeTriE=";
})
];
};
npmDeps = pkgs.fetchNpmDeps {
src = synctubeSrc;
hash = "sha256-dIbBNvTQCv1fjboBC6Q8kGqcZbLNuGmgHw9DgOmVnTw=";
};
synctubeApp = pkgs.stdenv.mkDerivation {
pname = "synctube";
version = "1.0.0";
src = synctubeSrc;
nativeBuildInputs = [
pkgs.haxe
pkgs.neko
pkgs.nodejs
];
buildPhase = ''
runHook preBuild
export HAXELIB_PATH=${haxeLibs}/lib/haxe
export npm_config_cache=${npmDeps}
npm ci --offline --ignore-scripts
haxe build-all.hxml
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p "$out/app"
cp -R build res user default-config.json package.json package-lock.json "$out/app/"
cp -R node_modules "$out/app/"
runHook postInstall
'';
};
synctubeImage = pkgs.dockerTools.buildImage {
name = "synctube";
tag = "nix";
copyToRoot = pkgs.buildEnv {
name = "synctube-root";
paths = [
synctubeApp
pkgs.nodejs
];
pathsToLink = [ "/" ];
};
config = {
WorkingDir = "/app";
Cmd = [
"/bin/node"
"build/server.js"
];
Env = [
"NODE_ENV=production"
"NODE_PATH=/app/node_modules"
"PATH=/bin"
];
};
};
in
{
options.my.servers.synctube = setup.mkOptions "synctube" "synctube" 4200;
config = lib.mkIf (cfg.enable && config.my.secureHost) {
virtualisation.oci-containers.containers.synctube = {
image = "synctube:nix";
imageFile = synctubeImage;
ports = [ "${toString cfg.port}:4200" ];
environment.TZ = config.my.timeZone;
};
};
}