{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.home.roles.launchers;

  # Generate a wrapper script for a package
  makeLauncher = packageName: pkgs.writeShellScriptBin packageName ''
    exec env NIXPKGS_ALLOW_UNFREE=1 ${pkgs.nix}/bin/nix run --impure nixpkgs#${packageName} -- "$@"
  '';

  # Generate all launcher scripts from the package list
  launcherPackages = map makeLauncher cfg.packages;
in
{
  options.home.roles.launchers = {
    enable = mkEnableOption "wrapper launchers for excluded packages";

    packages = mkOption {
      type = types.listOf types.str;
      default = [];
      example = [ "steam" "libreoffice" "lutris" ];
      description = ''
        List of package names to create launcher wrappers for.
        Each wrapper will run: NIXPKGS_ALLOW_UNFREE=1 nix run --impure nixpkgs#<package>

        This is useful for occasionally running packages without permanently installing them.
      '';
    };
  };

  config = mkIf cfg.enable {
    home.packages = launcherPackages;
  };
}
