sketchybar: use finalpackage wrapper

Use finalPackage pattern with wrapper so that people who don't use the
service can benefit from the other options.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman 2025-05-21 22:36:24 -05:00
parent 5dc3bc3368
commit e4b0102f69
10 changed files with 295 additions and 238 deletions

View file

@ -23,6 +23,13 @@ in
package = lib.mkPackageOption pkgs "sketchybar" { };
finalPackage = mkOption {
type = types.package;
readOnly = true;
internal = true;
description = "Resulting customized sketchybar package.";
};
configType = mkOption {
type = types.enum [
"bash"
@ -77,6 +84,38 @@ in
'';
};
sbarLuaPackage = lib.mkPackageOption pkgs "sbarlua" {
nullable = true;
extraDescription = "Required when using a lua configuration.";
};
luaPackage = lib.mkPackageOption pkgs "lua5_4" {
nullable = true;
extraDescription = "Lua interpreter to use when configType is lua.";
};
extraLuaPackages = mkOption {
type = with types; functionTo (listOf package);
default = _: [ ];
defaultText = literalExpression "ps: [ ]";
example = literalExpression "luaPkgs: with luaPkgs; [ luautf8 ]";
description = ''
The extra Lua packages required for your plugins to work.
This option accepts a function that takes a Lua package set as an argument,
and selects the required Lua packages from this package set.
See the example for more info.
'';
};
extraPackages = mkOption {
type = with lib.types; listOf package;
default = [ ];
example = literalExpression "[ pkgs.jq ]";
description = ''
Extra packages to add to PATH for the sketchybar service.
'';
};
service = {
enable = mkEnableOption "sketchybar service" // {
default = true;
@ -97,99 +136,122 @@ in
example = "/Users/khaneliman/Library/Logs/sketchybar.log";
description = "Absolute path to log all stdout output.";
};
sbarLuaPackage = lib.mkPackageOption pkgs "sbarlua" {
nullable = true;
extraDescription = "Required when using a lua configuration.";
};
extraLuaPackages = mkOption {
type = with types; functionTo (listOf package);
default = _: [ ];
defaultText = literalExpression "ps: [ ]";
example = literalExpression "luaPkgs: with luaPkgs; [ luautf8 ]";
description = ''
The extra Lua packages required for your plugins to work.
This option accepts a function that takes a Lua package set as an argument,
and selects the required Lua packages from this package set.
See the example for more info.
'';
};
extraPackages = mkOption {
type = with lib.types; listOf package;
default = [ ];
example = literalExpression "[ pkgs.jq ]";
description = ''
Extra packages to add to PATH for the sketchybar service.
'';
};
};
};
config = lib.mkMerge [
(lib.mkIf cfg.enable {
assertions = [
{
assertion = !(cfg.configType == "lua" && cfg.sbarLuaPackage == null);
message = "When configType is set to \"lua\", sbarLuaPackage must be specified";
config = lib.mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "programs.sketchybar" pkgs lib.platforms.darwin)
{
assertion = !(cfg.configType == "lua" && cfg.sbarLuaPackage == null);
message = "When configType is set to \"lua\", service.sbarLuaPackage must be specified";
}
];
programs.sketchybar.finalPackage =
let
resolvedExtraLuaPackages = cfg.extraLuaPackages pkgs.lua54Packages;
pathPackages =
[ cfg.package ]
++ cfg.extraPackages
++ lib.optional (cfg.configType == "lua" && cfg.luaPackage != null) cfg.luaPackage;
luaPaths = lib.filter (x: x != "") [
(lib.optionalString (cfg.configType == "lua" && resolvedExtraLuaPackages != [ ]) (
lib.concatMapStringsSep ";" pkgs.lua54Packages.getLuaPath resolvedExtraLuaPackages
))
(lib.optionalString (cfg.configType == "lua" && cfg.sbarLuaPackage != null) (
pkgs.lua54Packages.getLuaPath cfg.sbarLuaPackage
))
(lib.optionalString (cfg.configType == "lua" && cfg.config != null && cfg.config.source != null) (
let
configDir = "${config.xdg.configHome}/sketchybar";
in
"${configDir}/?.lua;${configDir}/?/init.lua;${configDir}/?/?.lua"
))
];
luaCPaths = lib.filter (x: x != "") [
(lib.optionalString (cfg.configType == "lua" && resolvedExtraLuaPackages != [ ]) (
lib.concatMapStringsSep ";" pkgs.lua54Packages.getLuaCPath resolvedExtraLuaPackages
))
(lib.optionalString (cfg.configType == "lua" && cfg.sbarLuaPackage != null) (
pkgs.lua54Packages.getLuaCPath cfg.sbarLuaPackage
))
];
makeWrapperArgs = lib.flatten (
lib.filter (x: x != [ ]) [
(lib.optional (pathPackages != [ ]) [
"--prefix"
"PATH"
":"
"${lib.makeBinPath pathPackages}"
])
(lib.optional (luaPaths != [ ]) [
"--prefix"
"LUA_PATH"
";"
"${lib.concatStringsSep ";" luaPaths}"
])
(lib.optional (luaCPaths != [ ]) [
"--prefix"
"LUA_CPATH"
";"
"${lib.concatStringsSep ";" luaCPaths}"
])
]
);
hasWrapperArgs = makeWrapperArgs != [ ];
in
if hasWrapperArgs then
pkgs.symlinkJoin {
name = "sketchybar";
paths = [ cfg.package ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/sketchybar ${lib.escapeShellArgs makeWrapperArgs}
'';
}
];
else
cfg.package;
home.packages = [ cfg.package ];
home.packages = [ cfg.finalPackage ];
xdg.configFile."sketchybar/sketchybarrc".source = lib.mkIf (cfg.config != "") (
pkgs.writeTextFile {
name = "sketchybarrc";
text =
if cfg.configType == "lua" then
''
#!/usr/bin/env lua
-- Generated by home-manager
${cfg.config}
''
else
''
#!/usr/bin/env bash
# Generated by home-manager
${cfg.config}
'';
executable = true;
}
);
})
xdg.configFile."sketchybar/sketchybarrc".source = lib.mkIf (cfg.config != "") (
pkgs.writeTextFile {
name = "sketchybarrc";
text =
if cfg.configType == "lua" then
''
#!/usr/bin/env lua
-- Generated by home-manager
${cfg.config}
''
else
''
#!/usr/bin/env bash
# Generated by home-manager
${cfg.config}
'';
executable = true;
}
);
(lib.mkIf cfg.service.enable {
assertions = [
(lib.hm.assertions.assertPlatform "programs.sketchybar" pkgs lib.platforms.darwin)
];
launchd.agents.sketchybar =
let
resolvedExtraLuaPackages = cfg.service.extraLuaPackages pkgs.lua54Packages;
in
{
enable = true;
config = {
Program = lib.getExe cfg.package;
ProcessType = "Interactive";
KeepAlive = true;
RunAtLoad = true;
StandardErrorPath = cfg.service.errorLogFile;
StandardOutPath = cfg.service.outLogFile;
EnvironmentVariables =
{
PATH = (lib.concatMapStringsSep ":" (p: "${p}/bin") ([ cfg.package ] ++ cfg.service.extraPackages));
}
// lib.optionalAttrs
(cfg.configType == "lua" && (cfg.sbarLuaPackage != null || cfg.extraLuaPackages != (_: [ ])))
{
LUA_CPATH = "${
lib.concatMapStringsSep ";" pkgs.lua54Packages.getLuaCPath resolvedExtraLuaPackages
};${cfg.sbarLuaPackage}/lib/lua/${pkgs.lua.luaversion}/?.so";
};
};
};
})
];
launchd.agents.sketchybar = {
enable = cfg.service.enable;
config = {
Program = lib.getExe cfg.finalPackage;
ProcessType = "Interactive";
KeepAlive = true;
RunAtLoad = true;
StandardErrorPath = cfg.service.errorLogFile;
StandardOutPath = cfg.service.outLogFile;
};
};
};
}