From 3e9468d8725d564bad5874b0a42357d386083e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sun, 7 May 2023 13:53:31 +0200 Subject: [PATCH] feat(wallpaper): add better wallpaper module --- modules/config/bin/default.nix | 4 +- modules/config/bin/randomWallpaper.nix | 17 ------ modules/profiles/desktop.nix | 1 + modules/programs/bspwm/default.nix | 2 +- modules/programs/hyprland/config.nix | 2 +- modules/programs/sway.nix | 2 +- modules/programs/wallpaper/default.nix | 24 +++++++++ modules/programs/wallpaper/wallpaper.sh | 70 +++++++++++++++++++++++++ 8 files changed, 99 insertions(+), 23 deletions(-) delete mode 100644 modules/config/bin/randomWallpaper.nix create mode 100644 modules/programs/wallpaper/default.nix create mode 100644 modules/programs/wallpaper/wallpaper.sh diff --git a/modules/config/bin/default.nix b/modules/config/bin/default.nix index 7f4520e..5cbf5ed 100644 --- a/modules/config/bin/default.nix +++ b/modules/config/bin/default.nix @@ -9,7 +9,6 @@ let cfg = config.my.bin; cycleSinks = import ./cycleSinks.nix { inherit pkgs; }; protonge = import ./protonge.nix { inherit pkgs; }; - randomWallpaper = import ./randomWallpaper.nix { inherit pkgs; }; share = import ./share.nix { inherit pkgs; }; sxhkdHelp = import ./sxhkdHelp.nix { inherit pkgs; }; in @@ -17,10 +16,9 @@ in options.my.bin.enable = mkEnableOption "bin"; config = mkIf cfg.enable { - environment.systemPackages = with pkgs; [ + environment.systemPackages = [ cycleSinks protonge - randomWallpaper share sxhkdHelp ]; diff --git a/modules/config/bin/randomWallpaper.nix b/modules/config/bin/randomWallpaper.nix deleted file mode 100644 index e9be064..0000000 --- a/modules/config/bin/randomWallpaper.nix +++ /dev/null @@ -1,17 +0,0 @@ -{ pkgs }: -pkgs.writeShellApplication { - name = "randomWallpaper"; - - runtimeInputs = with pkgs; [ findutils coreutils feh hyprland jq ]; - - text = '' - wallpaper="$(find ~/.config/wallpapers/ -type f,l | shuf -n 1)" - if [ -z ''${WAYLAND_DISPLAY+x} ]; then - feh --bg-fill "$wallpaper" - else - hyprctl hyprpaper preload "$wallpaper" - hyprctl monitors -j | jq '.[].name' | xargs -I{} hyprctl hyprpaper wallpaper '{}',"$wallpaper" - hyprctl hyprpaper unload all - fi - ''; -} diff --git a/modules/profiles/desktop.nix b/modules/profiles/desktop.nix index 855c581..ac348a0 100644 --- a/modules/profiles/desktop.nix +++ b/modules/profiles/desktop.nix @@ -29,6 +29,7 @@ with lib; { spotify.enable = mkDefault true; ssh.enable = mkDefault true; thunar.enable = mkDefault true; + wallpaper.enable = mkDefault true; zathura.enable = mkDefault true; }; services = { diff --git a/modules/programs/bspwm/default.nix b/modules/programs/bspwm/default.nix index 81e1a60..fba274c 100644 --- a/modules/programs/bspwm/default.nix +++ b/modules/programs/bspwm/default.nix @@ -45,7 +45,7 @@ in focus_follows_pointer = true; }; startupPrograms = [ - "randomWallpaper" + "wallpaper -r" ]; extraConfig = builtins.readFile ./bspwmrc; }; diff --git a/modules/programs/hyprland/config.nix b/modules/programs/hyprland/config.nix index bbc63ef..4672193 100644 --- a/modules/programs/hyprland/config.nix +++ b/modules/programs/hyprland/config.nix @@ -224,5 +224,5 @@ in bindm = $mainMod, mouse:272, movewindow bindm = $mainMod, mouse:273, resizewindow - exec-once=randomWallpaper + exec-once=wallpaper -r '' diff --git a/modules/programs/sway.nix b/modules/programs/sway.nix index 162a1b9..a11b077 100644 --- a/modules/programs/sway.nix +++ b/modules/programs/sway.nix @@ -71,7 +71,7 @@ in command = "systemctl --user restart waybar"; always = true; } - { command = "randomWallpaper"; } + { command = "wallpaper -r"; } ]; }; }; diff --git a/modules/programs/wallpaper/default.nix b/modules/programs/wallpaper/default.nix new file mode 100644 index 0000000..f8ec99c --- /dev/null +++ b/modules/programs/wallpaper/default.nix @@ -0,0 +1,24 @@ +{ lib, config, pkgs, ... }: + + +with lib; +let + cfg = config.my.programs.wallpaper; +in +{ + options.my.programs.wallpaper.enable = mkEnableOption "wallpaper"; + + config = mkIf cfg.enable { + environment.systemPackages = + let + wallpaper = pkgs.writeShellApplication { + name = "wallpaper"; + runtimeInputs = with pkgs; [ findutils coreutils feh hyprland jq fzf viu ]; + text = builtins.readFile ./wallpaper.sh; + }; + in + [ + wallpaper + ]; + }; +} diff --git a/modules/programs/wallpaper/wallpaper.sh b/modules/programs/wallpaper/wallpaper.sh new file mode 100644 index 0000000..2644566 --- /dev/null +++ b/modules/programs/wallpaper/wallpaper.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +WALLPAPERS_PATH="$HOME/.config/wallpapers" +WALLPAPERS=$(find "$WALLPAPERS_PATH" -type f,l) + +function help() { + echo "Usage: wallpaper [OPTIONS]" + echo "" + echo "Options:" + echo " -h, --help Show this help message and exit" + echo " -r, --random Set a random wallpaper" + echo " -s, --set Set a wallpaper" +} + +function randomWallpaper() { + find ~/.config/wallpapers/ -type f,l | shuf -n 1 +} + +function setWallpaper() { + case "$DESKTOP_SESSION" in + hyprland) + hyprctl hyprpaper preload "$1" &>/dev/null + hyprctl monitors -j | jq '.[].name' | xargs -I{} -P 0 hyprctl hyprpaper wallpaper '{}',"$1" &>/dev/null + hyprctl hyprpaper unload all &>/dev/null + ;; + *) + feh --bg-fill "$1" &>/dev/null + ;; + esac +} + +# Parse arguments +# https://stackoverflow.com/a/14203146 +POSITIONAL=() +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + -h | --help) + help + exit 0 + ;; + -r | --random) + WALLPAPER=$(randomWallpaper) + shift # past argument + ;; + -s | --set) + WALLPAPER="$2" + shift # past argument + shift # past value + ;; + *) + POSITIONAL+=("$1") # save it in an array for later + shift # past argument + ;; + esac +done + +set -- "${POSITIONAL[@]}" # restore positional arguments + +if [[ -z ${WALLPAPER+x} ]]; then + WALLPAPER=$(echo "$WALLPAPERS" | fzf --preview="viu -sb -h 30 {}" --preview-window=right:70%:wrap) +fi + +if [[ ! -f "$WALLPAPER" ]]; then + echo "File not found: $WALLPAPER" + exit 1 +fi + +setWallpaper "$WALLPAPER"