ctucx.git: nixfiles

ctucx' nixfiles

commit 4d112e8d58ac4c04d905797370f0b0a7fe45ea62
parent b01b4ca85ec640c001944959e1fca4518a330ce1
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sat, 11 Jun 2022 21:54:40 +0200

machines: add blechkasten
6 files changed, 529 insertions(+), 0 deletions(-)
A
machines/blechkasten/darwin-configuration.nix
|
208
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
machines/blechkasten/keyboard.nix
|
80
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
machines/blechkasten/services/syncthing.nix
|
20
++++++++++++++++++++
A
machines/blechkasten/skhd.nix
|
47
+++++++++++++++++++++++++++++++++++++++++++++++
A
machines/blechkasten/speakers.nix
|
30
++++++++++++++++++++++++++++++
A
machines/blechkasten/yabai.nix
|
144
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/machines/blechkasten/darwin-configuration.nix b/machines/blechkasten/darwin-configuration.nix
@@ -0,0 +1,208 @@
+{ config, pkgs, lib, ... }:
+
+let
+  bgiparser = pkgs.callPackage ../../pkgs/bgiparser.nix {};
+  asitop    = pkgs.callPackage ../../pkgs/asitop.nix {};
+
+in {
+
+  imports = [
+    <home-manager/nix-darwin>
+    ../../modules/darwin/syncthing.nix
+    ../../modules/darwin/quirks.nix
+
+    ../../helpers/make-nixpkgs.nix
+
+    ./services/syncthing.nix
+    ../../configurations/yubikey.nix
+    ../../configurations/homebrew.nix
+    ../../configurations/programs/cli/bash.nix
+    ../../configurations/programs/cli/micro.nix
+    ../../configurations/programs/cli/ssh.nix
+    ../../configurations/programs/cli/git.nix
+    ../../configurations/programs/cli/tmux.nix
+    ../../configurations/programs/cli/htop.nix
+    ../../configurations/programs/cli/password-store.nix
+    ../../configurations/programs/cli/texlive.nix
+    ../../configurations/programs/cli/utilities.nix
+    ../../configurations/programs/cli/network-utilities.nix
+    ../../configurations/programs/cli/scripts.nix
+    ../../configurations/programs/cli/garminexport.nix
+    ./yabai.nix
+    ./skhd.nix
+    ./keyboard.nix
+    ./speakers.nix
+  ];
+
+  nix.package = pkgs.nix;
+  services.nix-daemon.enable = true;
+
+  home-manager = {
+    useUserPackages = true;
+
+    users.leah.home.packages      = [ bgiparser asitop ];
+    users.leah.home.homeDirectory = lib.mkForce "/Users/leah/";
+  };
+
+  fonts = {
+    fontDir.enable = true;
+    fonts = with pkgs; [
+      iosevka-bin
+    ];
+  };
+
+  environment = {
+    darwinConfig   = "\$HOME/nixfiles/darwin-configuration.nix";
+    loginShell     = "${pkgs.bashInteractive}/bin/bash";
+    shells         = [ pkgs.bashInteractive ];
+    systemPackages = with pkgs; [
+      bashInteractive
+    ];
+  };
+
+  networking.hostName     = "blechkasten";
+  networking.computerName = config.networking.hostName;
+
+  system.activationScripts.postActivation.text = ''
+    # Set the default shell to bash. MacOS doesn't do this like nixOS does
+    sudo chsh -s ${pkgs.bashInteractive}/bin/bash leah
+
+    # Enable sudo Touch ID authentication, if not already enabled
+    if ! grep 'pam_tid.so' /etc/pam.d/sudo > /dev/null; then
+      sed -i "" '2i\
+    auth       sufficient     pam_tid.so # use TouchID with sudo
+      ' /etc/pam.d/sudo
+   fi
+  '';
+
+  system.activationScripts.extraSystemSettings.text = ''
+    # Sleep display after 30 minutes
+    #sudo pmset -a displaysleep 30
+    sudo systemsetup -settimezone "Europe/Berlin" > /dev/null
+  '';
+
+  system.activationScripts.extraUserActivation.text = ''
+    # Set accent color to green
+    defaults write .GlobalPreferences AppleAccentColor -int 3
+
+    # Show battery percentage in menubar
+    defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist BatteryShowPercentage -bool true
+
+    # Show sound-applet always in menubar
+    defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist Sound -int 16
+
+    # Set menubar clock format
+    defaults write com.apple.menuextra.clock "DateFormat" -string "\"d MMM HH:mm:ss\""
+
+    # Flash clock time seperators
+    defaults write com.apple.menuextra.clock "FlashDateSeparators" -bool "true"
+
+    # Set defaults for panes in Finder's "Get Info"
+    defaults write com.apple.finder FXInfoPanesExpanded -dict \
+      General -bool true \
+      OpenWith -bool true \
+      Privileges -bool true \
+      Preview -bool false
+
+    # Disable iTerm's annoying promt when quitting it
+    defaults write com.googlecode.iterm2 PromptOnQuit -bool false
+
+    # Disable RichText in TextEdit
+    defaults write com.apple.TextEdit RichText -int 0
+
+    # Open and save files as UTF-8 in TextEdit
+    defaults write com.apple.TextEdit PlainTextEncoding -int 4
+    defaults write com.apple.TextEdit PlainTextEncodingForWrite -int 4
+
+    # Prevent Photos from opening automatically when devices are plugged in
+    defaults -currentHost write com.apple.ImageCapture disableHotPlug -bool true
+
+    # drag windows by holding CTRL + CMD and clicking anywhere in the window
+    # does not work with all applications
+    defaults write -g NSWindowShouldDragOnGesture -bool true
+
+    # do not autogather large files when submitting a report
+    defaults write com.apple.appleseed.FeedbackAssistant "Autogather" -bool "false"
+
+    # disable "Application Downloaded from Internet" popup
+    defaults write com.apple.LaunchServices "LSQuarantine" -bool "false"
+
+    # disable animation when opening the info window in finder (CMD + I)
+    defaults write com.apple.finder DisableAllAnimations -bool true
+  '';
+
+  system.defaults = {
+    NSGlobalDomain = {
+      AppleInterfaceStyle                      = "Dark";
+      AppleInterfaceStyleSwitchesAutomatically = false;
+
+      AppleTemperatureUnit   = "Celsius";
+      AppleMeasurementUnits  = "Centimeters";
+      AppleMetricUnits       = 1;
+
+      AppleShowAllExtensions = true;
+      AppleShowScrollBars    = "Always";
+
+      # navigate UI elements with keyboard (tab key)
+      AppleKeyboardUIMode    = 3;
+
+      "com.apple.sound.beep.volume"        = "0.6";
+
+      NSTableViewDefaultSizeMode           = 2;
+
+      NSDocumentSaveNewDocumentsToCloud    = false;
+
+      NSNavPanelExpandedStateForSaveMode   = true;
+      NSNavPanelExpandedStateForSaveMode2  = true;
+
+      NSAutomaticCapitalizationEnabled     = false;
+      NSAutomaticDashSubstitutionEnabled   = false;
+      NSAutomaticPeriodSubstitutionEnabled = false;
+      NSAutomaticQuoteSubstitutionEnabled  = false;
+      NSAutomaticSpellingCorrectionEnabled = false;
+      
+    };
+
+    dock = {
+      tilesize                = 50;
+      minimize-to-application = true;
+      show-recents            = false;
+      # Dont rearrange my workspaces automagicly, thanks
+      mru-spaces              = false;
+
+      # Disable all hot corners
+      wvous-tl-corner         = 1;
+      wvous-tr-corner         = 1;
+      wvous-bl-corner         = 1;
+      wvous-br-corner         = 1;
+    };
+
+    finder = {
+      AppleShowAllExtensions         = true;
+      CreateDesktop                  = false;
+      ShowStatusBar                  = true;
+      ShowPathbar                    = false;
+      FXPreferredViewStyle           = "Nlsv";
+      FXDefaultSearchScope           = "SCcf";
+      FXEnableExtensionChangeWarning = false;
+    };
+
+    screencapture = {
+      disable-shadow = true;
+      location       = "/Users/leah/Pictures/Screenshots";
+    };
+
+    smb = {
+      NetBIOSName       = config.networking.hostName;
+      ServerDescription = config.networking.hostName;
+    };
+
+    trackpad = {
+      FirstClickThreshold  = 1;
+      SecondClickThreshold = 2;
+    };
+
+  };
+
+  system.stateVersion = 4;
+}
diff --git a/machines/blechkasten/keyboard.nix b/machines/blechkasten/keyboard.nix
@@ -0,0 +1,80 @@
+{ config, pkgs, lib, ... }:
+
+let
+  XPCEventStreamHandler = pkgs.callPackage ../../pkgs/XPCEventStreamHandler {};
+
+  keyMappings = [
+    {
+      # Internal keyboard
+      VendorID        = "5ac";
+      ProductID       = "281";
+      UserKeyMapping = [
+        # Remap tilde on non-US keyboards
+        { HIDKeyboardModifierMappingSrc = 30064771172; HIDKeyboardModifierMappingDst = 30064771125; }
+        # Swap right_command and right_option
+        { HIDKeyboardModifierMappingSrc = 30064771303; HIDKeyboardModifierMappingDst = 30064771302; }
+        { HIDKeyboardModifierMappingSrc = 30064771302; HIDKeyboardModifierMappingDst = 30064771303; }
+      ];
+    }
+    {
+      # Trackpoint Keyboard 2
+      VendorID       = "17ef";
+      ProductID      = "60ee";
+      UserKeyMapping = [
+        # Swap left_command and left_option
+        { HIDKeyboardModifierMappingSrc = 30064771299; HIDKeyboardModifierMappingDst = 30064771298; }
+        { HIDKeyboardModifierMappingSrc = 30064771298; HIDKeyboardModifierMappingDst = 30064771299; }
+        # Remap PrintScreen to left_command
+        { HIDKeyboardModifierMappingSrc = 30064771142; HIDKeyboardModifierMappingDst = 30064771299; }
+      ];
+    }
+  ];
+
+  # convert hex to int with nix!
+  pow      = base: exp: lib.foldl' (a: x: x * a) 1 (lib.genList (_: base) exp);
+  hexToDec = v:
+    let
+      hexToInt = { "0" = 0; "1" = 1;  "2" = 2; "3" = 3; "4" = 4;  "5" = 5; "6" = 6; "7" = 7;  "8" = 8; "9" = 9; "a" = 10; "b" = 11; "c" = 12;"d" = 13; "e" = 14; "f" = 15; };
+      chars    = lib.stringToCharacters v;
+      charsLen = lib.length chars;
+    in lib.foldl (a: v: a + v) 0 (lib.imap0 (k: v: hexToInt."${v}" * (pow 16 (charsLen - k - 1))) chars);
+
+  commandList  = lib.forEach keyMappings (entry: ''hidutil property --matching '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}' > /dev/null'');
+
+in {
+
+  launchd.user.agents = builtins.listToAttrs (lib.forEach keyMappings (entry: {
+    name  = "activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
+    value.serviceConfig = {
+      Disabled          = if (entry.VendorID != "5ac" && entry.ProductID != "281") then false else true;
+      Label             = "org.nixos.activateUserKeyMapping-${entry.VendorID}:${entry.ProductID}";
+#      StandardErrorPath = "/tmp/activateUserKeyMapping.stderr";
+#      StandardOutPath   = "/tmp/activateUserKeyMapping.stdout";
+      ProgramArguments  = [
+        "${XPCEventStreamHandler}/bin/xpc_set_event_stream_handler"
+        "${pkgs.writeScript "hidutil" ''
+          #!/usr/bin/env bash
+          osascript -e 'display notification "Load UserKeyMapping for ${entry.VendorID}:${entry.ProductID}" with title "hidutil"'
+          hidutil property --matching '{"VendorID":0x${entry.VendorID},"ProductID":0x${entry.ProductID}}' --set '{"UserKeyMapping":${builtins.toJSON entry.UserKeyMapping}}'  > /dev/null
+        ''}"
+      ];
+      LaunchEvents = {
+        "com.apple.iokit.matching" = {
+          "com.apple.device-attach" = {
+            IOMatchLaunchStream = true;
+            IOProviderClass     = "IOUSBDevice";
+            idVendor            = hexToDec entry.VendorID;
+            idProduct           = hexToDec entry.ProductID;
+          };
+        };
+      };
+    };
+  }));
+
+  system.activationScripts.keyboard.text = ''
+    # Configuring keyboard
+    echo "configuring keyboard..." >&2
+    ${lib.concatStringsSep "\n" commandList}
+  '';
+
+}
diff --git a/machines/blechkasten/services/syncthing.nix b/machines/blechkasten/services/syncthing.nix
@@ -0,0 +1,20 @@
+{ pkgs, config, lib, ... }:
+
+let
+  syncthingConfig = import ../../../configurations/syncthing.nix { inherit pkgs; inherit config; inherit lib; };
+
+in {
+
+  services = {
+    syncthing = {
+      enable  = true;
+
+      user    = "leah";
+      group   = "staff";
+
+      dataDir = syncthingConfig.dataDir;
+      devices = syncthingConfig.devices;
+      folders = syncthingConfig.folders;
+    };
+  };
+}
diff --git a/machines/blechkasten/skhd.nix b/machines/blechkasten/skhd.nix
@@ -0,0 +1,47 @@
+{ config, pkgs, ... }:
+
+let
+  newTerminal = pkgs.writeScript "newTerminalWindow.sh" ''
+    #!/usr/bin/env bash
+    # Open iTerm2 with single instance
+
+    # Detects if iTerm2 is running
+    if ! pgrep -f "iTerm" > /dev/null; then
+      open -a "$HOME/Applications/iTerm.app"
+    else
+      # Create a new window
+      if ! osascript -e 'tell application "iTerm2" to create window with default profile' > /dev/null; then
+        # Get pids for any app with "iTerm" and kill
+        for i in $(pgrep -f "iTerm"); do kill -15 "$i"; done
+        open  -a "$HOME/Applications/iTerm.app"
+      fi
+    fi
+  '';
+
+in {
+
+  home-manager.users.leah.home.packages = [ pkgs.skhd ];
+
+  services.skhd = {
+    enable     = true;
+    skhdConfig = ''
+      # spawn new terminal window
+      lalt - return : ${newTerminal}
+
+      # sleep
+      lalt - z : pmset sleepnow
+
+      # switch between workspaces
+      lalt - 1 : ${pkgs.skhd}/bin/skhd -k "ctrl - 1"
+      lalt - 2 : ${pkgs.skhd}/bin/skhd -k "ctrl - 2"
+      lalt - 3 : ${pkgs.skhd}/bin/skhd -k "ctrl - 3"
+      lalt - 4 : ${pkgs.skhd}/bin/skhd -k "ctrl - 4"
+      lalt - 5 : ${pkgs.skhd}/bin/skhd -k "ctrl - 5"
+      lalt - 6 : ${pkgs.skhd}/bin/skhd -k "ctrl - 6"
+      lalt - 7 : ${pkgs.skhd}/bin/skhd -k "ctrl - 7"
+      lalt - 8 : ${pkgs.skhd}/bin/skhd -k "ctrl - 8"
+      lalt - 9 : ${pkgs.skhd}/bin/skhd -k "ctrl - 9"
+    '';
+  };
+
+}
diff --git a/machines/blechkasten/speakers.nix b/machines/blechkasten/speakers.nix
@@ -0,0 +1,30 @@
+{ config, pkgs, lib, ... }:
+
+let
+  secrets        = import ../../secrets;
+  SpeakerDaemon  = pkgs.callPackage ../../pkgs/SpeakerDaemon {};
+  switchSpeakers = pkgs.writeScript "switchSpeakers.sh" ''
+    #!/usr/bin/env bash
+
+    curl -d '{"accessToken": "${secrets.hosts.lollo.smartied.accessToken}", "type": "SwitchStateAction", "deviceName": "modbus-20", "relay": 0, "toggle": true}' http://10.0.0.1:5000
+  '';
+
+in {
+
+  services.skhd = {
+    enable     = true;
+    skhdConfig = ''
+      # Switch speakers
+      lalt - delete : ${switchSpeakers}
+    '';
+  };
+
+  launchd.user.agents.SpeakerDaemon = {
+    path = [ config.environment.systemPath ];
+
+    serviceConfig = {
+      ProgramArguments = [ "${SpeakerDaemon}/bin/SpeakerDaemon" ];
+      KeepAlive        = true;
+    };
+  };
+}
diff --git a/machines/blechkasten/yabai.nix b/machines/blechkasten/yabai.nix
@@ -0,0 +1,144 @@
+{ config, pkgs, ... }:
+
+let
+  yabai       = pkgs.callPackage ../../pkgs/yabai.nix {};
+
+  toggleFloating = pkgs.writeScript "yabaiToggleFloating.sh" ''
+    #!/usr/bin/env bash
+    PATH=$(eval echo $PATH)
+
+    # Toggle the workspace layout between float and bsp
+    read -r curType index <<< $(echo $(yabai -m query --spaces --space | jq '.type, .index'))
+
+    if [ $curType = '"bsp"' ]; then
+      yabai -m space --layout float
+      osascript -e "display notification \"Change the layout of workspace $index to float\" with title \"yabai\""
+    else
+      yabai -m space --layout bsp
+      osascript -e "display notification \"Change the layout of workspace $index to bsp\" with title \"yabai\""
+    fi
+  '';
+
+in {
+
+  environment.etc."sudoers.d/yabai".text = ''
+    leah ALL = (root) NOPASSWD: ${yabai}/bin/yabai --load-sa
+  '';
+
+  services.yabai = {
+    enable                  = true;
+    # currently not working :'(
+    # enableScriptingAddition = true;
+    package                 = yabai;
+    config                  = {
+      layout              = "float";
+
+      window_placement    = "second_child";
+      window_topmost      = "off";
+      window_shadow       = "off";
+      window_opacity      = "off";
+
+#      focus_follows_mouse = "autofocus";
+      mouse_follows_focus = "off";
+
+      mouse_modifier      = "alt";
+      mouse_action1       = "move";
+      mouse_action2       = "resize";
+
+    };
+
+    extraConfig = ''
+      #sudo yabai --load-sa
+
+      # load scripting-additions when Dock.app restarts
+      #yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"
+
+      # fix focus when closing windows
+      yabai -m signal --add event=window_destroyed action="yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse"
+      yabai -m signal --add event=application_terminated action="yabai -m query --windows --window &> /dev/null || yabai -m window --focus mouse"
+
+      # float system preferences
+      yabai -m rule --add app='^System Information$' manage=off layer=above
+      yabai -m rule --add app='^System Preferences$' manage=off layer=above
+      yabai -m rule --add title='Preferences$' manage=off layer=above
+
+      # float settings and file windows
+      yabai -m rule --add title='Settings$' manage=off layer=above
+      yabai -m rule --add title='Opening$' manage=off layer=above
+
+      # float Finder and AppStore
+      #yabai -m rule --add app='Finder' manage=off layer=above
+      #yabai -m rule --add app='App Store' manage=off layer=above
+
+      #float firefox PiP window
+      #yabai -m rule --add label="Firfox PIP" app="^Firefox$" title="^(Picture-in-Picture)$" manage=off layer=above sticky=on
+
+      yabai -m rule --add app='pinentry-mac' manage=off layer=above sticky=on
+
+      #float Calculator
+      yabai -m rule --add app='Calculator' manage=off layer=above sticky=on
+
+      yabai -m rule --add app='Firefox' space=1
+      yabai -m rule --add app='Mail'    space=2
+      yabai -m rule --add app='Finder'  space=6
+    '';
+  };
+
+  services.skhd = {
+    enable     = true;
+    skhdConfig = ''
+      # kill focused window
+      lalt + shift - q : yabai -m window --close
+
+      # float / unfloat window and center on screen
+      #lalt - space : yabai -m window --toggle float; yabai -m window --grid 4:4:1:1:2:2
+
+      # enter fullscreen mode for the focused window
+      lalt - f : yabai -m window --toggle zoom-fullscreen
+
+      # change layout of desktop
+      lalt - e : ${toggleFloating}
+
+      # rotate tree
+      lalt - r : yabai -m space --rotate 90
+
+      # change focus (using arrow keys)
+      lalt - left  : yabai -m window --focus west
+      lalt - right : yabai -m window --focus east
+      lalt - down  : yabai -m window --focus south
+      lalt - up    : yabai -m window --focus north
+
+      # move managed window
+      shift + lalt - left  : yabai -m window --warp west
+      shift + lalt - right : yabai -m window --warp east
+      shift + lalt - down  : yabai -m window --warp south
+      shift + lalt - up    : yabai -m window --warp north
+
+      # move between workspaces
+      #lalt - 1 : yabai -m space --focus 1
+      #lalt - 2 : yabai -m space --focus 2
+      #lalt - 3 : yabai -m space --focus 3
+      #lalt - 4 : yabai -m space --focus 4
+      #lalt - 5 : yabai -m space --focus 5
+      #lalt - 6 : yabai -m space --focus 6
+      #lalt - 7 : yabai -m space --focus 7
+      #lalt - 8 : yabai -m space --focus 8
+      #lalt - 9 : yabai -m space --focus 9
+
+      # go back to previous workspace (kind of like back_and_forth in i3)
+      lalt - 0 : yabai -m space --focus recent
+
+      # move focused window to workspace
+      lalt + shift - 1 : yabai -m window --space 1
+      lalt + shift - 2 : yabai -m window --space 2
+      lalt + shift - 3 : yabai -m window --space 3
+      lalt + shift - 4 : yabai -m window --space 4
+      lalt + shift - 5 : yabai -m window --space 5
+      lalt + shift - 6 : yabai -m window --space 6
+      lalt + shift - 7 : yabai -m window --space 7
+      lalt + shift - 8 : yabai -m window --space 8
+      lalt + shift - 9 : yabai -m window --space 9
+    '';
+  };
+
+}