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

let
    concat = strings: builtins.concatStringsSep " " strings;
    wacom.src =
        let
            xsetwacom = "${pkgs.xf86_input_wacom}/bin/xsetwacom";
            awk = lib.getExe pkgs.gawk;
        in
        ''
            #!/bin/sh

            # Wait for X socket
            for i in $(seq 30); do
            	[ -S /tmp/.X11-unix/X0 ] && break
            	sleep 0.5
            done

			# Just to be sure that the displays are ready...
			sleep 5

            export DISPLAY=:0
            export XAUTHORITY="$HOME/.Xauthority"

            for i in $(seq 10); do
            	if ${xsetwacom} list devices | grep -q Wacom; then
            		break
            	fi
            	sleep 1
            done

            stylus=$(${xsetwacom} list devices | ${awk} -F' \t' '/stylus/{print $1}')

            if [ -z "$${stylus}" ]; then
            	exit 0
            fi

            ${xsetwacom} set "$stylus" MapToOutput HEAD-0
        '';
in
{
    services.xserver.videoDrivers = [ "nvidia" ];
    services.xserver.wacom.enable = true;

    hardware.nvidia = {
        modesetting.enable = true;

        open = true;

        nvidiaSettings = true;

        package = config.boot.kernelPackages.nvidiaPackages.stable;
    };

    home-manager.users.anton.xsession.profileExtra = concat [
        "${lib.getExe pkgs.xrandr}"
        "${concat [
            "--output DP-2"
            "--mode 1920x1080"
            "--pos 0x0"
            "--rate 144"
            "--primary"
        ]}"
        "${concat [
            "--output HDMI-0"
            "--mode 1920x1080"
            "--pos 1920x0"
            "--rate 144"
        ]}"
    ];

    systemd.user.services.wacom = {
        enable = true;
        description = "Configure my Wacom (One) tablet";
        wantedBy = [ "default.target" ];
        after = [ "graphical-session.target" ];
        serviceConfig = {
            Type = "oneshot";
            ExecStart = pkgs.writeShellScript "wacom.sh" wacom.src;
        };
    };

    services.udev.extraRules = ''
        ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="056a", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}+="wacom.service"
    '';
}
