diff --git a/modules/misc/news.nix b/modules/misc/news.nix index c3f7c548..580efcd6 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1185,6 +1185,14 @@ in Specifying them as strings is deprecated. ''; } + + { + time = "2019-09-17T19:33:49+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'services.sxhkd'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index c4261182..42f425d4 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -128,6 +128,7 @@ let (loadModule ./services/screen-locker.nix { }) (loadModule ./services/stalonetray.nix { }) (loadModule ./services/status-notifier-watcher.nix { }) + (loadModule ./services/sxhkd.nix { condition = hostPlatform.isLinux; }) (loadModule ./services/syncthing.nix { }) (loadModule ./services/taffybar.nix { }) (loadModule ./services/tahoe-lafs.nix { }) diff --git a/modules/services/sxhkd.nix b/modules/services/sxhkd.nix new file mode 100644 index 00000000..d9f0a968 --- /dev/null +++ b/modules/services/sxhkd.nix @@ -0,0 +1,86 @@ +{config, lib, pkgs, ...}: + +with lib; + +let + + cfg = config.services.sxhkd; + + keybindingsStr = concatStringsSep "\n" ( + mapAttrsToList (hotkey: command: + optionalString (command != null) '' + ${hotkey} + ${command} + '' + ) + cfg.keybindings + ); + +in + +{ + options.services.sxhkd = { + enable = mkEnableOption "simple X hotkey daemon"; + + keybindings = mkOption { + type = types.attrsOf (types.nullOr types.str); + default = {}; + description = "An attribute set that assigns hotkeys to commands."; + example = literalExample '' + { + "super + shift + {r,c}" = "i3-msg {restart,reload}"; + "super + {s,w}" = "i3-msg {stacking,tabbed}"; + } + ''; + }; + + extraConfig = mkOption { + default = ""; + type = types.lines; + description = "Additional configuration to add."; + example = literalExample '' + super + {_,shift +} {1-9,0} + i3-msg {workspace,move container to workspace} {1-10} + ''; + }; + + extraPath = mkOption { + default = ""; + type = types.envVar; + description = '' + Additional PATH entries to search for commands. + ''; + example = "/home/some-user/bin:/extra/path/bin"; + }; + }; + + config = mkIf cfg.enable { + home.packages = [ pkgs.sxhkd ]; + + xdg.configFile."sxhkd/sxhkdrc".text = concatStringsSep "\n" [ + keybindingsStr + cfg.extraConfig + ]; + + systemd.user.services.sxhkd = { + Unit = { + Description = "simple X hotkey daemon"; + After = [ "graphical-session-pre.target" ]; + PartOf = [ "graphical-session.target" ]; + }; + + Service = { + Environment = + "PATH=" + + "${config.home.profileDirectory}/bin" + + optionalString (cfg.extraPath != "") ":" + + cfg.extraPath; + ExecStart = "${pkgs.sxhkd}/bin/sxhkd"; + }; + + Install = { + WantedBy = [ "graphical-session.target" ]; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 355427ac..4bda0474 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -38,6 +38,7 @@ import nmt { // import ./modules/misc/xsession // import ./modules/programs/firefox // import ./modules/programs/rofi + // import ./modules/services/sxhkd // import ./modules/systemd ) // import ./modules/home-environment diff --git a/tests/modules/services/sxhkd/configuration.nix b/tests/modules/services/sxhkd/configuration.nix new file mode 100644 index 00000000..ac04a7ec --- /dev/null +++ b/tests/modules/services/sxhkd/configuration.nix @@ -0,0 +1,31 @@ +{ config, ... }: +{ + config = { + services.sxhkd = { + enable = true; + + keybindings = { + "super + a" = "run command a"; + "super + b" = null; + "super + Shift + b" = "run command b"; + }; + + extraConfig = '' + super + c + call command c + + # comment + super + d + call command d + ''; + }; + + nmt.script = '' + local sxhkdrc=home-files/.config/sxhkd/sxhkdrc + + assertFileExists $sxhkdrc + + assertFileContent $sxhkdrc ${./sxhkdrc} + ''; + }; +} diff --git a/tests/modules/services/sxhkd/default.nix b/tests/modules/services/sxhkd/default.nix new file mode 100644 index 00000000..ec25252c --- /dev/null +++ b/tests/modules/services/sxhkd/default.nix @@ -0,0 +1,4 @@ +{ + sxhkd-configuration = ./configuration.nix; + sxhkd-service = ./service.nix; +} diff --git a/tests/modules/services/sxhkd/service.nix b/tests/modules/services/sxhkd/service.nix new file mode 100644 index 00000000..46ce259a --- /dev/null +++ b/tests/modules/services/sxhkd/service.nix @@ -0,0 +1,20 @@ +{ config, ... }: +{ + config = { + services.sxhkd = { + enable = true; + extraPath = "/home/the-user/bin:/extra/path/bin"; + }; + + nmt.script = '' + local serviceFile=home-files/.config/systemd/user/sxhkd.service + + assertFileExists $serviceFile + + assertFileRegex $serviceFile 'ExecStart=.*/bin/sxhkd' + + assertFileRegex $serviceFile \ + 'Environment=PATH=.*\.nix-profile/bin:/home/the-user/bin:/extra/path/bin' + ''; + }; +} diff --git a/tests/modules/services/sxhkd/sxhkdrc b/tests/modules/services/sxhkd/sxhkdrc new file mode 100644 index 00000000..c8883464 --- /dev/null +++ b/tests/modules/services/sxhkd/sxhkdrc @@ -0,0 +1,13 @@ +super + Shift + b + run command b + +super + a + run command a + + +super + c + call command c + +# comment +super + d + call command d