commit 71129a3a91c7d5cc3c107de894348990c4fcb954
parent 210a029e037305f6ddc5c8e9ade3551a0993c3f3
Author: Leah (ctucx) <git@ctu.cx>
Date: Tue, 17 Jan 2023 17:00:44 +0100
parent 210a029e037305f6ddc5c8e9ade3551a0993c3f3
Author: Leah (ctucx) <git@ctu.cx>
Date: Tue, 17 Jan 2023 17:00:44 +0100
configurations/darwin: add `uhubDaemon`
4 files changed, 112 insertions(+), 1 deletion(-)
A
|
96
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/configurations/darwin/default.nix b/configurations/darwin/default.nix @@ -10,10 +10,10 @@ ./homebrew.nix ./finder.nix ./skhd.nix -# ./yabai.nix ./syncthing.nix ./dock.nix ./locationchanger.nix + ./uhubDaemon.nix ../common/programs/gpg.nix ../common/programs/password-store.nix
diff --git a/configurations/darwin/uhubDaemon.nix b/configurations/darwin/uhubDaemon.nix @@ -0,0 +1,14 @@ +{ inputs, config, pkgs, lib, ... }: + +{ + + launchd.user.agents.uhubDaemon = { + path = [ config.environment.systemPath ]; + + serviceConfig = { + ProgramArguments = [ "${pkgs.uhubDaemon}/bin/uhubDaemon" ]; + KeepAlive = true; + }; + }; + +}
diff --git a/pkgs/darwin-overlay.nix b/pkgs/darwin-overlay.nix @@ -5,6 +5,7 @@ final: prev: yabai = final.callPackage ./yabai.nix {}; asitop = final.callPackage ./asitop.nix {}; bgiparser = final.callPackage ./bgiparser.nix {}; + uhubDaemon = final.callPackage ./uhubDaemon.nix {}; XPCEventStreamHandler = final.callPackage ./XPCEventStreamHandler {}; # jemalloc depends on llvm10 which is doesn't supports darwin-aarch64
diff --git a/pkgs/uhubDaemon.nix b/pkgs/uhubDaemon.nix @@ -0,0 +1,96 @@ +{ stdenv, uhubctl, runCommand, writeText, ...}: + +let + uhubDaemon = writeText "uhubDaemon.c" '' + #include <ctype.h> + #include <stdlib.h> + #include <stdio.h> + + #include <mach/mach_port.h> + #include <mach/mach_interface.h> + #include <mach/mach_init.h> + + #include <IOKit/pwr_mgt/IOPMLib.h> + #include <IOKit/IOMessage.h> + + io_connect_t root_port; // a reference to the Root Power Domain IOService + + void MySleepCallBack( void * refCon, io_service_t service, natural_t messageType, void * messageArgument ) { + switch ( messageType ) { + case kIOMessageCanSystemSleep: + IOAllowPowerChange( root_port, (long)messageArgument ); + break; + + case kIOMessageSystemWillSleep: + system("${uhubctl}/bin/uhubctl -n 0bda -p 2-2 -a off"); + IOAllowPowerChange( root_port, (long)messageArgument ); + break; + + case kIOMessageSystemWillPowerOn: + system("${uhubctl}/bin/uhubctl -n 0bda -p 2-2 -a on"); + break; + + case kIOMessageSystemHasPoweredOn: + //System has finished waking up... + break; + + default: + break; + } + } + + int main( int argc, char **argv ) { + // notification port allocated by IORegisterForSystemPower + IONotificationPortRef notifyPortRef; + + // notifier object, used to deregister later + io_object_t notifierObject; + // this parameter is passed to the callback + void* refCon; + + // register to receive system sleep notifications + root_port = IORegisterForSystemPower( refCon, ¬ifyPortRef, MySleepCallBack, ¬ifierObject ); + if ( root_port == 0 ) { + printf("IORegisterForSystemPower failed\n"); + return 1; + } + + // add the notification port to the application runloop + CFRunLoopAddSource( CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes ); + + /* Start the run loop to receive sleep notifications. Don't call CFRunLoopRun if this code + is running on the main thread of a Cocoa or Carbon application. Cocoa and Carbon + manage the main thread's run loop for you as part of their event handling + mechanisms. + */ + CFRunLoopRun(); + + //Not reached, CFRunLoopRun doesn't return in this case. + return (0); + } + ''; + + buildSymlinks = runCommand "macvim-build-symlinks" {} '' + mkdir -p $out/bin + ln -s /usr/bin/gcc $out/bin + ''; + +in stdenv.mkDerivation { + name = "uhubDaemon"; + src = ./.; + + nativeBuildInputs = [ buildSymlinks ]; + + sandboxProfile = '' + (allow file-read* file-write* process-exec mach-lookup) + ; block homebrew dependencies + (deny file-read* file-write* process-exec mach-lookup (subpath "/usr/local") (with no-log)) + ''; + + buildPhase = "gcc -framework IOKit -framework Cocoa -lcurl -o uhubDaemon ${uhubDaemon}"; + + installPhase = '' + mkdir -p $out/bin + cp uhubDaemon $out/bin/ + ''; +}