arnaucube
arnaucube/configsPublic Self-hosted repository
configs/nixos/surface-extra-hardware-configuration.nix
nixos/surface-extra-hardware-configuration.nixRaw
  1# This is a custom file for a surface.
  2
  3{ config, lib, pkgs, modulesPath, ... }:
  4
  5{
  6  environment.systemPackages = with pkgs; [
  7	obs-studio
  8	rnote
  9	xournalpp
 10  ];
 11
 12
 13  # Set an usable key configuration: Ctrl at capslock, Win at Alt, Alt at Ctrl
 14  services.xserver.displayManager.sessionCommands =''
 15    ${pkgs.xorg.xmodmap}/bin/xmodmap "${pkgs.writeText  "xkb-layout" ''
 16      !
 17      ! Swap Caps_Lock and Control_L
 18      !
 19      
 20	remove Lock = Caps_Lock
 21	remove Control = Control_L
 22	! keysym Control_L = Caps_Lock
 23	keysym Control_L = Mode_switch
 24	keysym Caps_Lock = Control_L
 25	add Lock = Caps_Lock
 26	add Control = Control_L
 27
 28	! map arrows to more ergonomic position
 29	keycode  43 = h H Left
 30	keycode  44 = j J Down
 31	keycode  45 = k K Up 
 32	keycode  46 = l L Right 
 33    ''}"
 34  '';
 35
 36
 37  # add pulseaudio support to manage audio
 38  hardware.pulseaudio.enable = true;
 39
 40  # next sleep & wake is code from: https://github.com/hpfr/system/blob/2e5b3b967b0436203d7add6adbd6b6f55e87cf3c/hosts/linux-surface.nix
 41  systemd.services = {
 42    surface-sleep = {
 43      enable = lib.versionOlder config.boot.kernelPackages.kernel.version "5.4";
 44      before = [ "suspend.target" ];
 45      wantedBy = [ "suspend.target" ];
 46      serviceConfig.Type = "oneshot";
 47      path = with pkgs; [ procps kmod bluez ];
 48      script = ''
 49        # Disable bluetooth if no device is connected
 50        if ps cax | grep bluetoothd && ! bluetoothctl info; then
 51          bluetoothctl power off
 52        fi
 53
 54        ## Disable bluetooth regardless if devices are connected (see notes below)
 55        # if ps cax | grep bluetoothd; then
 56        #   bluetoothctl power off
 57        # fi
 58
 59        ## > Remove IPTS from ME side
 60        modprobe -r ipts_surface
 61        modprobe -r intel_ipts
 62        # modprobe -r mei_hdcp
 63        modprobe -r mei_me
 64        modprobe -r mei
 65        ## > Remove IPTS from i915 side
 66        for i in $(find /sys/kernel/debug/dri -name i915_ipts_cleanup); do
 67          echo 1 > $i
 68        done
 69      '';
 70    };
 71    surface-wake = {
 72      enable = lib.versionOlder config.boot.kernelPackages.kernel.version "5.4";
 73      after = [ "post-resume.target" ];
 74      wantedBy = [ "post-resume.target" ];
 75      serviceConfig.Type = "oneshot";
 76      path = with pkgs; [ procps kmod bluez ];
 77      script = ''
 78        ## > Load IPTS from i915 side
 79        for i in $(find /sys/kernel/debug/dri -name i915_ipts_init); do
 80          echo 1 > $i
 81        done
 82        ## > Load IPTS from ME side
 83        modprobe mei
 84        modprobe mei_me
 85        # modprobe mei_hdcp
 86        modprobe intel_ipts
 87        modprobe ipts_surface
 88
 89        # Restart bluetooth
 90        if ps cax | grep bluetoothd; then
 91          bluetoothctl power on
 92        fi
 93      '';
 94    };
 95  };
 96
 97## NOTES:
 98# Susspend issue:
 99# https://github.com/linux-surface/linux-surface/wiki/Known-Issues-and-FAQ#suspend-aka-sleep-vs-lid-closingopening-events
100# run:
101# > sudo modprobe -r surface_gpe
102# and:
103# > sudo bash -c 'echo -e "\n# Blacklisting lid vs. suspend issue module\nblacklist surface_gpe" >> /etc/modprobe.d/blacklist.conf'
104#
105# Now folding the keyboard to the screen will not suspend and brick the
106# session, but you will need to manually suspend the session if saving battery
107# is desired.
108}