From 3b75df4b94cb2896daec5818b63862cd38876808 Mon Sep 17 00:00:00 2001
From: Myned <dev@bjork.tech>
Date: Sat, 5 Apr 2025 19:07:14 -0500
Subject: [PATCH] wallpaper: convert to systemd service

---
 options/custom/default.nix                    |  1 -
 options/custom/desktops/hyprland/keywords.nix |  3 --
 options/custom/desktops/niri/misc.nix         | 14 +++-----
 options/custom/programs/hyprlock.nix          |  2 +-
 options/custom/programs/swaylock.nix          |  2 +-
 options/custom/services/wallpaper.nix         | 34 +++++++++++++++++++
 profiles/desktop/default.nix                  |  2 +-
 7 files changed, 42 insertions(+), 16 deletions(-)
 create mode 100644 options/custom/services/wallpaper.nix

diff --git a/options/custom/default.nix b/options/custom/default.nix
index b4481cb..99284f3 100644
--- a/options/custom/default.nix
+++ b/options/custom/default.nix
@@ -39,7 +39,6 @@ in {
 
     lockscreen = mkOption {default = "hyprlock";};
     menu = mkOption {default = "rofi";};
-    wallpaper = mkOption {default = false;};
 
     browser = {
       # TODO: Use lib.getExe' instead of /bin/ where possible
diff --git a/options/custom/desktops/hyprland/keywords.nix b/options/custom/desktops/hyprland/keywords.nix
index 98e8860..f72cc1a 100644
--- a/options/custom/desktops/hyprland/keywords.nix
+++ b/options/custom/desktops/hyprland/keywords.nix
@@ -105,9 +105,6 @@ in {
               "[workspace special:hidden silent] ${command loupe}"
               "[workspace special:hidden silent] ${command nautilus}"
             ]
-            ++ optionals config.custom.wallpaper [
-              (command wallpaper)
-            ]
             ++ optionals config.custom.settings.vm.passthrough.blacklist [
               # HACK: Delay driver initialization to work around reset issues
               (command "${virsh} list | ${grep} ${config.custom.settings.vm.passthrough.guest} || sudo ${modprobe} ${config.custom.settings.vm.passthrough.driver}")
diff --git a/options/custom/desktops/niri/misc.nix b/options/custom/desktops/niri/misc.nix
index 2622e4e..f7063a7 100644
--- a/options/custom/desktops/niri/misc.nix
+++ b/options/custom/desktops/niri/misc.nix
@@ -52,15 +52,11 @@ in {
           #!! Not executed in a shell
           # https://github.com/YaLTeR/niri/wiki/Configuration:-Key-Bindings#spawn
           # https://github.com/sodiboo/niri-flake/blob/main/docs.md#programsnirisettingsspawn-at-startup
-          spawn-at-startup =
-            [
-              {command = [audio "--init"];} # Enforce audio profile state
-              {command = [config.custom.menus.clipboard.clear-silent];} # Clear clipboard history
-              {command = [sway-audio-idle-inhibit];} # Inhibit while audio is playing
-            ]
-            ++ optionals config.custom.wallpaper [
-              {command = [wallpaper];}
-            ];
+          spawn-at-startup = [
+            {command = [audio "--init"];} # Enforce audio profile state
+            {command = [config.custom.menus.clipboard.clear-silent];} # Clear clipboard history
+            {command = [sway-audio-idle-inhibit];} # Inhibit while audio is playing
+          ];
 
           # https://github.com/sodiboo/niri-flake/blob/main/docs.md#programsnirisettingsswitch-eventslid-close
           switch-events = {
diff --git a/options/custom/programs/hyprlock.nix b/options/custom/programs/hyprlock.nix
index e3a7d37..cd86272 100644
--- a/options/custom/programs/hyprlock.nix
+++ b/options/custom/programs/hyprlock.nix
@@ -41,7 +41,7 @@ in {
             background = {
               blur_passes = 5;
               color = "rgb(073642)"; # Fallback
-              path = mkIf config.custom.wallpaper "/tmp/wallpaper.png";
+              path = mkIf config.custom.services.wallpaper.enable "/tmp/wallpaper.png";
             };
 
             # https://wiki.hyprland.org/Hypr-Ecosystem/hyprlock/#input-field
diff --git a/options/custom/programs/swaylock.nix b/options/custom/programs/swaylock.nix
index 6eb5642..eeabe16 100644
--- a/options/custom/programs/swaylock.nix
+++ b/options/custom/programs/swaylock.nix
@@ -32,7 +32,7 @@ in {
         indicator-radius = 150;
         font-size = 48 * config.custom.scale;
         font = config.stylix.fonts.monospace.name;
-        image = mkIf config.custom.wallpaper "/tmp/altered.png";
+        image = mkIf config.custom.services.wallpaper.enable "/tmp/altered.png";
         bs-hl-color = "93a1a1";
         color = "073642";
         inside-caps-lock-color = "002b36";
diff --git a/options/custom/services/wallpaper.nix b/options/custom/services/wallpaper.nix
new file mode 100644
index 0000000..c10708c
--- /dev/null
+++ b/options/custom/services/wallpaper.nix
@@ -0,0 +1,34 @@
+{
+  config,
+  lib,
+  ...
+}:
+with lib; let
+  cfg = config.custom.services.wallpaper;
+  hm = config.home-manager.users.${config.custom.username};
+in {
+  options.custom.services.wallpaper = {
+    enable = mkEnableOption "wallpaper";
+  };
+
+  config = mkIf cfg.enable {
+    home-manager.sharedModules = [
+      {
+        systemd.user.services.wallpaper = {
+          Unit = {
+            Description = "Periodically change wallpaper";
+          };
+
+          Install = {
+            WantedBy = ["default.target"];
+          };
+
+          Service = {
+            Type = "simple";
+            ExecStart = hm.home.file.".local/bin/wallpaper".source;
+          };
+        };
+      }
+    ];
+  };
+}
diff --git a/profiles/desktop/default.nix b/profiles/desktop/default.nix
index f91b57f..56749ea 100644
--- a/profiles/desktop/default.nix
+++ b/profiles/desktop/default.nix
@@ -3,11 +3,11 @@
     full = true;
     profile = "desktop";
     desktop = "niri";
-    wallpaper = true;
 
     services = {
       ollama.download = "high";
       power-profiles-daemon.enable = true;
+      wallpaper.enable = true;
     };
   };
 }