grimm-nixos-laptop/common/graphics/sway.nix

271 lines
7.7 KiB
Nix
Raw Normal View History

2024-05-07 23:31:41 +02:00
{
pkgs,
config,
lib,
...
}:
2024-04-10 16:51:28 +02:00
let
2024-05-11 22:55:59 +02:00
inherit (config.grimmShared) enable sway screens;
inherit (lib)
types
mkOption
mkEnableOption
mapAttrsToList
optionalString
concatMapStrings
isInt
min
max
foldl'
getExe
2024-09-21 09:43:57 +02:00
getExe'
2024-05-11 22:55:59 +02:00
isPath
isDerivation
concatLines
optional
mkIf
;
inherit (pkgs) writeShellScriptBin;
2024-05-07 23:31:41 +02:00
2024-05-11 22:55:59 +02:00
sway_conf = types.submodule (
{ ... }:
{
options = {
keybinds = mkOption {
type = types.attrsOf types.str;
default = { };
description = "set of keybinds assigning key combo to action";
};
2024-05-07 23:31:41 +02:00
2024-05-11 22:55:59 +02:00
autolaunch = mkOption {
type = types.listOf (types.either types.nonEmptyStr types.package);
default = [ ];
description = "set of commands to be run at sway startup";
};
2024-05-07 23:31:41 +02:00
2024-05-11 22:55:59 +02:00
execAlways = mkOption {
type = types.listOf (types.either types.nonEmptyStr types.package);
default = [ ];
description = "set of commands to be run at sway reload";
};
2024-05-07 23:31:41 +02:00
2024-05-11 22:55:59 +02:00
extraConfig = mkOption {
type = types.str;
default = "";
description = "additional sway config to be included";
};
2024-05-07 23:31:41 +02:00
2024-05-11 22:55:59 +02:00
definitions = mkOption {
type = types.attrsOf types.str;
default = { };
description = "set of definitions assigning variable to value";
2024-05-07 23:31:41 +02:00
};
2024-05-11 22:55:59 +02:00
modes = mkOption {
type = types.attrsOf sway_conf;
default = { };
description = "possible modes to switch to, e.g. resize";
};
};
}
);
2024-05-07 23:31:41 +02:00
build_screen_def =
fps_func:
let
output_def = mapAttrsToList (
name: value:
2024-05-02 15:04:38 +02:00
"output ${value.id} mode ${value.mode}@${toString (fps_func value.fps)}Hz"
+ (optionalString (value.pos != null) " position ${value.pos}")
2024-05-11 22:55:59 +02:00
) screens;
2024-05-07 23:31:41 +02:00
in
''
2024-09-21 09:43:57 +02:00
for pid in $(${getExe' pkgs.procps "pgrep"} sway -x)
2024-05-07 23:31:41 +02:00
do
2024-09-21 09:43:57 +02:00
uid=$(id -u $(${getExe' pkgs.procps "ps"} -o user= -p $pid))
2024-05-07 23:31:41 +02:00
export SWAYSOCK="/run/user/$uid/sway-ipc.$uid.$pid.sock"
if [[ -e "$SWAYSOCK" ]] ; then
echo "sock is $SWAYSOCK"
2024-09-21 09:43:57 +02:00
${getExe' config.programs.sway.package "swaymsg"} '${concatMapStrings (s: s + " ; ") output_def}'
2024-05-07 23:31:41 +02:00
fi
done
'';
2024-05-02 14:02:35 +02:00
2024-05-11 22:55:59 +02:00
fps_min = fps: if isInt fps then fps else (foldl' min 2147483647 fps);
fps_max = fps: if isInt fps then fps else (foldl' max 0 fps);
init_screens_min_fps = writeShellScriptBin "init-screens-min" (build_screen_def fps_min);
init_screens_max_fps = writeShellScriptBin "init-screens-max" (build_screen_def fps_max);
init_screens_auto = writeShellScriptBin "init-screens-auto" "which run-on-ac && which run-on-bat && run-on-ac ${getExe init_screens_max_fps} && run-on-bat ${getExe init_screens_min_fps} || ${getExe init_screens_max_fps}";
2024-04-10 16:51:28 +02:00
in
{
config =
let
2024-05-07 23:31:41 +02:00
bar_conf_file =
2024-05-11 22:55:59 +02:00
if (isPath sway.bar.config) then
sway.bar.config
2024-05-07 23:31:41 +02:00
else
2024-05-11 22:55:59 +02:00
pkgs.writers.writeJSON "config.json" sway.bar.config;
waybar_full = writeShellScriptBin "waybar-full" (
(getExe config.programs.waybar.package)
+ (optionalString (!isNull sway.bar.config) " -c ${bar_conf_file}")
+ (optionalString (!isNull sway.bar.style) " -s ${sway.bar.style}")
2024-03-26 14:18:33 +01:00
);
2024-03-24 16:59:47 +01:00
2024-04-10 16:51:28 +02:00
bar_config = ''
bar {
2024-05-02 14:02:35 +02:00
swaybar_command ${getExe waybar_full}
2024-04-10 16:51:28 +02:00
}
'';
2024-04-10 16:42:11 +02:00
2024-04-28 10:50:53 +02:00
dbus-sway-environment = pkgs.writeShellScriptBin "dbus-sway-environment" ''
dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP=sway
systemctl --user stop xdg-desktop-portal xdg-desktop-portal-wlr
systemctl --user start xdg-desktop-portal xdg-desktop-portal-wlr
'';
2024-05-07 23:31:41 +02:00
build_conf =
sway_conf:
2024-04-10 16:51:28 +02:00
let
build_definition_lines = mapAttrsToList (name: value: "set \$${name} ${value}");
build_keybind_lines = mapAttrsToList (key: value: "bindsym ${key} ${value}");
build_exec_lines = e: map (item: "${e} " + (if isDerivation item then (getExe item) else item));
2024-05-07 23:31:41 +02:00
build_mode_lines = mapAttrsToList (
name: value: ''
mode "${name}" {
${concatLines (map (item: " " + item) (build_conf value))}}''
);
2024-04-10 16:51:28 +02:00
in
2024-05-07 23:31:41 +02:00
(
[ ]
2024-04-10 16:51:28 +02:00
++ (build_definition_lines sway_conf.definitions)
++ (build_keybind_lines sway_conf.keybinds)
2024-05-02 14:02:35 +02:00
++ (build_exec_lines "exec_always" sway_conf.execAlways)
2024-09-29 14:57:54 +02:00
++ (build_exec_lines "exec" sway_conf.autolaunch)
2024-04-10 16:51:28 +02:00
++ (build_mode_lines sway_conf.modes)
++ optional (sway_conf.extraConfig != "") sway_conf.extraConfig
2024-04-10 16:51:28 +02:00
);
2024-04-10 16:42:11 +02:00
2024-05-11 22:55:59 +02:00
sway_conf = concatLines (
(build_conf sway.config)
++ optional sway.bar.enable bar_config
++ (mapAttrsToList (
2024-05-07 23:31:41 +02:00
name: value:
"output ${value.id} mode ${value.mode}"
2024-05-11 22:55:59 +02:00
+ (optionalString (value.pos != null) " position ${value.pos}")
) screens)
2024-04-10 16:51:28 +02:00
);
2024-03-24 16:59:47 +01:00
2024-04-20 19:50:23 +02:00
conf_path = "sway.conf";
2024-04-10 16:51:28 +02:00
in
2024-05-11 22:55:59 +02:00
mkIf (enable && sway.enable) {
2024-04-20 19:50:23 +02:00
environment.etc."${conf_path}".text = sway_conf;
2024-03-24 16:59:47 +01:00
2024-05-02 14:02:35 +02:00
grimmShared.sway.config.execAlways = [
dbus-sway-environment
init_screens_auto
];
2024-04-28 10:50:53 +02:00
2024-05-07 23:31:41 +02:00
environment.systemPackages =
[
waybar_full
dbus-sway-environment
init_screens_min_fps
init_screens_max_fps
init_screens_auto
]
++ (with pkgs; [
procps
slurp
libnotify
]);
2024-03-24 16:59:47 +01:00
2024-04-10 16:51:28 +02:00
systemd.services.reload-sway = {
description = "Reload all running sway instances";
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
script = ''
2024-09-21 09:43:57 +02:00
for pid in $(${getExe' pkgs.procps "pgrep"} sway -x)
2024-04-10 16:51:28 +02:00
do
2024-09-21 09:43:57 +02:00
uid=$(id -u $(${getExe' pkgs.procps "ps"} -o user= -p $pid))
2024-04-10 16:51:28 +02:00
export SWAYSOCK="/run/user/$uid/sway-ipc.$uid.$pid.sock"
if [[ -e "$SWAYSOCK" ]] ; then
echo "sock is $SWAYSOCK"
2024-09-21 09:43:57 +02:00
${getExe' config.programs.sway.package "swaymsg"} reload
2024-04-10 16:51:28 +02:00
fi
done
2024-04-21 16:02:55 +02:00
rm -rf /home/*/.cache/rmenu
2024-04-10 16:51:28 +02:00
'';
2024-04-20 19:50:23 +02:00
reloadTriggers = [ config.environment.etc."${conf_path}".source ];
2024-04-10 16:51:28 +02:00
};
2024-03-24 16:59:47 +01:00
2024-09-21 09:43:57 +02:00
# programs.waybar.enable = true;
2024-03-24 16:59:47 +01:00
2024-04-30 12:32:03 +02:00
programs.dconf.enable = true;
2024-04-10 16:51:28 +02:00
programs.sway = {
enable = true;
2024-04-20 19:50:23 +02:00
2024-04-10 16:51:28 +02:00
wrapperFeatures = {
gtk = true;
base = true;
};
extraPackages = with pkgs; [
2024-09-21 09:43:57 +02:00
# swaylock
2024-04-10 16:51:28 +02:00
swayidle
wl-clipboard
wf-recorder
dmenu
wmenu
waybar-mpris
];
extraOptions = [
"--config"
2024-04-20 19:50:23 +02:00
"/etc/${conf_path}"
2024-04-10 16:51:28 +02:00
];
extraSessionCommands = ''
2024-04-30 12:32:03 +02:00
# source /etc/profile
# test -f $HOME/.profile && source $HOME/.profile
2024-04-28 10:50:53 +02:00
systemctl --user import-environment
2024-04-21 16:02:55 +02:00
export XDG_CURRENT_DESKTOP=sway
export SDL_VIDEODRIVER=wayland
export QT_QPA_PLATFORM=wayland
export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
export _JAVA_AWT_WM_NONREPARENTING=1
export MOZ_ENABLE_WAYLAND=1
2024-04-26 10:25:17 +02:00
# export WLR_RENDERER=vulkan
# export DRI_PRIME=1
2024-04-21 16:02:55 +02:00
export NIXOS_OZONE_WL=1
2024-04-10 16:51:28 +02:00
'';
2024-03-24 16:59:47 +01:00
};
};
2024-05-11 22:55:59 +02:00
options.grimmShared.sway = {
enable = mkEnableOption "grimm-sway";
bar = {
enable = mkEnableOption "grimm-sway-bar";
style = mkOption {
type = types.nullOr types.path;
default = null;
description = "waybar style sheet to use";
};
config = mkOption {
2024-05-05 18:23:46 +02:00
type = types.nullOr (types.either types.path types.attrs);
default = null;
description = "waybar config to use";
};
};
config = mkOption {
type = sway_conf;
description = "sway config to use";
};
};
2024-03-24 16:59:47 +01:00
}