feat: use custom module for yabai+sip

This commit is contained in:
winston 2023-06-02 22:55:36 +02:00
parent 85770acd4c
commit 1f9f4732c5
Signed by: winston
GPG key ID: 3786770EDBC2B481
9 changed files with 195 additions and 114 deletions

View file

@ -78,7 +78,7 @@
useUserPackages = true; useUserPackages = true;
backupFileExtension = "backup"; backupFileExtension = "backup";
sharedModules = [ sharedModules = [
./modules ./modules/hm
inputs.nix-index-database.hmModules.nix-index inputs.nix-index-database.hmModules.nix-index
inputs.sops.homeManagerModules.sops inputs.sops.homeManagerModules.sops
]; ];
@ -123,6 +123,7 @@
system = "aarch64-darwin"; system = "aarch64-darwin";
modules = [ modules = [
home-manager.darwinModules.home-manager home-manager.darwinModules.home-manager
./modules/darwin
./machines/common ./machines/common
./machines/sashimi ./machines/sashimi
(commonHMConfig { (commonHMConfig {
@ -148,7 +149,7 @@
nixpkgs.overlays = [overlays]; nixpkgs.overlays = [overlays];
nixpkgs.config.allowUnfree = true; nixpkgs.config.allowUnfree = true;
} }
./modules ./modules/hm
./home ./home
inputs.nix-index-database.hmModules.nix-index inputs.nix-index-database.hmModules.nix-index
inputs.sops.homeManagerModules.sops inputs.sops.homeManagerModules.sops

View file

@ -1,110 +0,0 @@
{
config,
flakePath,
lib,
pkgs,
...
}: let
inherit (pkgs.stdenv.hostPlatform) isDarwin;
in {
xdg.configFile = lib.mkIf isDarwin {
"sketchybar" = {
source = config.lib.file.mkOutOfStoreSymlink "${flakePath}/home/apps/sketchybar";
recursive = true;
};
"yabai/yabairc" = {
text = let
rule = "yabai -m rule --add";
ignored = app: builtins.concatStringsSep "\n" (map (e: ''${rule} app="${e}" manage=off sticky=off layer=above border=off'') app);
unmanaged = app: builtins.concatStringsSep "\n" (map (e: ''${rule} app="${e}" manage=off'') app);
in ''
#!/usr/bin/env sh
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"
sudo yabai --load-sa
yabai -m config \
auto_balance off \
focus_follows_mouse off \
layout bsp \
mouse_drop_action swap \
mouse_follows_focus off \
window_animation_duration 0.0 \
window_border on \
window_border_blur on \
window_border_width 2 \
window_gap 5 \
left_padding 5 \
right_padding 5 \
top_padding 5 \
bottom_padding 5 \
window_origin_display default \
window_placement second_child \
window_shadow float \
active_window_border_color 0xfff5c2e7 \
normal_window_border_color 0xffcba6f7
# auto-inject scripting additions
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"
sudo yabai --load-sa
${ignored ["JetBrains Toolbox" "Mullvad VPN" "Sip" "iStat Menus"]}
${unmanaged ["GOG Galaxy" "Steam" "System Settings"]}
# etc.
${rule} manage=off border=off app="CleanShot"
${rule} manage=off sticky=on app="OBS Studio"
'';
executable = true;
};
"skhd/skhdrc" = {
text = let
mapKeymaps = with builtins;
cmd:
concatStringsSep "\n" (map (i:
replaceStrings ["Num"] [
(toString (
if (i == 10)
then 0
else i
))
]
cmd) (lib.range 1 10));
in ''
#!/usr/bin/env sh
# WORKS WITH SIP ENABLED:
# focus window
cmd + ctrl - h : yabai -m window --focus west
cmd + ctrl - j : yabai -m window --focus south
cmd + ctrl - k : yabai -m window --focus north
cmd + ctrl - l : yabai -m window --focus east
# move window
cmd + shift - h : yabai -m window --warp west
cmd + shift - j : yabai -m window --warp south
cmd + shift - k : yabai -m window --warp north
cmd + shift - l : yabai -m window --warp east
# toggle sticky/floating
cmd + shift - s: yabai -m window --toggle sticky --toggle float --toggle topmost
cmd + shift - d: yabai -m window --toggle float
# rotate
cmd + ctrl - e : yabai -m space --balance
cmd + ctrl - r : yabai -m space --rotate 270
# open terminal
cmd + shift - return : open -na "''${HOME}/Applications/Home Manager Apps/WezTerm.app"
# restart yabai
cmd + alt - r : brew services restart yabai
# ONLY WORKS WITH SIP DISABLED:
# fast focus space left/right
ctrl - left : yabai -m space --focus prev
ctrl - right : yabai -m space --focus next
# switch to space
${mapKeymaps "cmd + ctrl - Num : yabai -m space --focus Num"}
# send window to desktop and follow focus
${mapKeymaps "cmd + shift - Num : yabai -m window --space Num; yabai -m space --focus Num"}
'';
executable = true;
};
};
}

View file

@ -14,7 +14,6 @@ in {
./apps/gpg.nix ./apps/gpg.nix
./apps/i3.nix ./apps/i3.nix
./apps/kubernetes.nix ./apps/kubernetes.nix
./apps/macos.nix
./apps/mail.nix ./apps/mail.nix
./apps/media.nix ./apps/media.nix
./apps/neovim.nix ./apps/neovim.nix

View file

@ -1,4 +1,4 @@
{ {lib, ...}: {
# manipulate the global /etc/zshenv for PATH, etc. # manipulate the global /etc/zshenv for PATH, etc.
programs.zsh.enable = true; programs.zsh.enable = true;
@ -11,4 +11,97 @@
# Auto upgrade nix package and the daemon service. # Auto upgrade nix package and the daemon service.
services.nix-daemon.enable = true; services.nix-daemon.enable = true;
services = {
yabai = {
enable = true;
enableScriptingAddition = true;
logFile = "/var/tmp/yabai.log";
config = {
auto_balance = "off";
focus_follows_mouse = "off";
layout = "bsp";
mouse_drop_action = "swap";
mouse_follows_focus = "off";
window_animation_duration = "0.0";
window_border = "on";
window_border_blur = "on";
window_border_width = 2;
window_gap = 5;
left_padding = 5;
right_padding = 5;
top_padding = 5;
bottom_padding = 5;
window_origin_display = "default";
window_placement = "second_child";
window_shadow = "float";
active_window_border_color = "0xfff5c2e7";
normal_window_border_color = "0xffcba6f7";
};
extraConfig = let
rule = "yabai -m rule --add";
ignored = app: builtins.concatStringsSep "\n" (map (e: ''${rule} app="${e}" manage=off sticky=off layer=above border=off'') app);
unmanaged = app: builtins.concatStringsSep "\n" (map (e: ''${rule} app="${e}" manage=off'') app);
in ''
# auto-inject scripting additions
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"
sudo yabai --load-sa
${ignored ["JetBrains Toolbox" "Mullvad VPN" "Sip" "iStat Menus"]}
${unmanaged ["GOG Galaxy" "Steam" "System Settings"]}
# etc.
${rule} manage=off border=off app="CleanShot"
${rule} manage=off sticky=on app="OBS Studio"
'';
};
skhd = {
enable = true;
skhdConfig = let
mapKeymaps = with builtins;
cmd:
concatStringsSep "\n" (map (i:
replaceStrings ["Num"] [
(toString (
if (i == 10)
then 0
else i
))
]
cmd) (lib.range 1 10));
in ''
#!/usr/bin/env sh
# focus window
cmd + ctrl - h : yabai -m window --focus west
cmd + ctrl - j : yabai -m window --focus south
cmd + ctrl - k : yabai -m window --focus north
cmd + ctrl - l : yabai -m window --focus east
# move window
cmd + shift - h : yabai -m window --warp west
cmd + shift - j : yabai -m window --warp south
cmd + shift - k : yabai -m window --warp north
cmd + shift - l : yabai -m window --warp east
# toggle sticky/floating
cmd + shift - s: yabai -m window --toggle sticky --toggle float --toggle topmost
cmd + shift - d: yabai -m window --toggle float
# rotate
cmd + ctrl - e : yabai -m space --balance
cmd + ctrl - r : yabai -m space --rotate 270
# open terminal
cmd + shift - return : open -na "''${HOME}/Applications/Home Manager Apps/WezTerm.app"
# restart yabai
cmd + alt - r : brew services restart yabai
# ONLY WORKS WITH SIP DISABLED:
# fast focus space left/right
ctrl - left : yabai -m space --focus prev
ctrl - right : yabai -m space --focus next
# switch to space
${mapKeymaps "cmd + ctrl - Num : yabai -m space --focus Num"}
# send window to desktop and follow focus
${mapKeymaps "cmd + shift - Num : yabai -m window --space Num; yabai -m space --focus Num"}
'';
};
};
} }

View file

@ -0,0 +1,3 @@
{
imports = [./yabai.nix];
}

93
modules/darwin/yabai.nix Normal file
View file

@ -0,0 +1,93 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.yabai;
toYabaiConfig = opts:
concatStringsSep "\n" (mapAttrsToList
(p: v: "yabai -m config ${p} ${toString v}")
opts);
configFile = pkgs.writeScript "yabairc" (
optionalString cfg.enableScriptingAddition ''
${cfg.package}/bin/yabai -m signal --add event=dock_did_restart action="sudo ${cfg.package}/bin/yabai --load-sa"
sudo ${cfg.package}/bin/yabai --load-sa
''
+ optionalString (cfg.config != {}) ("\n" + (toYabaiConfig cfg.config) + "\n")
+ optionalString (cfg.extraConfig != "") ("\n" + cfg.extraConfig + "\n")
);
in {
disabledModules = ["services/yabai"];
options.services.yabai = with types; {
enable = mkEnableOption "Whether to enable the yabai window manager.";
package = mkPackageOption pkgs "yabai" {};
logFile = mkOption {
type = types.str;
default = "";
example = "/var/tmp/yabai.log";
description = "Path where you want to write daemon logs.";
};
enableScriptingAddition = mkEnableOption ''
Whether to enable yabai's scripting-addition.
SIP must be disabled for this to work.
'';
config = mkOption {
type = attrs;
default = {};
example = literalExpression ''
{
focus_follows_mouse = "autoraise";
mouse_follows_focus = "off";
window_placement = "second_child";
window_opacity = "off";
top_padding = 36;
bottom_padding = 10;
left_padding = 10;
right_padding = 10;
window_gap = 10;
}
'';
description = ''
Key/Value pairs to pass to yabai's 'config' domain, via the configuration file.
'';
};
extraConfig = mkOption {
type = str;
default = "";
example = literalExpression ''
yabai -m rule --add app='System Preferences' manage=off
'';
description = "Extra arbitrary configuration to append to the configuration file";
};
};
config = mkMerge [
(mkIf cfg.enable {
environment.systemPackages = [cfg.package];
launchd.user.agents.yabai.serviceConfig = rec {
ProgramArguments = ["${cfg.package}/bin/yabai" "-c" "${configFile}"];
KeepAlive = true;
RunAtLoad = true;
EnvironmentVariables.PATH = "${cfg.package}/bin:${config.environment.systemPath}";
StandardOutPath = mkIf (cfg.logFile != "") "${cfg.logFile}";
StandardErrorPath = StandardOutPath;
};
})
(mkIf cfg.enableScriptingAddition {
environment.etc."sudoers.d/yabai".text = let
sha = builtins.hashFile "sha256" "${cfg.package}/bin/yabai";
in "%admin ALL=(root) NOPASSWD: sha256:${sha} ${cfg.package}/bin/yabai --load-sa";
})
];
}

View file

@ -0,0 +1,2 @@
{
}