feat(nix): add nix-darwin module

This commit adds a nix-darwin module for komorebi and komorebi-bar,
exposed via `darwinModules.default` and `darwinModules.komorebi`.

The module provides the following options:

* `services.komorebi.enable`: enables the komorebi launchd agent
* `services.komorebi.config`: typed Nix configuration (defaults to example
  config)
* `services.komorebi.configFile`: path to a JSON config file (validated
  against schema)
* `services.komorebi.logLevel`: log verbosity level
* `services.komorebi.applicationSpecificConfiguration`: fetches
  app-specific configs from remote GitHub repositories at pinned
  revisions

* `services.komorebi-bar.enable`: enables komorebi-bar launchd agent(s)
* `services.komorebi-bar.bars.<name>`: named bar instances, one per
  monitor
* `services.komorebi-bar.bars.<name>.config`: typed Nix configuration per
  bar
* `services.komorebi-bar.bars.<name>.configFile`: path to a JSON config
  file per bar

The applicationSpecificConfiguration option supports multiple sources,
allowing users to combine the upstream default from
LGUG2Z/komorebi-application-specific-configuration with their own
personal overrides from other repositories.

When configFile is specified, the JSON file is validated against the
flake's version of the schema before being passed to the binary.

A new flake check, `darwin-module-validation`, validates the module
options and exercises the configFile validation against the example
JSON files loaded into `komorebic` for the `quickstart` cmd.
This commit is contained in:
LGUG2Z committed 2025-12-24 17:18:44 -08:00
1 parent 23aca28267
commit b51eb8aab2
4 files changed
+628 -5

No files matched your search

+5
View File
@@ -167,6 +167,8 @@
komorebi-workspace-nextest = build.craneLib.cargoNextest build.individualCrateArgs;
nix-options-validation = import ./nix/tests { inherit pkgs; };
darwin-module-validation = import ./nix/tests/darwin-module.nix { inherit pkgs; };
};
packages = {
@@ -255,6 +257,9 @@
inherit (build) komorebi komorebic komorebi-bar;
komorebi-full = build.fullBuild;
};
darwinModules.default = import ./nix/darwin-module.nix;
darwinModules.komorebi = import ./nix/darwin-module.nix;
};
};
}
+321
View File
@@ -0,0 +1,321 @@
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.komorebi;
cfgBar = config.services.komorebi-bar;
jsonFormat = pkgs.formats.json { };
komorebiSchemaPath = ../schema.json;
komorebiBarSchemaPath = ../schema.bar.json;
filterNulls =
value:
if value == null then
null
else if builtins.isList value then
map filterNulls (builtins.filter (x: x != null) value)
else if builtins.isAttrs value then
let
filtered = lib.filterAttrs (_n: v: v != null) (builtins.mapAttrs (_n: v: filterNulls v) value);
in
filtered
else
value;
# validate a json file against the flake's version of a schema, returning the validated file path
# strips comment lines before validation (common in configs for editor support)
validateJsonFile =
{
name,
jsonFile,
schemaFile,
}:
pkgs.runCommand "validated-${name}" { nativeBuildInputs = [ pkgs.check-jsonschema ]; } ''
echo "Validating ${name} against schema..."
# Strip // comment lines before validation
grep -v '^\s*//' ${jsonFile} > clean.json
check-jsonschema --schemafile ${schemaFile} clean.json
cp clean.json $out
'';
komorebiOptionsModule = import ./komorebi-options.nix { inherit lib; };
komorebiBarOptionsModule = import ./komorebi-bar-options.nix { inherit lib; };
defaultKomorebiConfig = (import ./tests/komorebi-example.nix { }).komorebi;
defaultKomorebiBarConfig = (import ./tests/komorebi-bar-example.nix { }).komorebi-bar;
# fetch application-specific configuration from remote sources
fetchAppSpecificSource =
source:
pkgs.fetchurl {
url = "https://raw.githubusercontent.com/${source.owner}/${source.repo}/${source.rev}/${source.path}";
hash = source.hash;
};
applicationSpecificConfigFiles = map fetchAppSpecificSource cfg.applicationSpecificConfiguration.sources;
# build app_specific_configuration_path value:
# - single source: use single path (string)
# - multiple sources: use array of paths
appSpecificPaths =
if builtins.length applicationSpecificConfigFiles == 1 then
"${builtins.head applicationSpecificConfigFiles}"
else
map (f: "${f}") applicationSpecificConfigFiles;
# build final komorebi config
# if applicationSpecificConfiguration is enabled and has sources: set app_specific_configuration_path
# if disabled or empty sources: remove app_specific_configuration_path entirely (handles default config with $HOME path)
shouldUseAppSpecific =
cfg.applicationSpecificConfiguration.enable && applicationSpecificConfigFiles != [ ];
baseKomorebiConfig =
if shouldUseAppSpecific then
cfg.config
else
removeAttrs cfg.config [ "app_specific_configuration_path" ];
finalKomorebiConfig = filterNulls (
baseKomorebiConfig
// lib.optionalAttrs shouldUseAppSpecific {
app_specific_configuration_path = appSpecificPaths;
}
);
generatedKomorebiConfigFile = jsonFormat.generate "komorebi.json" finalKomorebiConfig;
# final komorebi config file - either user-provided (validated) or generated from Nix options
komorebiConfigFile =
if cfg.configFile != null then
validateJsonFile {
name = "komorebi.json";
jsonFile = cfg.configFile;
schemaFile = komorebiSchemaPath;
}
else
generatedKomorebiConfigFile;
mkGeneratedBarConfigFile =
name: barCfg: jsonFormat.generate "komorebi-bar-${name}.json" (filterNulls barCfg.config);
# final bar config - either user-provided (validated) or generated from Nix options
mkBarConfigFile =
name: barCfg:
if barCfg.configFile != null then
validateJsonFile {
name = "komorebi-bar-${name}.json";
jsonFile = barCfg.configFile;
schemaFile = komorebiBarSchemaPath;
}
else
mkGeneratedBarConfigFile name barCfg;
in
{
options.services.komorebi = {
enable = lib.mkEnableOption "komorebi for Mac";
package = lib.mkPackageOption pkgs "komorebi" { };
logLevel = lib.mkOption {
type = lib.types.enum [
"error"
"warn"
"info"
"debug"
"trace"
];
default = "info";
description = "Log verbosity level";
};
configFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to a komorebi JSON configuration file.
If specified, this file will be validated against the schema and used instead of the `config` option.
'';
example = lib.literalExpression "./komorebi.json";
};
config = lib.mkOption {
type = komorebiOptionsModule.options.komorebi.type;
default = defaultKomorebiConfig;
description = ''
komorebi configuration (validated against schema). Defaults to example config.
Ignored if `configFile` is set.
'';
};
applicationSpecificConfiguration = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to use application-specific configuration sources.";
};
sources = lib.mkOption {
type = lib.types.listOf (
lib.types.submodule {
options = {
owner = lib.mkOption {
type = lib.types.str;
description = "GitHub repository owner.";
example = "LGUG2Z";
};
repo = lib.mkOption {
type = lib.types.str;
description = "GitHub repository name.";
example = "komorebi-application-specific-configuration";
};
rev = lib.mkOption {
type = lib.types.str;
description = "Git revision (commit hash, tag, or branch).";
example = "09ebdcd95780b168cecde2f2d49096a58e91365f";
};
path = lib.mkOption {
type = lib.types.str;
default = "applications.mac.json";
description = "Path to the JSON file within the repository.";
};
hash = lib.mkOption {
type = lib.types.str;
description = "SHA256 hash of the file for reproducibility.";
example = "sha256-gfYubJpVEU6vxdd/vqujQhQX8mdAEE9ImugqdIyTQKk=";
};
};
}
);
default = [
{
owner = "LGUG2Z";
repo = "komorebi-application-specific-configuration";
rev = "09ebdcd95780b168cecde2f2d49096a58e91365f";
path = "applications.mac.json";
hash = "sha256-gfYubJpVEU6vxdd/vqujQhQX8mdAEE9ImugqdIyTQKk=";
}
];
description = ''
List of remote sources for application-specific configuration files.
Each source is fetched from GitHub and combined into the final config.
'';
example = lib.literalExpression ''
[
{
owner = "LGUG2Z";
repo = "komorebi-application-specific-configuration";
rev = "09ebdcd95780b168cecde2f2d49096a58e91365f";
path = "applications.mac.json";
hash = "sha256-gfYubJpVEU6vxdd/vqujQhQX8mdAEE9ImugqdIyTQKk=";
}
{
owner = "myuser";
repo = "my-komorebi-overrides";
rev = "abc123...";
path = "overrides.json";
hash = "sha256-...";
}
]
'';
};
};
};
options.services.komorebi-bar = {
enable = lib.mkEnableOption "komorebi-bar status bar(s)";
package = lib.mkPackageOption pkgs "komorebi-bar" { };
bars = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
configFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to a komorebi-bar JSON configuration file.
If specified, this file will be validated against the schema and used instead of the `config` option.
'';
example = lib.literalExpression "./komorebi.bar.json";
};
config = lib.mkOption {
type = komorebiBarOptionsModule.options.komorebi-bar.type;
default = defaultKomorebiBarConfig;
description = ''
komorebi-bar configuration for this bar instance.
Ignored if `configFile` is set.
'';
};
};
}
);
default = { };
example = lib.literalExpression ''
{
main = {
config = {
monitor = 0;
left_widgets = [ ... ];
right_widgets = [ ... ];
};
};
secondary = {
# Use a JSON file for this bar
configFile = ./my-secondary-bar.json;
};
}
'';
description = "Named komorebi-bar instances, one per monitor";
};
};
config = lib.mkMerge [
# komorebi
(lib.mkIf cfg.enable {
environment.systemPackages = [
cfg.package
pkgs.komorebic
];
launchd.user.agents.komorebi = {
serviceConfig.ProgramArguments = [
"${cfg.package}/bin/komorebi"
"--config"
"${komorebiConfigFile}"
"--log-level"
cfg.logLevel
];
serviceConfig.KeepAlive = true;
serviceConfig.RunAtLoad = true;
serviceConfig.EnvironmentVariables = {
PATH = "${cfg.package}/bin:${config.environment.systemPath}";
};
};
})
# komorebi-bar
(lib.mkIf cfgBar.enable {
environment.systemPackages = [ cfgBar.package ];
launchd.user.agents = lib.mapAttrs' (
name: barCfg:
lib.nameValuePair "komorebi-bar-${name}" {
serviceConfig.ProgramArguments = [
"${cfgBar.package}/bin/komorebi-bar"
"--config"
"${mkBarConfigFile name barCfg}"
];
serviceConfig.KeepAlive = true;
serviceConfig.RunAtLoad = true;
}
) cfgBar.bars;
})
];
}
+298
View File
@@ -0,0 +1,298 @@
{
pkgs ? import <nixpkgs> { },
}:
let
lib = pkgs.lib;
evalDarwinModule =
testConfig:
lib.evalModules {
modules = [
# darwin-specific opt stubs
(
{ ... }:
{
options = {
environment.systemPackages = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
};
environment.systemPath = lib.mkOption {
type = lib.types.str;
default = "/usr/bin:/bin";
};
launchd.user.agents = lib.mkOption {
type = lib.types.attrsOf lib.types.attrs;
default = { };
};
};
}
)
../darwin-module.nix
# test config
(
{ ... }:
{
_module.args.pkgs = pkgs;
}
// testConfig
)
];
specialArgs = {
inherit pkgs;
};
};
# test default configs
testDefault = evalDarwinModule {
services.komorebi.enable = true;
services.komorebi-bar = {
enable = true;
bars.main = { };
};
};
# test multiple bars
testMultiMonitor = evalDarwinModule {
services.komorebi.enable = true;
services.komorebi-bar = {
enable = true;
bars = {
primary.config.monitor = 0;
secondary.config.monitor = 1;
tertiary.config.monitor = 2;
};
};
};
# test custom komorebi conf
testCustomConfig = evalDarwinModule {
services.komorebi = {
enable = true;
logLevel = "debug";
config = {
border = true;
border_width = 8;
default_workspace_padding = 20;
default_container_padding = 20;
monitors = [
{
workspaces = [
{
name = "main";
layout = "BSP";
}
{
name = "code";
layout = "Columns";
}
];
}
];
};
};
};
# test disabled
testDisabled = evalDarwinModule {
services.komorebi.enable = false;
services.komorebi-bar.enable = false;
};
# test applicationSpecificConfiguration enabled (default)
testAppSpecificEnabled = evalDarwinModule {
services.komorebi.enable = true;
};
# test applicationSpecificConfiguration disabled
testAppSpecificDisabled = evalDarwinModule {
services.komorebi = {
enable = true;
applicationSpecificConfiguration.enable = false;
};
};
# test applicationSpecificConfiguration with multiple sources
testAppSpecificMultipleSources = evalDarwinModule {
services.komorebi = {
enable = true;
applicationSpecificConfiguration.sources = [
{
owner = "LGUG2Z";
repo = "komorebi-application-specific-configuration";
rev = "09ebdcd95780b168cecde2f2d49096a58e91365f";
path = "applications.mac.json";
hash = "sha256-gfYubJpVEU6vxdd/vqujQhQX8mdAEE9ImugqdIyTQKk=";
}
{
owner = "LGUG2Z";
repo = "komorebi-application-specific-configuration";
rev = "09ebdcd95780b168cecde2f2d49096a58e91365f";
path = "applications.mac.json";
hash = "sha256-gfYubJpVEU6vxdd/vqujQhQX8mdAEE9ImugqdIyTQKk=";
}
];
};
};
# test applicationSpecificConfiguration with custom single source
testAppSpecificCustomSource = evalDarwinModule {
services.komorebi = {
enable = true;
applicationSpecificConfiguration.sources = [
{
owner = "someuser";
repo = "custom-config";
rev = "abc123";
path = "my-apps.json";
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}
];
};
};
# test applicationSpecificConfiguration with empty sources
testAppSpecificEmptySources = evalDarwinModule {
services.komorebi = {
enable = true;
applicationSpecificConfiguration.sources = [ ];
};
};
# test configFile option for komorebi
testKomorebiConfigFile = evalDarwinModule {
services.komorebi = {
enable = true;
configFile = ../../docs/komorebi.example.json;
};
};
# test configFile option for komorebi-bar
testBarConfigFile = evalDarwinModule {
services.komorebi-bar = {
enable = true;
bars.main.configFile = ../../docs/komorebi.bar.example.json;
};
};
assertions = [
{
name = "default-komorebi-enabled";
assertion = testDefault.config.services.komorebi.enable == true;
}
{
name = "default-komorebi-bar-enabled";
assertion = testDefault.config.services.komorebi-bar.enable == true;
}
{
name = "default-has-komorebi-agent";
assertion = builtins.hasAttr "komorebi" testDefault.config.launchd.user.agents;
}
{
name = "default-has-bar-agent";
assertion = builtins.hasAttr "komorebi-bar-main" testDefault.config.launchd.user.agents;
}
{
name = "multi-monitor-has-three-bars";
assertion =
let
agents = testMultiMonitor.config.launchd.user.agents;
in
builtins.hasAttr "komorebi-bar-primary" agents
&& builtins.hasAttr "komorebi-bar-secondary" agents
&& builtins.hasAttr "komorebi-bar-tertiary" agents;
}
{
name = "custom-config-log-level";
assertion = testCustomConfig.config.services.komorebi.logLevel == "debug";
}
{
name = "custom-config-border-width";
assertion = testCustomConfig.config.services.komorebi.config.border_width == 8;
}
{
name = "disabled-no-komorebi-agent";
assertion = !(builtins.hasAttr "komorebi" testDisabled.config.launchd.user.agents);
}
{
name = "disabled-no-bar-agents";
assertion = testDisabled.config.launchd.user.agents == { };
}
{
name = "app-specific-enabled-by-default";
assertion =
testAppSpecificEnabled.config.services.komorebi.applicationSpecificConfiguration.enable == true;
}
{
name = "app-specific-has-default-source";
assertion =
let
sources = testAppSpecificEnabled.config.services.komorebi.applicationSpecificConfiguration.sources;
in
builtins.length sources == 1 && (builtins.head sources).owner == "LGUG2Z";
}
{
name = "app-specific-disabled-works";
assertion =
testAppSpecificDisabled.config.services.komorebi.applicationSpecificConfiguration.enable == false;
}
{
name = "app-specific-multiple-sources-count";
assertion =
builtins.length testAppSpecificMultipleSources.config.services.komorebi.applicationSpecificConfiguration.sources
== 2;
}
{
name = "app-specific-custom-source-owner";
assertion =
let
sources =
testAppSpecificCustomSource.config.services.komorebi.applicationSpecificConfiguration.sources;
in
(builtins.head sources).owner == "someuser";
}
{
name = "app-specific-empty-sources";
assertion =
testAppSpecificEmptySources.config.services.komorebi.applicationSpecificConfiguration.sources
== [ ];
}
{
name = "config-file-komorebi-sets-option";
assertion = testKomorebiConfigFile.config.services.komorebi.configFile != null;
}
{
name = "config-file-bar-sets-option";
assertion = testBarConfigFile.config.services.komorebi-bar.bars.main.configFile != null;
}
];
failedAssertions = builtins.filter (a: !a.assertion) assertions;
komorebiValidatedConfigPath = builtins.elemAt testKomorebiConfigFile.config.launchd.user.agents.komorebi.serviceConfig.ProgramArguments 2;
barValidatedConfigPath = builtins.elemAt testBarConfigFile.config.launchd.user.agents.komorebi-bar-main.serviceConfig.ProgramArguments 2;
in
pkgs.runCommand "darwin-module-validation" { } ''
${
if failedAssertions == [ ] then
''
echo "All ${toString (builtins.length assertions)} darwin module assertions passed!"
echo "Validating komorebi configFile against schema..."
cat ${komorebiValidatedConfigPath} > /dev/null
echo "Validating komorebi-bar configFile against schema..."
cat ${barValidatedConfigPath} > /dev/null
echo "All configFile validations passed!"
touch $out
''
else
''
echo "Failed assertions:"
${lib.concatMapStringsSep "\n" (a: "echo ' - ${a.name}'") failedAssertions}
exit 1
''
}
''
+4 -5
View File
@@ -1,14 +1,14 @@
# E2E test harness for validating generated Nix options
# Run with: nix build -f nix/tests/default.nix
# Or via flake: nix flake check (runs as part of checks)
# run with: nix build -f nix/tests/default.nix
# or via flake: nix flake check (runs as part of checks)
{
pkgs ? import <nixpkgs> { },
}:
let
lib = pkgs.lib;
# Recursively filter out null values and empty attrs from a config
# This is needed because the Nix module system uses null for unset optional fields,
# recursively filter out null values and empty attrs from a config
# this is needed because the Nix module system uses null for unset optional fields,
# but the JSON schema expects those fields to be absent entirely
filterNulls =
value:
@@ -24,7 +24,6 @@ let
else
value;
# Helper to evaluate a config module and extract the config value
evalConfig =
optionsModule: configModule: configPath:
let