diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 91bc541c..2dadfdf5 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -18,6 +18,9 @@ Makefile @thiagokokada /modules/misc/dconf.nix @rycee +/modules/misc/editorconfig.nix @loicreynier +/test/modules/misc/editorconfig @loicreynier + /modules/misc/fontconfig.nix @rycee /tests/modules/misc/fontconfig @rycee diff --git a/modules/misc/editorconfig.nix b/modules/misc/editorconfig.nix new file mode 100644 index 00000000..579ef1f8 --- /dev/null +++ b/modules/misc/editorconfig.nix @@ -0,0 +1,53 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.editorconfig; + + iniFormat = pkgs.formats.ini { }; + +in { + meta.maintainers = with maintainers; [ loicreynier ]; + + options.editorconfig = { + enable = mkEnableOption "EditorConfig home configuration file"; + + settings = mkOption { + type = iniFormat.type; + default = { }; + description = '' + Configuration written to $HOME/.editorconfig. + root = true is automatically added to the file, + it must not be added here. + See for documentation. + ''; + example = literalExpression '' + { + "*" = { + charset = "utf-8"; + end_of_line = "lf"; + trim_trailing_whitespace = true; + insert_final_newline = true; + max_line_width = 78; + indent_style = "space"; + indent_size = 4; + }; + }; + ''; + }; + }; + + config = mkIf (cfg.enable && cfg.settings != { }) { + home.file.".editorconfig".text = let + renderedSettings = generators.toINIWithGlobalSection { } { + globalSection = { root = true; }; + sections = cfg.settings; + }; + in '' + # Generated by Home Manager + ${renderedSettings} + ''; + }; +} diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 247d4bff..63855183 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -668,6 +668,13 @@ in A new module is available: 'programs.btop'. ''; } + + { + time = "2022-09-05T11:05:25+00:00"; + message = '' + A new module is available: 'editorconfig'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index de977b22..60d979ea 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -24,6 +24,7 @@ let ./manual.nix ./misc/dconf.nix ./misc/debug.nix + ./misc/editorconfig.nix ./misc/fontconfig.nix ./misc/gtk.nix ./misc/lib.nix diff --git a/tests/default.nix b/tests/default.nix index 4db813b3..2a78e110 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -127,6 +127,7 @@ import nmt { ./modules/config/i18n ./modules/i18n/input-method ./modules/misc/debug + ./modules/misc/editorconfig ./modules/misc/gtk ./modules/misc/numlock ./modules/misc/pam diff --git a/tests/modules/misc/editorconfig/default.nix b/tests/modules/misc/editorconfig/default.nix new file mode 100644 index 00000000..9e81bc16 --- /dev/null +++ b/tests/modules/misc/editorconfig/default.nix @@ -0,0 +1 @@ +{ editorconfig-simple-config = ./editorconfig-simple-config.nix; } diff --git a/tests/modules/misc/editorconfig/editorconfig-simple-config-expected b/tests/modules/misc/editorconfig/editorconfig-simple-config-expected new file mode 100644 index 00000000..321c5e7e --- /dev/null +++ b/tests/modules/misc/editorconfig/editorconfig-simple-config-expected @@ -0,0 +1,15 @@ +# Generated by Home Manager +root=true + +[*] +charset=utf-8 +end_of_line=lf +indent_style=space +insert_final_newline=true +max_line_width=78 +trim_trailing_whitespace=true + +[*.md] +indent_size=unset +trim_trailing_whitespace=false + diff --git a/tests/modules/misc/editorconfig/editorconfig-simple-config.nix b/tests/modules/misc/editorconfig/editorconfig-simple-config.nix new file mode 100644 index 00000000..ca0581e5 --- /dev/null +++ b/tests/modules/misc/editorconfig/editorconfig-simple-config.nix @@ -0,0 +1,28 @@ +{ ... }: + +{ + editorconfig = { + enable = true; + settings = { + "*" = { + charset = "utf-8"; + end_of_line = "lf"; + trim_trailing_whitespace = true; + insert_final_newline = true; + max_line_width = 78; + indent_style = "space"; + }; + "*.md" = { + indent_size = "unset"; + trim_trailing_whitespace = false; + }; + }; + }; + + nmt.script = '' + assertFileExists home-files/.editorconfig + assertFileContent home-files/.editorconfig ${ + ./editorconfig-simple-config-expected + } + ''; +}