From 7ebda10e402468bf7f67ddb235c05a8836c92c14 Mon Sep 17 00:00:00 2001 From: cmacrae Date: Thu, 14 May 2020 15:09:40 +0100 Subject: [PATCH] module: add spacebar service --- modules/module-list.nix | 1 + modules/services/spacebar/default.nix | 74 +++++++++++++++++++++++++++ release.nix | 1 + tests/services-spacebar.nix | 27 ++++++++++ 4 files changed, 103 insertions(+) create mode 100644 modules/services/spacebar/default.nix create mode 100644 tests/services-spacebar.nix diff --git a/modules/module-list.nix b/modules/module-list.nix index bbe8e678..e639c027 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -51,6 +51,7 @@ ./services/privoxy ./services/redis ./services/skhd + ./services/spacebar ./services/synapse-bt.nix ./services/synergy ./services/yabai diff --git a/modules/services/spacebar/default.nix b/modules/services/spacebar/default.nix new file mode 100644 index 00000000..51ea998e --- /dev/null +++ b/modules/services/spacebar/default.nix @@ -0,0 +1,74 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.spacebar; + + toSpacebarConfig = opts: + concatStringsSep "\n" (mapAttrsToList + (p: v: "spacebar -m config ${p} ${toString v}") opts); + + configFile = mkIf (cfg.config != {} || cfg.extraConfig != "") + "${pkgs.writeScript "spacebarrc" ( + (if (cfg.config != {}) + then "${toSpacebarConfig cfg.config}" + else "") + + optionalString (cfg.extraConfig != "") cfg.extraConfig)}"; +in + +{ + options = with types; { + services.spacebar.enable = mkOption { + type = bool; + default = false; + description = "Whether to enable the spacebar spacebar."; + }; + + services.spacebar.package = mkOption { + type = path; + description = "The spacebar package to use."; + }; + + services.spacebar.config = mkOption { + type = attrs; + default = {}; + example = literalExample '' + { + clock_format = "%R"; + background_color = "0xff202020"; + foreground_color = "0xffa8a8a8"; + } + ''; + description = '' + Key/Value pairs to pass to spacebar's 'config' domain, via the configuration file. + ''; + }; + + services.spacebar.extraConfig = mkOption { + type = str; + default = ""; + example = literalExample '' + echo "spacebar config loaded..." + ''; + description = '' + Extra arbitrary configuration to append to the configuration file. + ''; + }; + }; + + config = mkIf (cfg.enable) { + environment.systemPackages = [ cfg.package ]; + + launchd.user.agents.spacebar = { + serviceConfig.ProgramArguments = [ "${cfg.package}/bin/spacebar" ] + ++ optionals (cfg.config != {} || cfg.extraConfig != "") [ "-c" configFile ]; + + serviceConfig.KeepAlive = true; + serviceConfig.RunAtLoad = true; + serviceConfig.EnvironmentVariables = { + PATH = "${cfg.package}/bin:${config.environment.systemPath}"; + }; + }; + }; +} diff --git a/release.nix b/release.nix index 4f006ac8..77eff196 100644 --- a/release.nix +++ b/release.nix @@ -121,6 +121,7 @@ let tests.services-offlineimap = makeTest ./tests/services-offlineimap.nix; tests.services-privoxy = makeTest ./tests/services-privoxy.nix; tests.services-skhd = makeTest ./tests/services-skhd.nix; + tests.services-spacebar = makeTest ./tests/services-spacebar.nix; tests.services-synapse-bt = makeTest ./tests/services-synapse-bt.nix; tests.services-synergy = makeTest ./tests/services-synergy.nix; tests.services-yabai = makeTest ./tests/services-yabai.nix; diff --git a/tests/services-spacebar.nix b/tests/services-spacebar.nix new file mode 100644 index 00000000..79257faf --- /dev/null +++ b/tests/services-spacebar.nix @@ -0,0 +1,27 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + spacebar = pkgs.runCommand "spacebar-0.0.0" {} "mkdir $out"; +in + +{ + services.spacebar.enable = true; + services.spacebar.package = spacebar; + services.spacebar.config = { background_color = "0xff202020"; }; + services.spacebar.extraConfig = ''echo "spacebar config loaded..."''; + + test = '' + echo >&2 "checking spacebar service in ~/Library/LaunchAgents" + grep "org.nixos.spacebar" ${config.out}/user/Library/LaunchAgents/org.nixos.spacebar.plist + grep "${spacebar}/bin/spacebar" ${config.out}/user/Library/LaunchAgents/org.nixos.spacebar.plist + + conf=`sed -En '/-c<\/string>/{n; s/\s+?<\/?string>//g; p;}' \ + ${config.out}/user/Library/LaunchAgents/org.nixos.spacebar.plist` + + echo >&2 "checking config in $conf" + grep "spacebar -m config background_color 0xff202020" $conf + grep "spacebar config loaded..." $conf + ''; +}