From e3e3f34646af9e928d1ce7afbafa94ca8799a36a Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Wed, 4 Dec 2024 13:28:30 -0700 Subject: [PATCH] Package rescrobbled from an open nixpkgs PR, add home-manager module for it --- README.md | 9 ++- flake.nix | 1 + home-manager/custom-modules/default.nix | 6 ++ home-manager/custom-modules/rescrobbled.nix | 85 +++++++++++++++++++++ home-manager/default.nix | 5 +- home-manager/home.nix | 1 + home-manager/modules/rescrobbled.nix | 21 +++++ packages/rescrobbled/default.nix | 44 +++++++++++ secrets.example.nix | 15 ++++ 9 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 home-manager/custom-modules/default.nix create mode 100644 home-manager/custom-modules/rescrobbled.nix create mode 100644 home-manager/modules/rescrobbled.nix create mode 100644 packages/rescrobbled/default.nix diff --git a/README.md b/README.md index e85d315..4cdeb79 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ This folder contains sub-folders for each of my NixOS hosts, of which there is o This folder contains all of my personally-made nix packages. This is mainly just packages I can't seem to find on the interwebs, like some themes. Theoretically they could be used by someone else, but I haven't quite made my config modular yet, so don't try. -| Package | License | Original Link | -| ------------------- | ------- | ---------------------------------------------------------------------- | -| BeautyLine (Garuda) | GPLv3 | https://gitlab.com/garuda-linux/themes-and-settings/artwork/beautyline | -| Magna Splash 6 | GPLv3 | https://www.pling.com/p/2136626/ | +| Package | License | Original Link | +| ------------------- | ------- | ---------------------------------------------------------------------------------| +| BeautyLine (Garuda) | GPLv3 | https://gitlab.com/garuda-linux/themes-and-settings/artwork/beautyline | +| Magna Splash 6 | GPLv3 | https://www.pling.com/p/2136626/ | +| Rescrobbled | GPLv3 | https://github.com/NixOS/nixpkgs/pull/274553 (unmerged nixpkgs PR by negatethis) | diff --git a/flake.nix b/flake.nix index f11475e..fde207b 100755 --- a/flake.nix +++ b/flake.nix @@ -83,6 +83,7 @@ # the path to your home.nix. modules = [ plasma-manager.homeManagerModules.plasma-manager + ./home-manager/custom-modules ./home-manager/home.nix ]; diff --git a/home-manager/custom-modules/default.nix b/home-manager/custom-modules/default.nix new file mode 100644 index 0000000..7e01992 --- /dev/null +++ b/home-manager/custom-modules/default.nix @@ -0,0 +1,6 @@ +{ ... }: +{ + imports = [ + ./rescrobbled.nix + ]; +} \ No newline at end of file diff --git a/home-manager/custom-modules/rescrobbled.nix b/home-manager/custom-modules/rescrobbled.nix new file mode 100644 index 0000000..9f0edf3 --- /dev/null +++ b/home-manager/custom-modules/rescrobbled.nix @@ -0,0 +1,85 @@ +{ pkgs, lib, config, ... }: +let tomlFormat = pkgs.formats.toml { }; in { + options = { + services.custom.rescrobbled = { + enable = lib.mkEnableOption "rescrobbled"; + + package = lib.mkOption { + type = lib.types.package; + default = (pkgs.callPackage ../../packages/rescrobbled {}); + defaultText = lib.literalExpression "pkgs.rescrobbled"; + description = "The package to use for rescrobbled"; + }; + + session = lib.mkOption { + type = lib.types.str; + description = '' + The session token to use for last.fm. + This can be found by setting a last.fm API key & secret, + and then manually running the daemon. Then, the session token + will be located at {file}`$XDG_CONFIG_HOME/rescrobbled/session`. + ''; + default = null; + }; + + settings = lib.mkOption { + type = tomlFormat.type; + default = {}; + example = lib.literalExpression '' + lastfm-key = "Last.fm API key" + lastfm-secret = "Last.fm API secret" + min-play-time = 0 + player-whitelist = [ "Player MPRIS identity or bus name" ] + filter-script = "path/to/script" + + [[listenbrainz]] + url = "Custom API URL" + token = "User token" + ''; + description = '' + Configuration written to + {file}`$XDG_CONFIG_HOME/rescrobbled/config.toml`. + + See for the full list + of options. + ''; + }; + }; + }; + + config = let cfg = config.services.custom.rescrobbled; in lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + xdg.configFile ={ + "rescrobbled/config.toml" = lib.mkIf (cfg.settings != { }) { + enable = true; + + source = tomlFormat.generate "rescrobbled-config" cfg.settings; + }; + + "rescrobbled/session" = lib.mkIf (cfg.settings != { }) { + enable = true; + # Force makes it easy to generate the session file, then add it to nix cfg without issues + force = true; + + text = cfg.session; + }; + }; + + systemd.user.services.rescrobbled = { + Unit = { + Description = "An MPRIS scrobbler"; + Documentation = "https://github.com/InputUsername/rescrobbled"; + Wants = "network-online.target"; + After = "network-online.target"; + }; + + Service = { + ExecStartPre = "${pkgs.coreutils}/bin/sleep 15"; # Avoid listenbrainz errors by adding delay till network is connected + ExecStart = "${cfg.package}/bin/rescrobbled"; + }; + + Install.WantedBy = [ "default.target" ]; + }; + }; +} diff --git a/home-manager/default.nix b/home-manager/default.nix index a3d4f6f..9c76ef5 100755 --- a/home-manager/default.nix +++ b/home-manager/default.nix @@ -2,7 +2,10 @@ { home-manager.useGlobalPkgs = true; home-manager.useUserPackages = true; - home-manager.sharedModules = [ inputs.plasma-manager.homeManagerModules.plasma-manager ]; + home-manager.sharedModules = [ + inputs.plasma-manager.homeManagerModules.plasma-manager + ./custom-modules + ]; home-manager.extraSpecialArgs = { inherit inputs system secrets; }; diff --git a/home-manager/home.nix b/home-manager/home.nix index d96f786..9cb1a64 100755 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -17,5 +17,6 @@ ./modules/desktop.nix ./modules/gtk.nix ./modules/beets.nix + ./modules/rescrobbled.nix ]; } \ No newline at end of file diff --git a/home-manager/modules/rescrobbled.nix b/home-manager/modules/rescrobbled.nix new file mode 100644 index 0000000..190c80c --- /dev/null +++ b/home-manager/modules/rescrobbled.nix @@ -0,0 +1,21 @@ +{ secrets, lib, ... }: +{ + services.custom.rescrobbled = let s = secrets.programs.rescrobbled; in lib.mkIf s.enable { + enable = true; + + session = s.lastfm.session; + + settings = { + player-whitelist = [ "elisa" ]; + + listenbrainz = lib.mkIf (s.listenbrainz.token != null) [ + { + token = s.listenbrainz.token; + } + ]; + + lastfm-key = lib.mkIf (s.lastfm.key != null && s.lastfm.secret != null) s.lastfm.key; + lastfm-secret = lib.mkIf (s.lastfm.key != null && s.lastfm.secret != null) s.lastfm.secret; + }; + }; +} \ No newline at end of file diff --git a/packages/rescrobbled/default.nix b/packages/rescrobbled/default.nix new file mode 100644 index 0000000..d915fd4 --- /dev/null +++ b/packages/rescrobbled/default.nix @@ -0,0 +1,44 @@ +{ lib +, bash +, fetchFromGitHub +, rustPlatform +, pkg-config +, openssl +, dbus +}: + +rustPlatform.buildRustPackage rec { + + pname = "rescrobbled"; + version = "0.7.1"; + + src = fetchFromGitHub { + owner = "InputUsername"; + repo = "rescrobbled"; + rev = "v${version}"; + hash = "sha256-1E+SeKjHCah+IFn2QLAyyv7jgEcZ1gtkh8iHgiVBuz4="; + }; + + cargoHash = "sha256-ZJbyYFvGTuXt1aqhGOATcDRrkTk7SorWXkN81sUoDdo="; + + nativeBuildInputs = [ pkg-config ]; + + buildInputs = [ openssl dbus ]; + + postPatch = '' + # Required for tests + substituteInPlace src/filter.rs --replace '#!/usr/bin/bash' '#!${bash}/bin/bash' + ''; + + postInstall = '' + substituteInPlace rescrobbled.service --replace '%h/.cargo/bin/rescrobbled' "$out/bin/rescrobbled" + ''; + + meta = with lib; { + description = "MPRIS music scrobbler daemon"; + homepage = "https://github.com/InputUsername/rescrobbled"; + license = licenses.gpl3Plus; + mainProgram = "rescrobbled"; + platforms = platforms.unix; + }; +} diff --git a/secrets.example.nix b/secrets.example.nix index 0ec7d7e..3cabf2a 100644 --- a/secrets.example.nix +++ b/secrets.example.nix @@ -4,6 +4,21 @@ # The API key to use for google custom search lyrics backend google_api_key = null; }; + + rescrobbled = { + enable = false; + + # The credentials to use for last.fm and listenbrainz + lastfm = { + key = null; + secret = null; + # Generated by manually running the rescrobbled daemon and reading $XDG_CONFIG_HOME/rescrobbled/session + session = null; + }; + listenbrainz = { + token = null; + }; + }; }; passwords = {