# For reference of the available options, see https://docs.noctalia.dev/getting-started/nixos/#config-ref
#
# To see a diff of the nix-set and GUI-set (current) settings, run:
# nix shell nixpkgs#jq nixpkgs#colordiff -c bash -c "colordiff -u --nobanner <(jq -S . ~/.config/noctalia/settings.json) <(noctalia-shell ipc call state all | jq -S .settings)"
{ config, lib, pkgs, inputs, ... }:

let
  cfg = config.noctaliaShell;
in
{
  imports = [
    inputs.noctalia.homeModules.default
  ];

  options.noctaliaShell = {
    enable = lib.mkEnableOption "Noctalia shell";

    settings = lib.mkOption {
      type = lib.types.attrs;
      default = { };
      description = "Noctalia shell settings. See https://docs.noctalia.dev/getting-started/nixos/#config-ref";
    };
  };

  config = lib.mkIf cfg.enable {
    programs.noctalia-shell = {
      enable = true;
      # Override with calendarSupport for evolution-data-server integration
      package = inputs.noctalia.packages.${pkgs.stdenv.hostPlatform.system}.default.override {
        calendarSupport = true;
      };
      # Defaults merged with per-host settings; per-host wins on conflict.
      settings = lib.recursiveUpdate {
        # Detach launched apps into a new session so they don't inherit
        # noctalia's controlling tty / process group. Fixes Steam silently
        # failing to launch via the launcher on NixOS while staying light
        # enough not to interfere with Proton/pressure-vessel child processes
        # (unlike `systemd-run --user --scope`, which broke Overwatch).
        appLauncher = {
          customLaunchPrefixEnabled = true;
          customLaunchPrefix = "setsid --fork";
        };
      } cfg.settings;
    };

    # Disable conflicting services that Noctalia replaces
    services.swayosd.enable = lib.mkForce false;
    services.swaync.enable = lib.mkForce false;

    # Disable wlsunset - Noctalia has built-in night light
    services.wlsunset.enable = lib.mkForce false;

    # Disable waybar - Noctalia has its own bar
    waybarConfig.enable = lib.mkForce false;
    programs.waybar.enable = lib.mkForce false;
  };
}
