fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
2.3 KiB83 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.feh;
8
9  bindingsOf = t: with types; attrsOf (nullOr (either t (listOf t)));
10
11  renderBindings = bindings:
12    let
13      enabled = filterAttrs (n: v: v != null) bindings;
14      disabled = filterAttrs (n: v: v == null) bindings;
15      render = mapAttrsToList renderBinding;
16    in concatStringsSep "\n" (render disabled ++ render enabled);
17
18  renderBinding = func: key:
19    if key == null then
20      func
21    else if isList key then
22      concatStringsSep " " ([ func ] ++ map toString key)
23    else
24      "${func} ${toString key}";
25
26in {
27  options.programs.feh = {
28    enable = mkEnableOption "feh - a fast and light image viewer";
29
30    package = mkPackageOption pkgs "feh" { };
31
32    buttons = mkOption {
33      default = { };
34      type = with types; bindingsOf (either str int);
35      example = {
36        zoom_in = 4;
37        zoom_out = "C-4";
38        prev_img = [ 3 "C-3" ];
39      };
40      description = ''
41        Override feh's default mouse button mapping. If you want to disable an
42        action, set its value to null. If you want to bind multiple buttons to
43        an action, set its value to a list.
44        See <https://man.finalrewind.org/1/feh/#x425554544f4e53> for
45        default bindings and available commands.
46      '';
47    };
48
49    keybindings = mkOption {
50      default = { };
51      type = bindingsOf types.str;
52      example = {
53        zoom_in = "plus";
54        zoom_out = "minus";
55        prev_img = [ "h" "Left" ];
56      };
57      description = ''
58        Override feh's default keybindings. If you want to disable a keybinding
59        set its value to null. If you want to bind multiple keys to an action,
60        set its value to a list.
61        See <https://man.finalrewind.org/1/feh/#x4b455953> for
62        default bindings and available commands.
63      '';
64    };
65  };
66
67  config = mkIf cfg.enable {
68    assertions = [{
69      assertion = ((filterAttrs (n: v: v == "") cfg.keybindings) == { });
70      message =
71        "To disable a keybinding, use `null` instead of an empty string.";
72    }];
73
74    home.packages = [ cfg.package ];
75
76    xdg.configFile."feh/buttons" =
77      mkIf (cfg.buttons != { }) { text = renderBindings cfg.buttons + "\n"; };
78
79    xdg.configFile."feh/keys" = mkIf (cfg.keybindings != { }) {
80      text = renderBindings cfg.keybindings + "\n";
81    };
82  };
83}