···
+
cfg = config.services.lavalink;
+
format = pkgs.formats.yaml { };
+
options.services.lavalink = {
+
enable = mkEnableOption "Lavalink";
+
package = lib.mkPackageOption pkgs "lavalink" { };
+
type = types.nullOr types.str;
+
example = "s3cRe!p4SsW0rD";
+
The password for Lavalink's authentication in plain text.
+
The port that Lavalink will use.
+
The network address to bind to.
+
openFirewall = mkOption {
+
Whether to expose the port to the network.
+
The user of the service.
+
The group of the service.
+
default = "/var/lib/lavalink";
+
example = "/home/lavalink";
+
The home directory for lavalink.
+
enableHttp2 = mkEnableOption "HTTP/2 support";
+
example = "-Djava.io.tmpdir=/var/lib/lavalink/tmp -Xmx6G";
+
Set custom JVM arguments.
+
environmentFile = mkOption {
+
type = types.nullOr types.str;
+
example = "/run/secrets/lavalink/passwordEnvFile";
+
Add custom environment variables from a file.
+
See <https://lavalink.dev/configuration/index.html#example-environment-variables> for the full documentation.
+
dependency = mkOption {
+
example = "dev.lavalink.youtube:youtube-plugin:1.8.0";
+
The coordinates of the plugin.
+
repository = mkOption {
+
example = "https://maven.example.com/releases";
+
default = "https://maven.lavalink.dev/releases";
+
The plugin repository. Defaults to the lavalink releases repository.
+
To use the snapshots repository, use <https://maven.lavalink.dev/snapshots> instead
+
example = lib.fakeHash;
+
The hash of the plugin.
+
configName = mkOption {
+
type = types.nullOr types.str;
+
The name of the plugin to use as the key for the plugin configuration.
+
extraConfig = mkOption {
+
type = types.submodule { freeformType = format.type; };
+
The configuration for the plugin.
+
The {option}`services.lavalink.plugins.*.configName` option must be set.
+
example = lib.literalExpression ''
+
dependency = "dev.lavalink.youtube:youtube-plugin:1.8.0";
+
repository = "https://maven.lavalink.dev/snapshots";
+
configName = "youtube";
+
allowDirectVideoIds = true;
+
allowDirectPlaylistIds = true;
+
A list of plugins for lavalink.
+
extraConfig = mkOption {
+
type = types.submodule { freeformType = format.type; };
+
Configuration to write to {file}`application.yml`.
+
See <https://lavalink.dev/configuration/#example-applicationyml> for the full documentation.
+
Individual configuration parameters can be overwritten using environment variables.
+
See <https://lavalink.dev/configuration/#example-environment-variables> for more information.
+
example = lib.literalExpression ''
+
logging.file.path = "./logs/";
+
pluginSymlinks = lib.concatStringsSep "\n" (
+
pluginParts = lib.match ''^(.*?:(.*?):)([0-9]+\.[0-9]+\.[0-9]+)$'' pluginCfg.dependency;
+
pluginWebPath = lib.replaceStrings [ "." ":" ] [ "/" "/" ] (lib.elemAt pluginParts 0);
+
pluginFileName = lib.elemAt pluginParts 1;
+
pluginVersion = lib.elemAt pluginParts 2;
+
pluginFile = "${pluginFileName}-${pluginVersion}.jar";
+
pluginUrl = "${pluginCfg.repository}/${pluginWebPath}${pluginVersion}/${pluginFile}";
+
plugin = pkgs.fetchurl {
+
inherit (pluginCfg) hash;
+
"ln -sf ${plugin} ${cfg.home}/plugins/${pluginFile}"
+
pluginExtraConfigs = builtins.listToAttrs (
+
pluginConfig: lib.attrsets.nameValuePair pluginConfig.configName pluginConfig.extraConfig
+
) (lib.lists.filter (pluginCfg: pluginCfg.configName != null) cfg.plugins)
+
config = lib.attrsets.recursiveUpdate cfg.extraConfig {
+
inherit (cfg) port address;
+
http2.enabled = cfg.enableHttp2;
+
plugins = pluginExtraConfigs;
+
builtins.removeAttrs pluginConfig [
+
configWithPassword = lib.attrsets.recursiveUpdate config (
+
lib.attrsets.optionalAttrs (cfg.password != null) { lavalink.server.password = cfg.password; }
+
configFile = format.generate "application.yml" configWithPassword;
+
pluginCfg: pluginCfg.extraConfig != { } && pluginCfg.configName == null
+
message = "Plugins with extra configuration need to have the `configName` attribute defined";
+
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
+
users.groups = mkIf (cfg.group == "lavalink") { lavalink = { }; };
+
users.users = mkIf (cfg.user == "lavalink") {
+
description = "The user for the Lavalink server";
+
systemd.tmpfiles.settings."10-lavalink" =
+
inherit (cfg) user group;
+
"${cfg.home}/plugins".d = mkIf (cfg.plugins != [ ]) dirConfig;
+
${cfg.home}.d = dirConfig;
+
systemd.services.lavalink = {
+
description = "Lavalink Service";
+
wantedBy = [ "multi-user.target" ];
+
ln -sf ${configFile} ${cfg.home}/application.yml
+
export _JAVA_OPTIONS="${cfg.jvmArgs}"
+
${lib.getExe cfg.package}
+
Restart = "on-failure";
+
EnvironmentFile = cfg.environmentFile;
+
WorkingDirectory = cfg.home;