playdoobs 0.2.0

Service and matching CLI application for controlling the currently active MPRIS2 player
Documentation
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    utils.url = "github:numtide/flake-utils";
  };
  outputs = {
    self,
    nixpkgs,
    utils,
  }:
    (utils.lib.eachDefaultSystem (
      system: let
        pkgs = import nixpkgs {inherit system;};
        inherit (pkgs) lib;
        commitDate = lib.join "-" (lib.match "^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2}).*" self.lastModifiedDate);
        fullBuild = pkgs.rustPlatform.buildRustPackage {
          pname = "playdoobs-full";
          version = "unstable-${commitDate}";

          src = ./.;
          cargoLock = {
            lockFile = ./Cargo.lock;
            allowBuiltinFetchGit = true;
          };

          VERGEN_GIT_DESCRIBE =
            if self?rev
            then builtins.substring 0 7 self.rev
            else "dirty";
          VERGEN_GIT_COMMIT_DATE = commitDate;

          # build the regular bins, as well as the waybar plugin
          cargoBuildFlags = [
            "--bins"
            "--example"
            "playdoobs-waybar"
          ];
          postInstall = let
            cargoTarget = pkgs.rustPlatform.cargoInstallHook.targetSubdirectory;
          in ''
            install -D target/${cargoTarget}/release/examples/playdoobs-waybar $out/bin/playdoobs-waybar
          '';

          checkFlags = [
            # dbus is not available
            "--skip=daemon::test::test_active_player_change"
          ];
        };
      in rec {
        packages = {
          playdoobs = pkgs.stdenvNoCC.mkDerivation rec {
            pname = "playdoobs";
            inherit (fullBuild) version;

            src = fullBuild;
            installPhase = ''
              ls -laF
              mkdir -p $out/bin
              ln -s $src/bin/${pname} $out/bin/
              ln -s $src/bin/playdoobsd $out/bin/
            '';

            meta.mainProgram = pname;
          };
          playdoobs-waybar = pkgs.stdenvNoCC.mkDerivation rec {
            pname = "playdoobs-waybar";
            inherit (fullBuild) version;

            src = fullBuild;
            installPhase = ''
              mkdir -p $out/bin
              ln -s $src/bin/${pname} $out/bin/
            '';

            meta.mainProgram = pname;
          };
          default = packages.playdoobs;
        };

        formatter = pkgs.alejandra;
      }
    ))
    // {
      nixosModules.playdoobs = {
        config,
        lib,
        pkgs,
        ...
      }: let
        system = pkgs.stdenv.hostPlatform.system;
        cfg = config.services.playdoobsd;
      in {
        options.services.playdoobsd = {
          enable = lib.mkEnableOption "the playdoobs daemon";
          package = lib.mkPackageOption self.packages.${system} "playdoobs" {};
        };

        config = lib.mkIf cfg.enable {
          environment.systemPackages = [cfg.package];
          systemd.user.services.playdoobsd = {
            description = "playdoobs daemon to track media player activity";
            wantedBy = ["default.target"];

            serviceConfig = {
              Type = "exec";
              ExecStart = "${cfg.package}/bin/playdoobsd";
            };
            restartIfChanged = true;
          };
        };
      };
    };
}