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

{
  imports =
    [
      ./hardware-configuration.nix
    ];

  beatific.hostName = "unfolder";
  beatific.extraPrograms = true;

  # Bootloader.
  boot.loader.grub = {
    enable = true;
    device = "/dev/sda";
  };

  networking.networkmanager.enable = true;

  fileSystems."/mnt/backyard" = {
    device = "//backyard.home/tvb";
    fsType = "cifs";
    options = [
      "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s,credentials=/home/tvb/.local/state/smb/backyard"
    ];
  };

  # Override time zone to PST
  time.timeZone = "America/Los_Angeles";

  # Enable Gnome because it has a virtual keyboard for tablet mode
  services.xserver = {
    enable = true;
    displayManager.gdm.enable = true;
    desktopManager.gnome.enable = true;

    # keymap configuration
    xkb.layout = "us";
    xkb.variant = "";
  };

  # Enable sound with pipewire.
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };

  systemd.user.services.yoga-rotate = {
    enable = false; # TODO
    description = "ThinkPad Yoga display rotation";
    #wantedBy = ["multi-user.target"];
    wantedBy = [ "graphical-session.target" ];
    #requires = ["display-manager.service"];
    partOf = [ "graphical-session.target" ];
    #after = ["display-manager.service"];
    path = [ pkgs.gnome-randr ];
    serviceConfig = {
      #Type = "simple";
      ExecStart = "${pkgs.python3}/bin/python /home/tvb/rotate.py";
      #KillMode = "process";
      #User = "tvb";
      Restart = "on-failure";
      RestartSec = 5;
    };
    #environment = {
    #  DISPLAY = ":0";
    #  XAUTHORITY = "/home/tvb/.Xauthority";
    #};
  };

  users.users.tvb = {
    extraGroups = [ "networkmanager" "docker" ];
    packages = with pkgs; [
      gnome-randr
      python3
      (writeShellScriptBin "single-file" ''
        exec ${single-file-cli}/bin/single-file --browser-executable-path ${chromium}/bin/chromium "$@"
      '')
    ];
  };

  nixpkgs.config.allowUnfree = true;
  environment.systemPackages = with pkgs; [
    bitwarden-cli
    bitwarden-desktop
    comic-mono
    firefox
    gnome-terminal
    libreoffice
    obsidian
    mpv
    qutebrowser
    ruby
  ];
  programs.direnv.enable = true;

  networking.firewall = {
    enable = true;
  };

  # This seems to be sufficient to autodetect the printing functionality of HP OfficeJet 6950
  services.printing.enable = true;
  services.printing.drivers = [
    pkgs.brlaser
    pkgs.hplip
  ];
  services.avahi = {
    enable = true;
    nssmdns4 = true;
    openFirewall = true;
  };

  # This value governs how some stateful data, like databases, are handled
  # across different versions of NixOS. This should not be changed to a new
  # release unless the sysadmin has determined that no services would be
  # adversely affected by changing this.
  system.stateVersion = "22.11";
}