From 507cd7a86244bc3b08a92c76fe29eea5d71be89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Pobo=C5=99il?= Date: Sat, 26 Aug 2023 15:36:08 +0200 Subject: [PATCH] ranger: add module --- modules/lib/maintainers.nix | 6 + modules/misc/news.nix | 7 + modules/modules.nix | 1 + modules/programs/ranger.nix | 182 ++++++++++++++++++ tests/default.nix | 1 + .../programs/ranger/configuration-rc.conf | 13 ++ .../programs/ranger/configuration-rifle.conf | 2 + .../modules/programs/ranger/configuration.nix | 43 +++++ tests/modules/programs/ranger/default.nix | 1 + 9 files changed, 256 insertions(+) create mode 100644 modules/programs/ranger.nix create mode 100644 tests/modules/programs/ranger/configuration-rc.conf create mode 100644 tests/modules/programs/ranger/configuration-rifle.conf create mode 100644 tests/modules/programs/ranger/configuration.nix create mode 100644 tests/modules/programs/ranger/default.nix diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix index 8e6bd4044..9265f04ff 100644 --- a/modules/lib/maintainers.nix +++ b/modules/lib/maintainers.nix @@ -113,6 +113,12 @@ github = "foo-dogsquared"; githubId = 34962634; }; + fpob = { + name = "Filip PoboĊ™il"; + email = "fpob@proton.me"; + github = "fpob"; + githubId = 6289078; + }; olmokramer = { name = "Olmo Kramer"; email = "olmokramer@users.noreply.github.com"; diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 8d681658e..5a40f60c2 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1429,6 +1429,13 @@ in { A new module is available: 'programs.zk' ''; } + + { + time = "2024-03-08T22:23:24+00:00"; + message = '' + A new module is available: 'programs.ranger'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 16206cc17..ec2af169f 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -189,6 +189,7 @@ let ./programs/pylint.nix ./programs/qcal.nix ./programs/qutebrowser.nix + ./programs/ranger.nix ./programs/rbw.nix ./programs/readline.nix ./programs/rio.nix diff --git a/modules/programs/ranger.nix b/modules/programs/ranger.nix new file mode 100644 index 000000000..63d33fad3 --- /dev/null +++ b/modules/programs/ranger.nix @@ -0,0 +1,182 @@ +{ config, pkgs, lib, ... }: + +with lib; + +let cfg = config.programs.ranger; + +in { + options.programs.ranger = { + enable = mkEnableOption "ranger file manager"; + + package = mkOption { + type = types.package; + default = pkgs.ranger; + defaultText = literalExpression "pkgs.ranger"; + description = "The ranger package to use."; + }; + + extraPackages = mkOption { + type = types.listOf types.package; + default = [ ]; + description = "Extra packages added to ranger."; + }; + + finalPackage = mkOption { + type = types.package; + readOnly = true; + visible = false; + description = "Resulting ranger package."; + }; + + settings = mkOption { + type = types.attrsOf + (types.oneOf [ types.bool types.float types.int types.str ]); + default = { }; + description = '' + Settings written to {file}`$XDG_CONFIG_HOME/ranger/rc.conf`. + ''; + example = { + column_ratios = "1,3,3"; + confirm_on_delete = "never"; + unicode_ellipsis = true; + scroll_offset = 8; + }; + }; + + aliases = mkOption { + type = types.attrsOf types.str; + default = { }; + description = '' + Aliases written to {file}`$XDG_CONFIG_HOME/ranger/rc.conf`. + ''; + example = { + e = "edit"; + setl = "setlocal"; + filter = "scout -prts"; + }; + }; + + mappings = mkOption { + type = types.attrsOf types.str; + default = { }; + description = '' + Mappings written to {file}`$XDG_CONFIG_HOME/ranger/rc.conf`. + ''; + example = { + Q = "quitall"; + q = "quit"; + }; + }; + + extraConfig = mkOption { + type = types.lines; + default = ""; + description = '' + Extra configuration lines to add to + {file}`$XDG_CONFIG_HOME/ranger/rc.conf`. + ''; + }; + + plugins = mkOption { + type = types.listOf (types.submodule { + options = { + name = mkOption { + type = types.str; + description = '' + Name of the plugin linked to + {file}`$XDG_CONFIG_HOME/ranger/plugins/`. In the case of a + single-file plugin, it must also have `.py` suffix. + ''; + }; + src = mkOption { + type = types.path; + description = '' + The plugin file or directory. + ''; + }; + }; + }); + default = [ ]; + description = '' + List of files to be added to {file}`$XDG_CONFIG_HOME/ranger/plugins/`. + ''; + example = literalExpression '' + [ + { + name = "zoxide"; + src = builtins.fetchGit { + url = "https://github.com/jchook/ranger-zoxide.git"; + rev = "363df97af34c96ea873c5b13b035413f56b12ead"; + }; + } + ] + ''; + }; + + rifle = mkOption { + type = types.listOf (types.submodule { + options = { + condition = mkOption { + type = types.str; + description = '' + A condition to match a file. + ''; + example = "mime ^text, label editor"; + }; + command = mkOption { + type = types.str; + description = '' + A command to run for the matching file. + ''; + example = literalExpression ''"${pkgs.vim}/bin/vim -- \"$@\""''; + }; + }; + }); + default = [ ]; + description = '' + Settings written to {file}`$XDG_CONFIG_HOME/ranger/rifle.conf`. + ''; + }; + }; + + config = mkIf cfg.enable (mkMerge [ + { + programs.ranger.finalPackage = cfg.package.overrideAttrs (oldAttrs: { + propagatedBuildInputs = oldAttrs.propagatedBuildInputs + ++ cfg.extraPackages; + }); + + home.packages = [ cfg.finalPackage ]; + + xdg.configFile."ranger/rc.conf".text = let + mkString = generators.mkValueStringDefault { }; + mkConfig = cmd: + generators.toKeyValue { + mkKeyValue = k: v: "${cmd} ${k} ${mkString v}"; + }; + in '' + ${mkConfig "set" cfg.settings} + ${mkConfig "alias" cfg.aliases} + ${mkConfig "map" cfg.mappings} + ${cfg.extraConfig} + ''; + } + + (mkIf (cfg.plugins != [ ]) { + xdg.configFile = let + toAttrs = i: { + name = "ranger/plugins/${i.name}"; + value.source = i.src; + }; + in listToAttrs (map toAttrs cfg.plugins); + }) + + (mkIf (cfg.rifle != [ ]) { + xdg.configFile."ranger/rifle.conf".text = + let lines = map (i: "${i.condition} = ${i.command}") cfg.rifle; + in concatLines lines; + }) + ]); + + meta.maintainers = [ hm.maintainers.fpob ]; +} diff --git a/tests/default.nix b/tests/default.nix index 1e14df0c4..5084d2d10 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -126,6 +126,7 @@ in import nmtSrc { ./modules/programs/pyenv ./modules/programs/qcal ./modules/programs/qutebrowser + ./modules/programs/ranger ./modules/programs/readline ./modules/programs/rio ./modules/programs/ripgrep diff --git a/tests/modules/programs/ranger/configuration-rc.conf b/tests/modules/programs/ranger/configuration-rc.conf new file mode 100644 index 000000000..ee18f4c61 --- /dev/null +++ b/tests/modules/programs/ranger/configuration-rc.conf @@ -0,0 +1,13 @@ +set column_ratios 1,3,3 +set confirm_on_delete never +set scroll_offset 8 +set unicode_ellipsis true + +alias e edit +alias filter scout -prts +alias setl setlocal + +map Q quitall +map q quit + +unmap gd diff --git a/tests/modules/programs/ranger/configuration-rifle.conf b/tests/modules/programs/ranger/configuration-rifle.conf new file mode 100644 index 000000000..7a8671a1b --- /dev/null +++ b/tests/modules/programs/ranger/configuration-rifle.conf @@ -0,0 +1,2 @@ +mime ^text, label editor = vim -- "$@" +mime ^text, label pager = less -- "$@" diff --git a/tests/modules/programs/ranger/configuration.nix b/tests/modules/programs/ranger/configuration.nix new file mode 100644 index 000000000..5d534e49c --- /dev/null +++ b/tests/modules/programs/ranger/configuration.nix @@ -0,0 +1,43 @@ +{ ... }: + +{ + programs.ranger = { + enable = true; + settings = { + column_ratios = "1,3,3"; + confirm_on_delete = "never"; + unicode_ellipsis = true; + scroll_offset = 8; + }; + aliases = { + e = "edit"; + setl = "setlocal"; + filter = "scout -prts"; + }; + mappings = { + Q = "quitall"; + q = "quit"; + }; + extraConfig = "unmap gd"; + rifle = [ + { + condition = "mime ^text, label editor"; + command = ''vim -- "$@"''; + } + { + condition = "mime ^text, label pager"; + command = ''less -- "$@"''; + } + ]; + }; + + nmt.script = '' + assertFileExists home-files/.config/ranger/rc.conf + assertFileContent home-files/.config/ranger/rc.conf \ + ${./configuration-rc.conf} + + assertFileExists home-files/.config/ranger/rifle.conf + assertFileContent home-files/.config/ranger/rifle.conf \ + ${./configuration-rifle.conf} + ''; +} diff --git a/tests/modules/programs/ranger/default.nix b/tests/modules/programs/ranger/default.nix new file mode 100644 index 000000000..84579d537 --- /dev/null +++ b/tests/modules/programs/ranger/default.nix @@ -0,0 +1 @@ +{ ranger-configuration = ./configuration.nix; }