From ba6b75011b44e85b1b755b6c423f85d0817645f7 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Thu, 28 Dec 2023 12:07:30 +0100 Subject: [PATCH] alacritty: make compatible with alacritty 0.13 The config file is in TOML from 0.13 onwards. --- modules/programs/alacritty.nix | 52 ++++++++++++------- tests/modules/programs/alacritty/default.nix | 1 + .../alacritty/example-settings-expected.toml | 8 +++ .../alacritty/example-settings-expected.yml | 1 - .../programs/alacritty/example-settings.nix | 8 +-- .../programs/alacritty/settings-merging.nix | 2 +- .../alacritty/settings-toml-expected.toml | 15 ++++++ .../programs/alacritty/toml_config.nix | 34 ++++++++++++ 8 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 tests/modules/programs/alacritty/example-settings-expected.toml delete mode 100644 tests/modules/programs/alacritty/example-settings-expected.yml create mode 100644 tests/modules/programs/alacritty/settings-toml-expected.toml create mode 100644 tests/modules/programs/alacritty/toml_config.nix diff --git a/modules/programs/alacritty.nix b/modules/programs/alacritty.nix index 56beb6d4..2f7845f4 100644 --- a/modules/programs/alacritty.nix +++ b/modules/programs/alacritty.nix @@ -4,7 +4,9 @@ with lib; let cfg = config.programs.alacritty; - yamlFormat = pkgs.formats.yaml { }; + useToml = lib.versionAtLeast cfg.package.version "0.13"; + tomlFormat = pkgs.formats.toml { }; + configFileName = "alacritty.${if useToml then "toml" else "yml"}"; in { options = { programs.alacritty = { @@ -18,7 +20,7 @@ in { }; settings = mkOption { - type = yamlFormat.type; + type = tomlFormat.type; default = { }; example = literalExpression '' { @@ -26,7 +28,7 @@ in { lines = 3; columns = 200; }; - key_bindings = [ + keyboard.bindings = [ { key = "K"; mods = "Control"; @@ -37,27 +39,37 @@ in { ''; description = '' Configuration written to - {file}`$XDG_CONFIG_HOME/alacritty/alacritty.yml`. See - - for the default configuration. + {file}`$XDG_CONFIG_HOME/alacritty/alacritty.yml` or + {file}`$XDG_CONFIG_HOME/alacritty/alacritty.toml` + (the latter being used for alacritty 0.13 and later). + See + for more info. ''; }; }; }; - config = mkMerge [ - (mkIf cfg.enable { - home.packages = [ cfg.package ]; + config = mkIf cfg.enable { + home.packages = [ cfg.package ]; - xdg.configFile."alacritty/alacritty.yml" = mkIf (cfg.settings != { }) { - # TODO: Replace by the generate function but need to figure out how to - # handle the escaping first. - # - # source = yamlFormat.generate "alacritty.yml" cfg.settings; - - text = - replaceStrings [ "\\\\" ] [ "\\" ] (builtins.toJSON cfg.settings); - }; - }) - ]; + xdg.configFile."alacritty/${configFileName}" = + lib.mkIf (cfg.settings != { }) (lib.mkMerge [ + (lib.mkIf useToml { + source = + (tomlFormat.generate configFileName cfg.settings).overrideAttrs + (finalAttrs: prevAttrs: { + buildCommand = lib.concatStringsSep "\n" [ + prevAttrs.buildCommand + # TODO: why is this needed? Is there a better way to retain escape sequences? + "substituteInPlace $out --replace '\\\\' '\\'" + ]; + }); + }) + # TODO remove this once we don't need to support Alacritty < 0.12 anymore + (lib.mkIf (!useToml) { + text = + replaceStrings [ "\\\\" ] [ "\\" ] (builtins.toJSON cfg.settings); + }) + ]); + }; } diff --git a/tests/modules/programs/alacritty/default.nix b/tests/modules/programs/alacritty/default.nix index 3ccd9a91..e5445439 100644 --- a/tests/modules/programs/alacritty/default.nix +++ b/tests/modules/programs/alacritty/default.nix @@ -2,4 +2,5 @@ alacritty-example-settings = ./example-settings.nix; alacritty-empty-settings = ./empty-settings.nix; alacritty-merging-settings = ./settings-merging.nix; + alacritty-toml-config = ./toml_config.nix; } diff --git a/tests/modules/programs/alacritty/example-settings-expected.toml b/tests/modules/programs/alacritty/example-settings-expected.toml new file mode 100644 index 00000000..723f1874 --- /dev/null +++ b/tests/modules/programs/alacritty/example-settings-expected.toml @@ -0,0 +1,8 @@ +[[keyboard.bindings]] +chars = "\x0c" +key = "K" +mods = "Control" + +[window.dimensions] +columns = 200 +lines = 3 diff --git a/tests/modules/programs/alacritty/example-settings-expected.yml b/tests/modules/programs/alacritty/example-settings-expected.yml deleted file mode 100644 index 06162419..00000000 --- a/tests/modules/programs/alacritty/example-settings-expected.yml +++ /dev/null @@ -1 +0,0 @@ -{"key_bindings":[{"chars":"\x0c","key":"K","mods":"Control"}],"window":{"dimensions":{"columns":200,"lines":3}}} \ No newline at end of file diff --git a/tests/modules/programs/alacritty/example-settings.nix b/tests/modules/programs/alacritty/example-settings.nix index 61b8b4b4..8aa01380 100644 --- a/tests/modules/programs/alacritty/example-settings.nix +++ b/tests/modules/programs/alacritty/example-settings.nix @@ -6,7 +6,7 @@ with lib; config = { programs.alacritty = { enable = true; - package = config.lib.test.mkStubPackage { }; + package = config.lib.test.mkStubPackage { version = "0.13.0"; }; settings = { window.dimensions = { @@ -14,7 +14,7 @@ with lib; columns = 200; }; - key_bindings = [{ + keyboard.bindings = [{ key = "K"; mods = "Control"; chars = "\\x0c"; @@ -24,8 +24,8 @@ with lib; nmt.script = '' assertFileContent \ - home-files/.config/alacritty/alacritty.yml \ - ${./example-settings-expected.yml} + home-files/.config/alacritty/alacritty.toml \ + ${./example-settings-expected.toml} ''; }; } diff --git a/tests/modules/programs/alacritty/settings-merging.nix b/tests/modules/programs/alacritty/settings-merging.nix index c7962fb5..76aedf4f 100644 --- a/tests/modules/programs/alacritty/settings-merging.nix +++ b/tests/modules/programs/alacritty/settings-merging.nix @@ -6,7 +6,7 @@ with lib; config = { programs.alacritty = { enable = true; - package = config.lib.test.mkStubPackage { }; + package = config.lib.test.mkStubPackage { version = "0.12.3"; }; settings = { window.dimensions = { diff --git a/tests/modules/programs/alacritty/settings-toml-expected.toml b/tests/modules/programs/alacritty/settings-toml-expected.toml new file mode 100644 index 00000000..9b0867ee --- /dev/null +++ b/tests/modules/programs/alacritty/settings-toml-expected.toml @@ -0,0 +1,15 @@ +[font] +[font.bold] +family = "SFMono" + +[font.normal] +family = "SFMono" + +[[keyboard.bindings]] +chars = "\x0c" +key = "K" +mods = "Control" + +[window.dimensions] +columns = 200 +lines = 3 diff --git a/tests/modules/programs/alacritty/toml_config.nix b/tests/modules/programs/alacritty/toml_config.nix new file mode 100644 index 00000000..4f15e70b --- /dev/null +++ b/tests/modules/programs/alacritty/toml_config.nix @@ -0,0 +1,34 @@ +{ config, ... }: + +{ + config = { + programs.alacritty = { + enable = true; + package = config.lib.test.mkStubPackage { version = "0.13.0"; }; + + settings = { + window.dimensions = { + lines = 3; + columns = 200; + }; + + keyboard.bindings = [{ + key = "K"; + mods = "Control"; + chars = "\\x0c"; + }]; + + font = { + normal.family = "SFMono"; + bold.family = "SFMono"; + }; + }; + }; + + nmt.script = '' + assertFileContent \ + home-files/.config/alacritty/alacritty.toml \ + ${./settings-toml-expected.toml} + ''; + }; +}