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

with lib;

# steam
# -------
# Don't forget to run 'xhost +' with your user
# to make sure the browser user can write to X
let

  bin = pkgs.writeShellScriptBin "steam" ''
    /var/run/wrappers/bin/sudo -u steam -i ${pkgs.steam}/bin/steam $@
  '';

  cfg = config.programs.custom.steam;

in {

  options.programs.custom.steam.enable = mkEnableOption "enable steam";

  config = mkIf cfg.enable {

    environment.systemPackages = [
      bin
      pkgs.xorg.xhost
      # to use xbox controllers
      pkgs.xboxdrv
    ];

    users.users.steam = {
      isNormalUser = true;
      home = "/home/steam";
      createHome = true;
      extraGroups = [ "audio" "input" "video" ];
    };

    # for steam
    # ---------
    hardware.opengl.driSupport = true;
    hardware.opengl.driSupport32Bit = true;

    security.sudo.extraConfig = ''
      ${config.users.extraUsers.mainUser.name} ALL=(steam) NOPASSWD: ALL
    '';
  };
}