programs.wezterm: add settings option

Add a `settings` option that allows WezTerm to be configured
declaratively using a Nix attribute set. Settings are serialized to
Lua using `lib.generators.toLua`. Raw Lua expressions such as
`wezterm.font` and `wezterm.action.*` can be embedded using
`lib.generators.mkLuaInline`.

When `settings` is set, `extraConfig` is still supported. It is
wrapped in an immediately invoked function expression so that any
`return` statement (including the common `return {}` and
`return config` patterns) is captured and merged on top of the
generated settings, preserving full backward compatibility.

The merge between `settings` and `extraConfig` is shallow: a key
returned from `extraConfig` replaces the entire corresponding value
from `settings`, including nested tables and arrays.

Closes #6047
This commit is contained in:
philip-730
2026-04-07 12:39:40 -04:00
committed by Austin Horstman
parent d65fb79dcc
commit 92d382b982
6 changed files with 272 additions and 16 deletions

View File

@@ -7,6 +7,14 @@ section is therefore not final.
This release has the following notable changes:
- WezTerm now supports declarative configuration via
[](#opt-programs.wezterm.settings). Settings are expressed as a Nix
attribute set and serialized to Lua using `lib.generators.toLua`.
Raw Lua expressions such as `wezterm.font` and `wezterm.action.*`
can be embedded using `lib.generators.mkLuaInline`. The existing
[](#opt-programs.wezterm.extraConfig) option remains fully supported
and can be combined with [](#opt-programs.wezterm.settings).
- The [](#opt-programs.anki.uiScale) option now expects a value in the
range 1.02.0, previously it erroneously expected values in the
range `0.01.0`.

View File

@@ -34,6 +34,40 @@ in
package = lib.mkPackageOption pkgs "wezterm" { };
settings = mkOption {
type = types.attrsOf types.anything;
default = { };
example = literalExpression ''
{
color_scheme = "Catppuccin Mocha";
font_size = 13;
hide_tab_bar_if_only_one_tab = true;
window_padding = {
left = 0;
right = 0;
top = 0;
bottom = 0;
};
font = lib.generators.mkLuaInline '''wezterm.font("JetBrains Mono")''';
}
'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/wezterm/wezterm.lua` using
`lib.generators.toLua`. Values created with
`lib.generators.mkLuaInline` are rendered as raw Lua
expressions, which allows embedding wezterm API calls such as
`wezterm.font`. See
<https://wezfurlong.org/wezterm/config/files.html> for
available options.
When this option is set, {option}`extraConfig` is still
supported. Any table returned from the extra Lua code is merged
on top of these settings, so {option}`extraConfig` takes
precedence on conflicts.
'';
};
extraConfig = mkOption {
type = types.lines;
default = "";
@@ -57,6 +91,15 @@ in
{file}`$XDG_CONFIG_HOME/wezterm/wezterm.lua`. See
<https://wezfurlong.org/wezterm/config/files.html>
how to configure.
When used together with {option}`settings`, any table returned
from this Lua code is merged into the generated configuration,
taking precedence over {option}`settings` on conflicts. Note
that this merge is shallow: a key returned here replaces the
entire corresponding value from {option}`settings`, so nested
tables and arrays are not combined. A {command}`return`
statement is optional code that directly modifies the
{command}`config` variable also works.
'';
};
@@ -99,24 +142,55 @@ in
config = mkIf cfg.enable {
home.packages = [ cfg.package ];
xdg.configFile = mkMerge [
(mkIf (cfg.extraConfig != "") {
"wezterm/wezterm.lua".text = ''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
xdg.configFile =
let
hasSettings = cfg.settings != { };
in
mkMerge [
(mkIf (hasSettings || cfg.extraConfig != "") {
"wezterm/wezterm.lua".text =
if hasSettings then
''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
local wezterm = require 'wezterm'
local wezterm = require 'wezterm'
local config = wezterm.config_builder and wezterm.config_builder() or {}
${cfg.extraConfig}
'';
})
(lib.mapAttrs' (
name: value:
lib.nameValuePair "wezterm/colors/${name}.toml" {
source = tomlFormat.generate "${name}.toml" { colors = value; };
}
) cfg.colorSchemes)
];
local hm_config = ${lib.generators.toLua { } cfg.settings}
for k, v in pairs(hm_config) do
config[k] = v
end
${lib.optionalString (cfg.extraConfig != "") ''
local _hm_extra = (function()
${cfg.extraConfig}
end)()
if type(_hm_extra) == "table" then
for k, v in pairs(_hm_extra) do
config[k] = v
end
end
''}
return config
''
else
''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
local wezterm = require 'wezterm'
${cfg.extraConfig}
'';
})
(lib.mapAttrs' (
name: value:
lib.nameValuePair "wezterm/colors/${name}.toml" {
source = tomlFormat.generate "${name}.toml" { colors = value; };
}
) cfg.colorSchemes)
];
programs.bash.initExtra = mkIf cfg.enableBashIntegration shellIntegrationStr;
programs.zsh.initContent = mkIf cfg.enableZshIntegration shellIntegrationStr;

View File

@@ -1,6 +1,9 @@
{
wezterm-example-setting = ./example-setting.nix;
wezterm-empty-setting = ./empty-setting.nix;
wezterm-settings = ./settings.nix;
wezterm-settings-with-extra-config = ./settings-with-extra-config.nix;
wezterm-settings-extra-config-direct = ./settings-extra-config-direct.nix;
wezterm-bash-integration-default = ./bash-integration-default.nix;
wezterm-bash-integration-disabled = ./bash-integration-disabled.nix;

View File

@@ -0,0 +1,47 @@
{ lib, ... }:
{
programs.wezterm = {
enable = true;
settings = {
font_size = 13;
};
extraConfig = ''
config.color_scheme = "Tomorrow Night"
'';
};
nmt.script =
let
expectedConfig = builtins.toFile "wezterm.lua" ''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
local wezterm = require 'wezterm'
local config = wezterm.config_builder and wezterm.config_builder() or {}
local hm_config = {
["font_size"] = 13
}
for k, v in pairs(hm_config) do
config[k] = v
end
local _hm_extra = (function()
config.color_scheme = "Tomorrow Night"
end)()
if type(_hm_extra) == "table" then
for k, v in pairs(_hm_extra) do
config[k] = v
end
end
return config
'';
in
''
assertFileExists home-files/.config/wezterm/wezterm.lua
assertFileContent home-files/.config/wezterm/wezterm.lua ${expectedConfig}
'';
}

View File

@@ -0,0 +1,51 @@
{ lib, ... }:
{
programs.wezterm = {
enable = true;
settings = {
font_size = 13;
};
extraConfig = ''
return {
color_scheme = "Tomorrow Night",
}
'';
};
nmt.script =
let
expectedConfig = builtins.toFile "wezterm.lua" ''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
local wezterm = require 'wezterm'
local config = wezterm.config_builder and wezterm.config_builder() or {}
local hm_config = {
["font_size"] = 13
}
for k, v in pairs(hm_config) do
config[k] = v
end
local _hm_extra = (function()
return {
color_scheme = "Tomorrow Night",
}
end)()
if type(_hm_extra) == "table" then
for k, v in pairs(_hm_extra) do
config[k] = v
end
end
return config
'';
in
''
assertFileExists home-files/.config/wezterm/wezterm.lua
assertFileContent home-files/.config/wezterm/wezterm.lua ${expectedConfig}
'';
}

View File

@@ -0,0 +1,73 @@
{ lib, ... }:
{
programs.wezterm = {
enable = true;
settings = {
color_scheme = "Catppuccin Mocha";
font_size = 13;
hide_tab_bar_if_only_one_tab = true;
window_padding = {
left = 0;
right = 0;
top = 0;
bottom = 0;
};
keys = [
{
key = "F11";
action = lib.generators.mkLuaInline "wezterm.action.ToggleFullScreen";
}
{
key = "+";
mods = "CTRL";
action = lib.generators.mkLuaInline "wezterm.action.IncreaseFontSize";
}
];
};
};
nmt.script =
let
expectedConfig = builtins.toFile "wezterm.lua" ''
-- Generated by Home Manager.
-- See https://wezfurlong.org/wezterm/
local wezterm = require 'wezterm'
local config = wezterm.config_builder and wezterm.config_builder() or {}
local hm_config = {
["color_scheme"] = "Catppuccin Mocha",
["font_size"] = 13,
["hide_tab_bar_if_only_one_tab"] = true,
["keys"] = {
{
["action"] = (wezterm.action.ToggleFullScreen),
["key"] = "F11"
},
{
["action"] = (wezterm.action.IncreaseFontSize),
["key"] = "+",
["mods"] = "CTRL"
}
},
["window_padding"] = {
["bottom"] = 0,
["left"] = 0,
["right"] = 0,
["top"] = 0
}
}
for k, v in pairs(hm_config) do
config[k] = v
end
return config
'';
in
''
assertFileExists home-files/.config/wezterm/wezterm.lua
assertFileContent home-files/.config/wezterm/wezterm.lua ${expectedConfig}
'';
}