{ inputs, lib, config, pkgs, userName, ... }:
  let
    unstable = import inputs.nixpkgs-unstable { system = "x86_64-linux"; config.allowUnfree = true; };
  in
{
    options.niriwm = {
      enable = lib.mkEnableOption "Enable niri window manager.";
      useNonFree = lib.mkOption {
          default = false;
          example = true;
          description = "Whether to enable non-free software in the niri config";
      };
      systemPackages = lib.mkOption {
          default = [];
          description = "Add any additional packages desired. Merged with niri defaults.";
      };
      user = lib.mkOption {
        type = lib.types.str;
      };

    };

    ###
    ##  Configuration
    ###
    config = lib.mkIf config.niriwm.enable {

        nixpkgs.config.allowUnfree = config.niriwm.useNonFree;
        
        ###
        ##  XDG portal setup
        ###
        xdg.portal = {
            config = {
                common = {
                  default = [
                    "wlr"
                  ];
                };
            };
            extraPortals = with pkgs; [
                xdg-desktop-portal-gnome
            ];
            wlr.enable = true;
            enable = true;
        };
        xdg.sounds.enable = true;

        ###
        ##  System Packages
        ###
        environment.systemPackages = with pkgs; lib.lists.flatten [
             [
                bash
                ghostty
                git
                glib # gsettings
                grim
                libnotify
                man-pages
                man-pages-posix
                nautilus
                networkmanagerapplet
                pavucontrol
                slurp
                syncthingtray
                swaylock
                wl-clipboard
                waybar
                wdisplays
                wofi
                xdg-utils
                zsh
                lxqt.lxqt-policykit
                unstable.xwayland-satellite
                # Fonts
             ]
            config.niriwm.systemPackages
        ];
        environment.variables.QT_STYLE_OVERRIDE = "kvantum";

        # adds additional man pages
        documentation.dev.enable = true;

        programs.steam = {
            enable = true;
            gamescopeSession.enable = true;
        };
        programs.gamemode = {
            enable = true;
            settings = {
                general = {
                    reaper_freq = 5;
                    desiredgov = "performance";
                    softrealtime = "auto";
                };
            };
        };
        programs.kdeconnect.enable = true;
        # programs.kdeconnect.package = pkgs.gnomeExtensions.gsconnect;

        # For nautilus
        services.gnome.sushi.enable = true;
        programs.nautilus-open-any-terminal = {
          enable = true;
          terminal = "ghostty";
        };
        programs.file-roller.enable = true;
        programs.xfconf.enable = true;

        services.syncthing = {
          enable = true;
          dataDir = "/home/${config.niriwm.user}/.syncthing";
          openDefaultPorts = true;
          user = config.niriwm.user;
        };
        systemd.services.syncthing.environment.STNODEFAULTFOLDER = "true"; # Don't create default ~/Sync folder

        programs.zsh.enable = true;
        
        # Set zsh as the default shell system-wide
        users.defaultUserShell = pkgs.zsh;
        environment.shells = with pkgs; [ zsh bash ];

        ###
        ##  Services
        ###
        virtualisation.docker.enable = true;
        services.blueman.enable = true;
        services.gvfs.enable = true;  # thunar mount, trash, etc
        services.tumbler.enable = true; # thunar thumbnails
        services.openssh.enable = true;
        services.dbus.enable = true;
        services.gnome.gnome-keyring.enable = true;
        # For yubioath desktop
        services.pcscd.enable = true;
        # Audio - Modern PipeWire setup for Framework laptop
        # Disable PulseAudio in favor of PipeWire
        services.flatpak.enable = true;
        services.pulseaudio.enable = false;
        security.rtkit.enable = true;
        services.pipewire = {
          enable = true;
          audio.enable = true;
          alsa.enable = true;
          alsa.support32Bit = true;
          pulse.enable = true;
          wireplumber.enable = true;
          wireplumber.extraConfig = {
            "wireplumber.settings" = {
              bluetooth.autoswitch-to-headset-profile = false;
            };
            bluetoothEnhancements = {
              "monitor.bluez.properties" = {
                  "bluez5.enable-sbc-xq" = true;
                  "bluez5.enable-msbc" = true;
                  "bluez5.enable-hw-volume" = true;
                  # Default roles: https://pipewire.pages.freedesktop.org/wireplumber/daemon/configuration/bluetooth.html#monitor-properties
                  "bluez5.roles" = [ "a2dp_sink" "a2dp_source" "bap_sink" "bap_source" "hfp_hf" "hfp_ag" ];
              };
            };
          };
        };

        ###
        ##  Misc
        ###
        # Necessary for home-manager niri setup
        security.polkit.enable = true;

        hardware.bluetooth = {
          enable = true; # enables support for Bluetooth
          powerOnBoot = true; # powers up the default Bluetooth controller on boot
          settings = {
            General = {
              Enable = "Source,Sink,Media,Socket";
              Experimental = true;
            };
          };
        };
    };
}
