Skip to content

Commit

Permalink
services/yabai: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Jan 8, 2025
1 parent ba9b317 commit ba3e66d
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions modules/services/yabai/default.nix
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
{ config, lib, pkgs, ... }:

with lib;

{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mkOption types;

cfg = config.services.yabai;

toYabaiConfig = opts:
concatStringsSep "\n" (mapAttrsToList
(p: v: "yabai -m config ${p} ${toString v}")
opts);
toYabaiConfig =
opts:
lib.concatStringsSep "\n" (lib.mapAttrsToList (p: v: "yabai -m config ${p} ${toString v}") opts);

configFile = mkIf (cfg.config != { } || cfg.extraConfig != "")
"${pkgs.writeScript "yabairc" (
(if (cfg.config != {})
then "${toYabaiConfig cfg.config}"
else "")
+ optionalString (cfg.extraConfig != "") ("\n" + cfg.extraConfig + "\n"))}";
configFile =
lib.mkIf (cfg.config != { } || cfg.extraConfig != "")
"${pkgs.writeScript "yabairc" (
(if (cfg.config != { }) then "${toYabaiConfig cfg.config}" else "")
+ lib.optionalString (cfg.extraConfig != "") ("\n" + cfg.extraConfig + "\n")
)}";
in

{
options = with types; {
services.yabai.enable = mkOption {
type = bool;
options.services.yabai = {
enable = mkOption {
type = types.bool;
default = false;
description = "Whether to enable the yabai window manager.";
};

services.yabai.package = mkOption {
type = path;
package = mkOption {
type = types.path;
default = pkgs.yabai;
description = "The yabai package to use.";
};

services.yabai.enableScriptingAddition = mkOption {
type = bool;
enableScriptingAddition = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable yabai's scripting-addition.
SIP must be disabled for this to work.
'';
};

services.yabai.config = mkOption {
type = attrs;
config = mkOption {
type = types.attrs;
default = { };
example = literalExpression ''
example = lib.literalExpression ''
{
focus_follows_mouse = "autoraise";
mouse_follows_focus = "off";
Expand All @@ -62,41 +64,48 @@ in
'';
};

services.yabai.extraConfig = mkOption {
type = lines;
extraConfig = mkOption {
type = types.lines;
default = "";
example = literalExpression ''
example = lib.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) {
config = lib.mkMerge [
(lib.mkIf (cfg.enable) {
environment.systemPackages = [ cfg.package ];

launchd.user.agents.yabai = {
serviceConfig.ProgramArguments = [ "${cfg.package}/bin/yabai" ]
++ optionals (cfg.config != { } || cfg.extraConfig != "") [ "-c" configFile ];

serviceConfig.KeepAlive = true;
serviceConfig.RunAtLoad = true;
serviceConfig.EnvironmentVariables = {
PATH = "${cfg.package}/bin:${config.environment.systemPath}";
serviceConfig = {
ProgramArguments =
[ "${cfg.package}/bin/yabai" ]
++ lib.optionals (cfg.config != { } || cfg.extraConfig != "") [
"-c"
configFile
];
KeepAlive = true;
RunAtLoad = true;
EnvironmentVariables = {
PATH = "${cfg.package}/bin:${config.environment.systemPath}";
};
};
};
})

# TODO: [@cmacrae] Handle removal of yabai scripting additions
(mkIf (cfg.enableScriptingAddition) {
(lib.mkIf (cfg.enableScriptingAddition) {
launchd.daemons.yabai-sa = {
script = "${cfg.package}/bin/yabai --load-sa";
serviceConfig.RunAtLoad = true;
serviceConfig.KeepAlive.SuccessfulExit = false;
serviceConfig = {
RunAtLoad = true;
KeepAlive.SuccessfulExit = false;
};
};

environment.etc."sudoers.d/yabai".source = pkgs.runCommand "sudoers-yabai" {} ''
environment.etc."sudoers.d/yabai".source = pkgs.runCommand "sudoers-yabai" { } ''
YABAI_BIN="${cfg.package}/bin/yabai"
SHASUM=$(sha256sum "$YABAI_BIN" | cut -d' ' -f1)
cat <<EOF >"$out"
Expand Down

0 comments on commit ba3e66d

Please sign in to comment.