ctucx.git: nixfiles

ctucx' nixfiles

commit fcb244919e36097032539685c6e65131ed0d087c
parent 2235f30fd3e914483edb28e92e562d81bdbfa34f
Author: Leah (ctucx) <git@ctu.cx>
Date: Mon, 16 Jan 2023 13:04:06 +0100

configurations/darwin: add wifi-network-location changer
3 files changed, 80 insertions(+), 0 deletions(-)
M
configurations/darwin/default.nix
|
1
+
M
configurations/darwin/dock.nix
|
2
++
A
configurations/darwin/locationchanger.nix
|
77
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/configurations/darwin/default.nix b/configurations/darwin/default.nix
@@ -13,6 +13,7 @@
 #    ./yabai.nix
     ./syncthing.nix
     ./dock.nix
+    ./locationchanger.nix
 
     ../common/programs/gpg.nix
     ../common/programs/password-store.nix
diff --git a/configurations/darwin/dock.nix b/configurations/darwin/dock.nix
@@ -1,6 +1,7 @@
 { pkgs, ... }:
 
 {
+
   system.activationScripts.postActivation.text = ''
     ${pkgs.dockutil}/bin/dockutil \
       --remove all \

@@ -18,4 +19,5 @@
       --add "/System/Applications/Utilities/Activity Monitor.app" \
       --add "/System/Applications/System Settings.app"
   '';
+
 } 
\ No newline at end of file
diff --git a/configurations/darwin/locationchanger.nix b/configurations/darwin/locationchanger.nix
@@ -0,0 +1,76 @@
+{ pkgs, ... }:
+
+{
+
+  launchd.user.agents.locationchanger = {
+    serviceConfig = {
+      Label      = "org.nixos.locationchanger";
+      RunAtLoad  = true;
+      WatchPaths = [ "/Library/Preferences/SystemConfiguration" ];
+    };
+    script = ''
+      # This script changes network location based on the name of Wi-Fi network.
+      exec 2>&1 >> $HOME/Library/Logs/LocationChanger.log
+      sleep 3
+
+      ts() {
+          date +"[%Y-%m-%d %H:%M] $*"
+      }
+
+      ID=`whoami`
+      if [ "$ID" == "root" ]; then
+          ts "Running as root will read a bad config, exiting."
+          exit 1;
+      fi
+
+      SSID=`networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork | awk '{print $NF}'`
+      LOCATION_NAMES=`networksetup -listlocations`
+      CURRENT_LOCATION=`networksetup -getcurrentlocation`
+      CONFIG_FILE=$HOME/.locations/locations.conf
+
+      ts "Connected to '$SSID'"
+      ts "Probing '$CONFIG_FILE'"
+
+      if [ -f $CONFIG_FILE ]; then
+          ts "Reading to '$CONFIG_FILE'"
+          ESSID=`echo "$SSID" | sed 's/[.[\*^$]/\\\\&/g'`
+          NEW_SSID=`grep "^$ESSID=" $CONFIG_FILE | cut -d = -f 2`
+          if [ "$NEW_SSID" != "" ]; then
+              ts "Will switch the location to '$NEW_SSID' (configuration file)"
+              SSID=$NEW_SSID
+          else
+              ts "Will switch the location to '$SSID'"
+          fi
+      fi
+
+      ESSID=`echo "$SSID" | sed 's/[.[\*^$]/\\\\&/g'`
+
+      if echo "$LOCATION_NAMES" | grep -q "^$ESSID$"; then
+          NEW_LOCATION="$SSID"
+      else
+          if echo "$LOCATION_NAMES" | grep -q "^Automatic$"; then
+              NEW_LOCATION=Automatic
+              ts "Location '$SSID' was not found. Will default to 'Automatic'"
+          else
+              ts "Location '$SSID' was not found. The following locations are available: $LOCATION_NAMES"
+              exit 1
+          fi
+      fi
+
+      if [ "$NEW_LOCATION" != "" ]; then
+          if [ "$NEW_LOCATION" != "$CURRENT_LOCATION" ]; then
+              ts "Changing the location to '$NEW_LOCATION'"
+              scselect "$NEW_LOCATION"
+              SCRIPT="$HOME/.locations/$NEW_LOCATION"
+              if [ -f "$SCRIPT" ]; then
+                  ts "Running '$SCRIPT'"
+                  "$SCRIPT"
+              fi
+          else
+              ts "Already at '$NEW_LOCATION'"
+          fi
+      fi
+    '';
+  };
+
+}+
\ No newline at end of file