From 58e0bb7ea8aa33a8dbcc9446d27529e9d7b3bf28 Mon Sep 17 00:00:00 2001 From: Bazyli Cyran Date: Wed, 18 Dec 2024 21:11:57 +0100 Subject: [PATCH] WIP: feat(flake): Add home-manager module --- flake.nix | 5 ++ nix/hm-module.nix | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 nix/hm-module.nix diff --git a/flake.nix b/flake.nix index b1cc375..03dc849 100644 --- a/flake.nix +++ b/flake.nix @@ -46,5 +46,10 @@ timewall = pkgs.callPackage ./nix/package.nix {inherit rev;}; default = timewall; }); + + homeManagerModules = rec { + timewall = import ./nix/hm-module.nix self; + default = timewall; + }; }; } diff --git a/nix/hm-module.nix b/nix/hm-module.nix new file mode 100644 index 0000000..6b70a90 --- /dev/null +++ b/nix/hm-module.nix @@ -0,0 +1,114 @@ +self: { + config, + lib, + pkgs, + ... +}: let + inherit (pkgs.stdenv.hostPlatform) system; + cfg = config.services.timewall; + package = self.packages.${system}.default; + configFormat = pkgs.formats.toml {}; +in { + options = { + services.timewall = { + enable = lib.mkEnableOption "timewall"; + + package = lib.mkPackageOption self.packages.${system} "timewall" { + default = "default"; + pkgsText = "timewall.packages.\${pkgs.system}"; + }; + + systemdTarget = lib.mkOption { + type = lib.types.str; + default = "graphical-session.target"; + description = "Systemd target to bind to."; + }; + + wallpaperPath = lib.mkOption { + type = with lib.types; nullOr path; + default = null; + description = "Path to the HEIF/HEIC dyncamic wallpaper file to set."; + }; + + config = { + location = { + lat = lib.mkOption { + type = with lib.types; nullOr (either float int); + default = null; + description = "Your geographical latitude."; + }; + lon = lib.mkOption { + type = with lib.types; nullOr (either float int); + default = null; + description = "You geographical longitude."; + }; + }; + + setter = { + command = lib.mkOption { + type = with lib.types; nullOr (listOf str); + default = null; + description = '' + Command to set the wallpaper. Use "%f" as a placeholder for the file path. + The command is NOT passed to a shell. + ''; + example = ["sww" "img" "%f"]; + }; + }; + + daemon = { + update_interval_seconds = lib.mkOption { + type = with lib.types; nullOr int; + default = null; + description = "Interval between wallpaper updates in seconds."; + }; + }; + }; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + { + assertion = (cfg.config.location.lat != null) == (cfg.config.location.lon != null); + message = "Both `latitude and `longitude` must be set for timewall"; + } + ]; + + home.packages = [cfg.package]; + + xdg.configFile."timewall/config.toml".source = configFormat.generate "config.toml" ( + lib.optionalAttrs (cfg.config.location.lat != null && cfg.config.location.lon != null) { + inherit (cfg.config) location; + } + // lib.optionalAttrs (cfg.config.setter.command != null) { + inherit (cfg.config) setter; + } + // lib.optionalAttrs (cfg.config.daemon.update_interval_seconds != null) { + inherit (cfg.config) daemon; + } + ); + + systemd.user.services.timewall = { + Unit = { + Description = "Dynamic wallpapers daemon"; + PartOf = ["graphical-session.target"]; + X-Restart-Triggers = + lib.mkIf (cfg.config != {}) + ["${config.xdg.configFile."timewall/config.toml".source}"]; + }; + Service = { + ExecStart = builtins.concatStringsSep " " [ + "${lib.getExe package} set --daemon" + ( + if cfg.wallpaperPath != null + then builtins.toString cfg.wallpaperPath + else "" + ) + ]; + Restat = "on-failure"; + }; + Install.WantedBy = [cfg.systemdTarget]; + }; + }; +}