{ config, pkgs, ... }:

{
  # Enable the X11 windowing system.
  services.xserver = {
    videoDrivers = [ "nvidia" ];
#    videoDrivers = [ "amdgpu" "nvidia" ]; # doesn't work
    exportConfiguration = true;
#    xrandrHeads = [
#      { output = "HDMI-0"; } # lg-hd
##      { output = "eDP"; primary = true; } # laptop
#      { output = "DP-0"; monitorConfig = "Option \"Rotate\" \"left\""; } # samsung-vertical
#      { output = "DP-2"; } # samsung-4k
#    ];

     config = pkgs.lib.mkAfter ''

# nvidia-settings: X configuration file generated by nvidia-settings
# nvidia-settings:  version 535.113.01

# Additional "InputClass" sections
# For each supported driver, add a "Device" and "Screen"
# section.

#Section "ServerLayout"

  # Reference the Screen sections for each driver.  This will
  # cause the X server to try each in turn.
#    Identifier     "Layout[all]"
#    Screen      0  "Screen0" 0 0
#    Inactive       "Device-amdgpu[0]"
#    InputDevice    "Keyboard0" "CoreKeyboard"
#    InputDevice    "Mouse0" "CorePointer"
#    Option         "Xinerama" "0"
#EndSection

Section "ServerLayout"

#  Reference the Screen sections for each driver.  This will
#  cause the X server to try each in turn.
#  Screen "Screen-nvidia-HDMI-0" RightOf "Screen-amdgpu-eDP"
    Identifier     "Layout"
    Screen         "Screen-nvidia-DP-2" 0 0
    Screen         "Screen-nvidia-DP-0" 0 0
    Screen         "Screen-nvidia-HDMI-0" 0 0
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "Mouse0" "CorePointer"
EndSection

Section "Files"
    ModulePath      "/nix/store/3wpasr09n77wgbw74r56shh3xil44gc1-xf86-video-amdgpu-23.0.0/lib/xorg/modules/drivers"
    ModulePath      "/nix/store/h0gmbxjjvw85dhzgr1g003d4xpq21kag-xorg-server-21.1.8/lib/xorg/modules"
    ModulePath      "/nix/store/h0gmbxjjvw85dhzgr1g003d4xpq21kag-xorg-server-21.1.8/lib/xorg/modules/drivers"
    ModulePath      "/nix/store/h0gmbxjjvw85dhzgr1g003d4xpq21kag-xorg-server-21.1.8/lib/xorg/modules/extensions"
    ModulePath      "/nix/store/h0gmbxjjvw85dhzgr1g003d4xpq21kag-xorg-server-21.1.8/lib/xorg/modules/input"
    ModulePath      "/nix/store/jxnf2iacvlbl7hygmrnhcvcph3spqn8m-xf86-input-libinput-1.3.0/lib/xorg/modules/input"
    ModulePath      "/nix/store/nhdzsjl89i434d1y1bm3lb19m47w5bfr-xf86-input-evdev-2.10.6/lib/xorg/modules/input"
    ModulePath      "/nix/store/nrclw8lg1mmfirj2a3zsd4x7aw2rkm57-xf86-input-wacom-1.2.0/lib/xorg/modules/input"
    ModulePath      "/nix/store/xq5a75fgjwn8xcjlzxg6v6w9l017v9si-nvidia-x11-535.113.01-6.1.55-bin/lib/xorg/modules/drivers"
    ModulePath      "/nix/store/xq5a75fgjwn8xcjlzxg6v6w9l017v9si-nvidia-x11-535.113.01-6.1.55-bin/lib/xorg/modules/extensions"
    FontPath        "/nix/store/y0a95pyn5mqg1dav97d30qhl8qg0582x-dina-font-2.92/share/fonts/misc"
    FontPath        "/nix/store/y0a95pyn5mqg1dav97d30qhl8qg0582x-dina-font-2.92/share/fonts/misc"
    FontPath        "/nix/store/l7y9igmq8b73ihxk4rr6zzwzppfk9gim-dosemu-fonts-1.4.0/share/fonts/X11/misc/dosemu"
    FontPath        "/nix/store/sch1fazif5i5dmng2h3ng67kzbr6qha1-gohufont-2.1/share/fonts/misc"
    FontPath        "/nix/store/jyqd3y6zzyssag1dqvaspm24bhhmcqbb-proggyfonts-0.1/share/fonts/misc"
    FontPath        "/nix/store/z3y6kfsn21giz5fbs5hkmsjqqfjsm9r9-terminus-font-4.49.1/share/fonts/terminus"
    FontPath        "/nix/store/bsls00piy55hsynpcslfpa9qxbqr7h7c-tewi-font-2.0.2/share/fonts/misc"
    FontPath        "/nix/store/sdambdlarbhkyhqf8c6fkldcl5ip4c6a-ucs-fonts-20090406/share/fonts/misc"
    FontPath        "/nix/store/y411060mwl3rf77g8mp0hlnr5f254sar-unifont-15.1.02/share/fonts"
    FontPath        "/nix/store/jffby8ji8n8fivqjgp2wam0vmazaff90-font-adobe-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/79zfhk2pzqqcxvhq6pq8zgnf2nn8s4wr-font-adobe-75dpi-1.0.4/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/y4gd4pyhly98nl6pkk7hs7gkapps4m1v-font-adobe-utopia-100dpi-1.0.5/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/dr49dkd68g2w287spanf697h9f5nsry6-font-adobe-utopia-75dpi-1.0.5/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/1xbd51w7zzr2i9bngs6cbc7hwrix5vci-font-adobe-utopia-type1-1.0.5/lib/X11/fonts/Type1"
    FontPath        "/nix/store/xl39x1xmz6d30h57ass0qs170iki5ziz-font-arabic-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/a2bs9159hs1lgz9c6a6rzaq2l3mx68ai-font-bh-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/rcpwj5i3567w2bjl34j8xsd8i1wbfh30-font-bh-75dpi-1.0.4/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/9s0pnwqpimilz2c17wm459kn8iq5m8xw-font-bh-lucidatypewriter-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/lfa4066nmr1w7d0j4gh5r4b1200bqsq1-font-bh-lucidatypewriter-75dpi-1.0.4/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/5dxhlni95yrz63v4g6pw5vxh9d9qh7li-font-bh-ttf-1.0.4/lib/X11/fonts/TTF"
    FontPath        "/nix/store/s2xw9pnn16ak42p09kvkl3mc6cc29jg2-font-bh-type1-1.0.4/lib/X11/fonts/Type1"
    FontPath        "/nix/store/4z7mp5skak2v39h441lb86jlajjkr0yv-font-bitstream-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/4z7mp5skak2v39h441lb86jlajjkr0yv-font-bitstream-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/fnkpamzj0c7dc1kv743w9z8jc9bmbkc6-font-bitstream-75dpi-1.0.4/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/qjbwgbhlkfiicnmy0yqc6cxv9rrnkcy6-font-bitstream-type1-1.0.4/lib/X11/fonts/Type1"
    FontPath        "/nix/store/b5wr9mavsd46iwfya9n1f5mlcppd0g6m-font-cronyx-cyrillic-1.0.4/lib/X11/fonts/cyrillic"
    FontPath        "/nix/store/w906k8q5vzmlm3b6ffbpgqh03ambdlfk-font-cursor-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/12vwsxb6f377886lpk3000sd8imkb7cb-font-daewoo-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/hm3dvysg0f7xgccaqfhj48c0yc42vkzp-font-dec-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/dx2pjv69x8mr0km3c44cwdxz51617ih4-font-ibm-type1-1.0.4/lib/X11/fonts/Type1"
    FontPath        "/nix/store/ilw03pi2sv3f828bhdqih6lay563nvzz-font-isas-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/6klxmgwknvi0ylf0h2qw6k1bhhqsar13-font-jis-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/8y9nqj7yiifd6w1bp9cndgh6m3bxx73k-font-micro-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/nqr96wc1plvk23lssiqgnmh1mvnbf4jh-font-misc-cyrillic-1.0.4/lib/X11/fonts/cyrillic"
    FontPath        "/nix/store/b30za2bpsjfzh0kmg2wz14yn9g688p7j-font-misc-ethiopic-1.0.5/lib/X11/fonts/OTF"
    FontPath        "/nix/store/b30za2bpsjfzh0kmg2wz14yn9g688p7j-font-misc-ethiopic-1.0.5/lib/X11/fonts/TTF"
    FontPath        "/nix/store/4x5s99yijf8r0papvz676szwd93ipkb4-font-misc-meltho-1.0.4/lib/X11/fonts/OTF"
    FontPath        "/nix/store/3yrh3w7l7i0gp726dwh8hy2yi91yi8d1-font-misc-misc-1.1.3/lib/X11/fonts/misc"
    FontPath        "/nix/store/i2891sdxgs0wc5vpxdri2cp958x6q5kl-font-mutt-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/9j282r8q99ncdljmv54ddqa74z5i8cd7-font-schumacher-misc-1.1.3/lib/X11/fonts/misc"
    FontPath        "/nix/store/lb0w1naxd0nyj3mac73fkgsfznpskii4-font-screen-cyrillic-1.0.5/lib/X11/fonts/cyrillic"
    FontPath        "/nix/store/c003zrrm12ard37nkx52j3h40q82h6ma-font-sony-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/s6rixnxbswpp98nzjbmcryp457fijnfa-font-sun-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/ci01k8pdpr2y0jh7iasw3b0pm84pcnxd-font-winitzki-cyrillic-1.0.4/lib/X11/fonts/cyrillic"
    FontPath        "/nix/store/sk36d940425rlrpx3lf0qdppgfh5pkza-font-xfree86-type1-1.0.5/lib/X11/fonts/Type1"
    FontPath        "/nix/store/w906k8q5vzmlm3b6ffbpgqh03ambdlfk-font-cursor-misc-1.0.4/lib/X11/fonts/misc"
    FontPath        "/nix/store/3yrh3w7l7i0gp726dwh8hy2yi91yi8d1-font-misc-misc-1.1.3/lib/X11/fonts/misc"
    FontPath        "/nix/store/y411060mwl3rf77g8mp0hlnr5f254sar-unifont-15.1.02/share/fonts"
    FontPath        "/nix/store/jffby8ji8n8fivqjgp2wam0vmazaff90-font-adobe-100dpi-1.0.4/lib/X11/fonts/100dpi"
    FontPath        "/nix/store/79zfhk2pzqqcxvhq6pq8zgnf2nn8s4wr-font-adobe-75dpi-1.0.4/lib/X11/fonts/75dpi"
    FontPath        "/nix/store/is9wgzvl4ijkj1fc4rlx2nfpc1abh9ba-X11-fonts/share/X11/fonts"
EndSection

Section "Module"
EndSection

Section "ServerFlags"
    Option         "AllowMouseOpenFail" "on"
    Option         "DontZap" "on"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Keyboard0"
    Driver         "kbd"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/input/mice"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection

Section "InputClass"
    Identifier         "libinput mouse configuration"
    MatchIsPointer     "on"
    MatchDriver        "libinput"
    Option         "AccelProfile" "adaptive"
    Option         "LeftHanded" "off"
    Option         "MiddleEmulation" "on"
    Option         "NaturalScrolling" "off"
    Option         "ScrollMethod" "twofinger"
    Option         "HorizontalScrolling" "on"
    Option         "SendEventsMode" "enabled"
    Option         "Tapping" "on"
    Option         "TappingDragLock" "on"
    Option         "DisableWhileTyping" "off"
EndSection

Section "InputClass"
    Identifier         "libinput touchpad configuration"
    MatchIsTouchpad    "on"
    MatchDriver        "libinput"
    Option         "AccelProfile" "adaptive"
    Option         "LeftHanded" "off"
    Option         "MiddleEmulation" "on"
    Option         "NaturalScrolling" "off"
    Option         "ScrollMethod" "twofinger"
    Option         "HorizontalScrolling" "on"
    Option         "SendEventsMode" "enabled"
    Option         "Tapping" "off"
    Option         "TappingDragLock" "on"
    Option         "DisableWhileTyping" "off"
EndSection

Section "Monitor"
    Identifier     "Monitor[0]"
EndSection

Section "Monitor"
    Identifier     "DP-2"
    Option         "Primary" "true"
    Option         "Rotate" "inverted"
    Option         "PreferredMode" "3840x2160"
    Option         "Enable" "true"
EndSection

Section "Monitor"
    Identifier     "DP-0"
    Option         "RightOf" "DP-2"
    Option         "Rotate" "left"
    Option         "PreferredMode" "1680x1050"
    Option         "Position" "3840 0"
    Option         "Enable" "true"
EndSection

Section "Monitor"
    Identifier     "HDMI-0"
    Option         "RightOf" "DP-0"
    Option         "PreferredMode" "1920x1080"
    Option         "Enable" "true"
    Option         "Position" "4890 434"
EndSection

Section "Monitor"
    Identifier     "Monitor0"
    VendorName     "Unknown"
    ModelName      "DELL S3422DWG"
    HorizSync       29.0 - 200.0
    VertRefresh     48.0 - 120.0
EndSection

Section "Device"
    Identifier     "Device-amdgpu[0]"
    Driver         "amdgpu"
    BusID          "PCI:6:0:0"
EndSection

Section "Device"
    Identifier     "Device-nvidia[0]"
    Driver         "nvidia"
    BusID          "PCI:1:0:0"
EndSection

Section "Device"
    Identifier     "Device-nvidia"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "GeForce RTX 3070 mobile"
    Option         "Monitor-HDMI-0" "HDMI-0"
    Option         "Monitor-DP-0" "DP-0"
    Option         "Monitor-DP-2" "DP-2"
    Option         "UseDisplayDevice" "HDMI-1"
    Option         "RenderAccel" "true"
    BusID          "PCI:1:0:0"
    Screen          0
EndSection

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    BoardName      "NVIDIA GeForce RTX 3070 Laptop GPU"
EndSection

Section "Screen"
    Identifier     "Screen-nvidia[0]"
    Device         "Device-nvidia[0]"
    Monitor        "Monitor[0]"
    Option         "AllowEmptyInitialConfiguration"
EndSection

Section "Screen"
    Identifier     "Screen-nvidia-HDMI-0"
    Device         "Device-nvidia"
    Monitor        "HDMI-0"
    Option         "AllowEmptyInitialConfiguration"
EndSection

Section "Screen"
    Identifier     "Screen-nvidia-DP-0"
    Device         "Device-nvidia"
    Monitor        "DP-0"
    Option         "AllowEmptyInitialConfiguration"
EndSection

Section "Screen"
    Identifier     "Screen-nvidia-DP-2"
    Device         "Device-nvidia"
    Monitor        "DP-2"
    Option         "AllowEmptyInitialConfiguration"
EndSection

Section "Screen"
    Identifier     "Screen0"
    Device         "Device0"
    Monitor        "Monitor0"
    DefaultDepth    24
    Option         "Stereo" "0"
    Option         "nvidiaXineramaInfoOrder" "DFP-0"
#    Option         "metamodes" "DP-2: nvidia-auto-select +1050+120 {rotation=inverted}, HDMI-0: nvidia-auto-select +1050+120, DP-0: nvidia-auto-select +0+0 {rotation=left}"
    Option         "metamodes" "DP-2: nvidia-auto-select +0+0 {rotation=invert}, HDMI-0: nvidia-auto-select +4890+481, DP-0: nvidia-auto-select +3840+361 {rotation=left}"
    Option         "SLI" "Off"
    Option         "MultiGPU" "Off"
    Option         "BaseMosaic" "off"
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

'';
    displayManager = {
      sessionCommands =  ''
#         su - chous 'xmonad --recompile' || echo "nevermind"
#         ls -lthlia ~/.Xauthority ~/.xmonad >> /tmp/session.log
#         chown chous:users /home/chous/.Xauthority
#         rm -f /home/chous/.xmonad/xmonad-x86_64-linux
         xmodmap -e 'add mod3 = Multi_key'
      '';
    };
  };
}
