Screenshot in Sway with GUIX
Screenshots in Sway with Guix Home Manager
A blog post documented an elegant way to configure screenshots in
wayland using grim and grimshot. After moving my sway config to use
the GUIX sway-service to generate the config file Iost my configured
screenshot bindings.
Output Files
The screenshots will be placed in the Pictures/screenshots folder
with the timestamp as filename. To avoid duplication and ensure
consistency we'll add a variable to generate the filename
(sway-configuration
(variables (append %sway-default-variables
`((menu . ,#~(string-append
#$fuzzel
"/bin/fuzzel"
" -w 50 -x 8 -y 8 -r 3 -b 232635ff -t A6Accdff -s A6Accdff -S 232635ff -C c792eacc -m c792eacc -f \"JetBrains Mono:weight=light:size=10\" --icon-theme=\"Papirus-Dark\" --no-exit-on-keyboard-focus-loss"))
(term . ,(file-append alacritty "/bin/alacritty"))
(screenshot_out . "$(xdg-user-dir PICTURES)/screenshots/$(date +\"%Y%m%d-%H%M%S\").png"))
))
...)
Key Bindings
I often use various screenshots pasted in chat windows or Jira tickets so the clipboard is an important destination. Alternatively saving them to move them to a target folder for inclusion whereverr they needed is an important secondary use-case.
We can now use the variable made previously and add the following keybindings:
(sway-configuration
...
(keybindings (append
`(...
;; Special key to take a screenshot with grim
($mod+Shift+p . "exec grimshot copy anything")
($mod+Shift+s . "exec grimshot save screen $screenshot_out")
($mod+Shift+w . "exec grimshot save window $screenshot_out")
($mod+Shift+a . "exec grimshot save area $screenshot_out")
...
)
Deploying the configuration
To activate the configuration
$ make home
echo Loading modules from ~/.config/dotfiles/guix/src
Loading modules from /home/pti/.config/dotfiles/guix/src
guix home reconfigure -L ~/.config/dotfiles/guix/channel -c 32 ~/.config/dotfiles/guix/home-config-`hostname | cut -d. -f1`.scm
...
which quickly (for some value of quick) updates the home configuration, writing a new ~/.config/sway/config file.
When the prompt returns a quick reload of the sway config with
$mod+Shift+c and testing if the modifications are active.
Testing it Out
I opened libre office writer to paste copied screenshots and made a
mess to show the copy anything works as expected. It does, it
highlights what it understands depending on your mouse location, a
window if hovering over a window, a rectangle if you start selecting
and all the screen if you move the mouse outside all windows (i.e. the
boundary on a tiling wm). It is a bit fiddly so sometimes I select the
wrong thing, but since it only clobbers the clipboard and I can
quickly try again, this seems reasonable.
Similarly using the 3 other bindings to save different type of screenshots.
$ ls ~/Pictures/screenshots/
20251025-141106.png 20251025-145006.png 20251025-145019.png
Cool, that made a mess in the folder and displaying them show that it are actually screenshots. Cool.