{ config, pkgs, callPackage, ... }:

{
  imports =
    [
      ./hardware-configuration.nix
    ];
  nix = {
    extraOptions = "experimental-features = nix-command flakes";
    package = pkgs.nixFlakes;
  };

  # Use the systemd-boot EFI boot loader.
  boot.kernelPackages = pkgs.linuxPackages_zen;
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  nixpkgs.config.allowUnfree = true;

  boot.kernelParams = [ "mem_sleep_default=deep" ];

  networking.hostName = "syl"; # Define your hostname.
  networking.networkmanager.enable = true;
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Set your time zone.
  time.timeZone = "America/Chicago";

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = false;
  networking.interfaces.wlp170s0.useDHCP = true;

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";

  hardware.uinput.enable = true;

  nixpkgs.config.packageOverrides = pkgs: {
    vaapiIntel = pkgs.vaapiIntel.override { enableHybridCodec = true; };
  };

  hardware.opengl = {
    enable = true;
    extraPackages = with pkgs; [
      intel-media-driver # LIBVA_DRIVER_NAME=iHD
      vaapiIntel         # LIBVA_DRIVER_NAME=i965 (older but works better for Firefox/Chromium)
      vaapiVdpau
      libvdpau-va-gl
    ];
  };

  services.xserver = {
    enable = true;
    windowManager.awesome = {
      enable = true;
      package = pkgs.myAwesome;
    };
    displayManager.sddm.enable = true;
    desktopManager.plasma5.enable = true;
  };
  
  # programs.qt5ct.enable = true;

  # Configure keymap in X11
  services.xserver.layout = "us";
  # services.xserver.xkbOptions = "eurosign:e";

  # Enable CUPS to print documents.
  services.printing.enable = true;
  services.printing.drivers = [ pkgs.gutenprint pkgs.gutenprintBin pkgs.hplipWithPlugin ]; 

  # Enable fingerprint
  services.fprintd.enable = true;
  programs.qt5ct.enable = true;

  # Enable sound.
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    wireplumber.enable = true;
  };
  
  # Needed for some pipewire progs
  programs.dconf.enable = true;

  # Turn on flatpak
  services.flatpak.enable = true;

  # Some other things
  services.thermald.enable = true;
  services.tlp = {
    enable = true;
    settings = {
      CPU_SCALING_GOVERNOR_ON_AC="performance";
      CPU_SCALING_GOVERNOR_ON_BAT="powersave";
      CPU_ENERGY_PERF_POLICY_ON_AC="balance_performance";
      CPU_ENERGY_PERF_POLICY_ON_BAT="power";
      PCIE_ASPM_ON_AC="default";
      PCIE_ASPM_ON_BAT="powersupersave";
      SCHED_POWERSAVE_ON_AC=0;
      SCHED_POWERSAVE_ON_BAT=1;
      START_CHARGE_THRESH_BAT1=70;
      STOP_CHARGE_THRESH_BAT1=80;
      USB_ALLOWLIST="32ac:0002";
    };
  };

  services.usbmuxd.enable = true;
  services.fstrim.enable = true;

  # Enable touchpad support (enabled default in most desktopManager).
  services.xserver.libinput.enable = true;

  # Set default shell to be dash for speed
  # Apparently this is bad because a lot of nix relies on bash
  # environment.binsh = "${pkgs.dash}/bin/dash";
  environment.variables = {
    EDITOR = "emacsclient -t -a";
    VISUAL = "emacsclient -c -a";
  
    # Fixing Qt apps in other environments
    # QT_XCB_GL_INTEGRATION = "xcb_egl";
    QT_QPA_PLATFORM_PLUGIN_PATH = "${pkgs.qt5.qtbase.bin}/lib/qt-${pkgs.qt5.qtbase.qtCompatVersion}/plugins/platforms";
  };
  environment.homeBinInPath = true;
  programs.fish.enable = true;
  programs.zsh.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.chris = {
    isNormalUser = true;
    extraGroups = [ "wheel" "networkmanager" "input" "uinput" ];
  };

  programs.partition-manager.enable = true;
  programs.kdeconnect.enable = true;

  security.pam.services.kwallet = {
    name = "kwallet";
    enableKwallet = true;
  };
  security.pam.services.sddm.enableKwallet = true;

  virtualisation.waydroid.enable = true;

  # nixpkgs.config.firefox.enableTridactyl = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    vim
    wget
    killall
    discover
    kde-rounded-corners
    lightly-qt
    pinentry
    pinentry-qt
    unzip
    unrar
    p7zip
    zip
    gzip
    usbutils
    binutils
    hunspell
    hunspellDicts.en_US
    git
    transmission
    openssh
    samba
    ark
    dash
    light
    kget
    krename
    kwallet-pam
    plasma5Packages.kwallet
    libimobiledevice
    sddm-kcm
    ydotool
    bottles
    exa
    imv
    feh
    mpv
    yt-dlp
    rofi-emoji
    nerdfonts
    latte-dock
    bat
    alacritty
    libsForQt5.bismuth
    libnotify
    rofi-wayland
    ripgrep
    fd
    plocate
    bc 
    sysstat
    procs
    pandoc
    papirus-icon-theme
    phinger-cursors
    plasma-hud
    kde-cli-tools
    gzip 
    htop
    btop
    brave
    # firefox
    kate
    kdialog
    openlp
    inkscape
    libreoffice-fresh
    vlc
    neochat
    haskellPackages.greenclip
    pulsemixer
    any-nix-shell
    wtype
    xdotool
    wmctrl
    xcape
    unclutter-xfixes
    bluez-tools
    networkmanager_dmenu
    plasma5Packages.qt5ct
    lxappearance
    spotdl
    kdenlive
    ffmpeg
    wlroots
    picom-next
    pamixer
    jellyfin-mpv-shim
    pfetch
    macchina
    gimp
    powertop
    quickemu
    neofetch
    afetch
    yafetch
    freshfetch
    disfetch
    bunnyfetch
    fet-sh
    macchina
    # Dev tools
    nix-index
    meson
    ninja
    gnumake
    gcc
    gdb
    clang
    clang-tools
    cmake 
    extra-cmake-modules
    pkg-config
    # LIBRARIES FOR DEV
    qt5.full
    qt5.qtbase
    qt5.qtquickcontrols2
    qt5.qtx11extras
    libsForQt5.kirigami2
    libsForQt5.ki18n
    libsForQt5.kcoreaddons
    fennel
    (libsForQt5.callPackage ../../LightlyShaders {})
  ];

  nixpkgs.overlays = [

    (import (builtins.fetchTarball {
      url = https://github.com/nix-community/emacs-overlay/archive/master.tar.gz;
      sha256 = "09rsqmz7i7lyays59b9600z11qqr6h6lcskw1zzp54yw2csxn2ix";
    }))

    (self: super:
      {
        myAwesome = super.awesome.overrideAttrs (old: rec {
          pname = "myAwesome";
          version = "git-20220508-c539e0e";
          src = super.fetchFromGitHub {
            owner = "awesomeWM";
            repo = "awesome";
            rev = "c539e0e4350a42f813952fc28dd8490f42d934b3";
            sha256 = "EDAL7NnLF2BiVI8DAlEciiZtDmwXOzCPypGTrlN/OoQ=";
          };
        });
      }
    )
    
    (self: super:
      {
        qt5ct = super.qt5ct.overrideAttrs (old: rec {
          patches = (old.patches or []) ++ [
            ../../qt5ct.patch
          ];
        });
      }
    )
  ];

  services.emacs = {
    enable = true;
    package = with pkgs; ((emacsPackagesFor emacsPgtkNativeComp).emacsWithPackages (epkgs: [ epkgs.vterm epkgs.magit epkgs.pdf-tools pkgs.mu ]));
  };

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "21.11"; # Did you read the comment?

}