diff --git a/flake.nix b/flake.nix index e7bd4f1..66a6f99 100644 --- a/flake.nix +++ b/flake.nix @@ -8,6 +8,7 @@ }; outputs = { + self, nixpkgs, rust-overlay, ... @@ -40,5 +41,10 @@ timewall = pkgs.callPackage ./package.nix {}; default = timewall; }); + + homeManagerModules = rec { + timewall = import ./hm-module.nix self; + default = timewall; + }; }; } diff --git a/hm-module.nix b/hm-module.nix new file mode 100644 index 0000000..c77ae4b --- /dev/null +++ b/hm-module.nix @@ -0,0 +1,107 @@ +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" + (lib.optionalString cfg.wallpaperPath) + ]; + Install.WantedBy = [cfg.systemdTarget]; + }; + }; +}