{ 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 sway defaults.";
        };

    };

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

        #programs.dconf.enable = true;
        #programs.sway.enable = true;
        #programs.sway.wrapperFeatures.gtk = true;

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

        ###
        ##  System Packages
        ###
        environment.systemPackages = with pkgs; lib.lists.flatten [
             [
                bash
                foot
                git
                glib # gsettings
                grim
                #sway-contrib.grimshot
                libnotify
                mako
                man-pages
                man-pages-posix
                ncspot
                networkmanagerapplet
                #unstable.niri
                pavucontrol
                slurp
                syncthingtray
                swaylock
                #swayidle
                #swaybg
                # swww
                #tailscale-systray
                wl-clipboard
                waybar
                wdisplays
                wofi
                xdg-utils
                zsh
                lxqt.lxqt-policykit
                # Fonts
             ]
            config.niriwm.systemPackages
        ];
        environment.variables.QT_STYLE_OVERRIDE = "kvantum";

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

        # Thunar config
        programs.thunar = {
            enable = true;
            plugins = with pkgs.xfce; [
                thunar-archive-plugin
                thunar-volman
            ];
        };
        programs.xfconf.enable = true;

        programs.zsh.enable = true;
        #programs.steam.enable = config.swaywm.installGaming;
        #programs.gamemode.enable = true;

        #programs.adb.enable = true;
        #programs.kdeconnect.enable = true;
        # service file to start the sshAgent
        #programs.ssh.startAgent = true;

        ###
        ##  Services
        ###
        virtualisation.docker.enable = true;
        services.blueman.enable = true;
        #services.flatpak.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.syncthing.enable = true;
        services.gnome.gnome-keyring.enable = true;
        #services.usbmuxd.enable = true;
        #services.tailscale = {
        #    enable = true;
        #    openFirewall = true;
        #};
        # For betaflight configurator
        #services.udev.extraRules = ''
            # DFU (Internal bootloader for STM32 and AT32 MCUs)
        #    SUBSYSTEM=="usb", ATTRS{idVendor}=="2e3c", ATTRS{idProduct}=="df11", MODE="0664", GROUP="dialout"
        #    SUBSYSTEM=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="df11", MODE="0664", GROUP="dialout"
        #'';
        # For yubioath desktop
        services.pcscd.enable = true;
        # Audio
        security.rtkit.enable = true;
        services.pipewire = {
          enable = true;
          alsa.enable = true;
          pulse.enable = true;
          wireplumber.enable = true;
        };
        sound.enable = false;

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

        #services.greetd = {
        #  enable = true;
        #  settings = rec {
        #    initial_session = {
        #      command = "${pkgs.sway}/bin/sway";
        #      user = "nate";
        #    };
        #    default_session = initial_session;
        #  };
        #};

        # services.xserver.videoDrivers = [ "amdgpu" ];
        # Enable HIP
        #systemd.tmpfiles.rules = [
        #    "L+    /opt/rocm/hip   -    -    -     -    ${pkgs.rocmPackages.clr}"
        #];
        hardware.bluetooth.enable = true; # enables support for Bluetooth
        hardware.bluetooth.powerOnBoot = true; # powers up the default Bluetooth controller on boot
        #hardware.opengl = {
            # Mesa
            # enable = true;
            # Vulkan
            # driSupport = true;
            # Rocm support and vulkan drivers
        #    extraPackages = with pkgs; [
        #        rocmPackages.clr.icd
        #    ];
        #};
    };
}
