Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NixOS module #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
description = "The Heart of YouCube. See the documentation at https://youcube.madefor.cc.";

inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, utils, }:
utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
]
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
version = builtins.substring 0 8 self.lastModifiedDate;
deps = with pkgs.python3Packages; [
sanic
yt-dlp
ujson
spotipy
];
in
{
packages = {
default = pkgs.python3Packages.buildPythonApplication {
pname = "youcube";
inherit version;
src = ./.;
format = "pyproject";
patchPhase = "rm src/requirements.txt";
buildInputs = [ pkgs.python3Packages.setuptools ];
nativeBuildInputs = [ pkgs.makeWrapper ];
propagatedBuildInputs = deps;
pythonImportsCheck = [ "youcube" ];
postInstall = ''
mkdir -p $out/bin
makeWrapper ${pkgs.python3}/bin/python3 $out/bin/youcube-server \
--add-flags "$out/lib/${pkgs.python3.libPrefix}/site-packages/youcube/__main__.py" \
--set PYTHONPATH "${pkgs.python3.pkgs.makePythonPath deps}" \
--set PATH "${pkgs.lib.makeBinPath [ pkgs.sanjuuni pkgs.ffmpeg ]}"
'';
};

docker =
let server = self.packages.${system}.default;
in pkgs.dockerTools.buildLayeredImage {
name = server.pname;
tag = server.version;
contents = [ server pkgs.sanjuuni pkgs.ffmpeg ] ++ self.packages.${system}.default.propagatedBuildInputs;

config = {
Cmd =
let pkg = self.packages.${system}.default;
in [ "${pkg}/bin/youcube-server" ];
WorkingDir = "/";
};
};
};

defaultApp = utils.lib.mkApp { drv = self.packages.${system}.default; };

devShells.default = pkgs.mkShell {
packages = with pkgs; [ python3 python3Packages.pip sanjuuni ffmpeg ];
};

}) // {

nixosModules.default = { config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.youcube-server;
in
{
options.services.youcube-server = {
enable = mkOption {
type = types.bool;
default = false;
};
packages = {
type = types.submodule;
description = lib.mdDoc "Packages used by YouCube";
options = {
sanjuuni = mkPackageOptionMD pkgs "sanjuuni" { };
ffmpeg = mkPackageOptionMD pkgs "ffmpeg" { };
};
};

spotify = {
type = types.submodule;
description = "Spotify support";
options = {
credentialsFile = mkOption {
type = types.path;
description = lib.mdDoc ''
File containing the SPOTIFY_CLIENT_ID and
SPOTIFY_CLIENT_SECRET in the format of
an EnvironmentFile=, as described by systemd.exec(5).
'';
example = "/etc/nixos/youcube-spotify-credentials";
default = null;
};
};
};

host = mkOption {
description = lib.mdDoc "The host of the YouCube server.";
default = "0.0.0.0";
type = types.string;
};

port = mkOption {
description = lib.mdDoc "YouCube server port.";
default = 5000;
type = types.port;
};
};

config = mkIf cfg.enable {
systemd.services."youcube.youcube-server" = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig =
let
pkg = self.packages.${system}.default;
in
{
Restart = "on-failure";
ExecStart = "python3 ${pkg}/${pkgs.python3.sitePackages}/youcube/__main__.py";
DynamicUser = "yes";
RuntimeDirectory = "youcube.youcube-server";
RuntimeDirectoryMode = "0755";
StateDirectory = "youcube.youcube-server";
StateDirectoryMode = "0700";
CacheDirectory = "youcube.youcube-server";
CacheDirectoryMode = "0750";
EnvironmentFile = [ (cfg.spotify.credentialsFile || "/dev/null") ];
};
environment = {
SANJUUNI_PATH = "${cfg.packages.sanjuuni}/bin/sanjuuni";
FFMPEG_PATH = "${cfg.packages.ffmpeg}/bin/ffmpeg";
HOST = "${cfg.host}";
PORT = "${cfg.port}";
};
};
};
};
};
}