sketchybar: config sourceFileOrLines

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman
2025-05-22 19:35:16 -05:00
parent abe66194b9
commit c096c39afc
6 changed files with 125 additions and 40 deletions

View File

@@ -45,12 +45,11 @@ in
};
config = mkOption {
type = with types; nullOr (either lines path);
default = "";
type = types.nullOr (lib.hm.types.sourceFileOrLines ".config/sketchybar" "sketchybarrc");
default = null;
example = literalExpression ''
# Bash example
#!/usr/bin/env bash
# String example - inline configuration
'''
# Define colors
export COLOR_BLACK="0xff181926"
export COLOR_WHITE="0xffcad3f5"
@@ -75,12 +74,28 @@ in
# Update the bar
sketchybar --update
'''
# Or directory example - for complex configurations
# {
# source = ./path/to/sketchybar-config;
# recursive = true;
# }
'';
description = ''
The complete sketchybar configuration content.
This should be written in the language specified by configType (bash or lua).
The sketchybar configuration. Can be specified as:
The appropriate shebang will be automatically added.
1. A string containing the configuration content directly
2. An attribute set with 'source' pointing to a directory containing
the full configuration, and optionally 'recursive = true' to
recursively copy all files
3. An attribute set with 'text' containing inline configuration
When using a string or 'text', the appropriate shebang will be
automatically added based on configType (bash or lua).
When using a directory source, it should contain a file named
"sketchybarrc" which serves as the main entry point.
'';
};
@@ -222,26 +237,6 @@ in
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;
}
);
launchd.agents.sketchybar = {
enable = cfg.service.enable;
config = {
@@ -253,5 +248,38 @@ in
StandardOutPath = cfg.service.outLogFile;
};
};
xdg.configFile = lib.mkIf (cfg.config != null) (
if cfg.config.source != null && cfg.config.recursive then
{
"sketchybar" = {
inherit (cfg.config) source recursive;
};
}
else if cfg.config.source != null then
{
"sketchybar/sketchybarrc".source = cfg.config.source;
}
else
{
"sketchybar/sketchybarrc".source = pkgs.writeTextFile {
name = "sketchybarrc";
text =
if cfg.configType == "lua" then
''
#!/usr/bin/env lua
-- Generated by home-manager
${cfg.config.text}
''
else
''
#!/usr/bin/env bash
# Generated by home-manager
${cfg.config.text}
'';
executable = true;
};
}
);
};
}

View File

@@ -1,6 +1,7 @@
{
sketchybar = ./sketchybar.nix; # Bash configuration with validation
sketchybar-lua-config = ./sketchybar-lua-config.nix; # Lua configuration with validation
sketchybar-invalid-lua-config = ./sketchybar-invalid-lua-config.nix; # Tests error on missing sbarLua
sketchybar-service-integration = ./sketchybar-service-integration.nix; # Service integration with validation
sketchybar = ./sketchybar.nix;
sketchybar-directory-config = ./sketchybar-directory-config.nix;
sketchybar-invalid-lua-config = ./sketchybar-invalid-lua-config.nix;
sketchybar-lua-config = ./sketchybar-lua-config.nix;
sketchybar-service-integration = ./sketchybar-service-integration.nix;
}

View File

@@ -0,0 +1,62 @@
{ config, pkgs, ... }:
let
# Create a mock directory structure for testing
configDir = pkgs.runCommand "sketchybar-config" { } ''
mkdir -p $out/plugins
cat > $out/sketchybarrc <<EOF
#!/usr/bin/env bash
# Main configuration file
source "$CONFIG_DIR/plugins/battery.sh"
source "$CONFIG_DIR/plugins/clock.sh"
sketchybar --bar height=32 position=top
sketchybar --update
EOF
chmod +x $out/sketchybarrc
cat > $out/plugins/battery.sh <<EOF
#!/usr/bin/env bash
sketchybar --add item battery right \\
--set battery script="\$CONFIG_DIR/plugins/battery.sh" \\
update_freq=10
EOF
cat > $out/plugins/clock.sh <<EOF
#!/usr/bin/env bash
sketchybar --add item clock right \\
--set clock script="date '+%H:%M'" \\
update_freq=60
EOF
'';
in
{
programs.sketchybar = {
enable = true;
package = config.lib.test.mkStubPackage {
name = "sketchybar";
buildScript = ''
mkdir -p $out/bin
touch $out/bin/sketchybar
chmod 755 $out/bin/sketchybar
'';
};
configType = "bash";
config = {
source = configDir;
recursive = true;
};
};
nmt.script = ''
assertFileExists home-files/.config/sketchybar/sketchybarrc
assertFileExists home-files/.config/sketchybar/plugins/battery.sh
assertFileExists home-files/.config/sketchybar/plugins/clock.sh
# Verify the main config file is executable
[[ -x home-files/.config/sketchybar/sketchybarrc ]] || \
echo "sketchybarrc should be executable"
'';
}

View File

@@ -64,7 +64,6 @@ in
sbar:update()'';
};
# Validate the generated Lua configuration file
nmt.script = ''
assertFileContent \
home-files/.config/sketchybar/sketchybarrc \

View File

@@ -9,7 +9,7 @@
<key>ProcessType</key>
<string>Interactive</string>
<key>Program</key>
<string>/@sketchybar@/bin/sketchybar</string>
<string>/nix/store/00000000000000000000000000000000-sketchybar/bin/sketchybar</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>

View File

@@ -38,7 +38,6 @@ in
sketchybar --update
'';
# Enable the integrated service
service = {
enable = true;
errorLogFile = "/home/hm-user/Library/Logs/sketchybar/sketchybar.err.log";
@@ -46,16 +45,12 @@ in
};
};
# Change home directory for the test
home.homeDirectory = "/home/hm-user";
# Validate the generated config files
nmt.script = ''
# Verify config file exists
assertFileExists home-files/.config/sketchybar/sketchybarrc
# Verify service file exists and matches expected content
serviceFile=LaunchAgents/org.nix-community.home.sketchybar.plist
serviceFile=$(normalizeStorePaths LaunchAgents/org.nix-community.home.sketchybar.plist)
assertFileExists "$serviceFile"
assertFileContent "$serviceFile" ${./sketchybar-service-expected.plist}
'';