fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
2.3 KiB85 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.pistol;
7
8  configFile = concatStringsSep "\n" (map ({ fpath, mime, command }:
9    if fpath == "" then "${mime} ${command}" else "fpath ${fpath} ${command}")
10    cfg.associations);
11
12  association = types.submodule {
13    options = {
14      command = mkOption {
15        type = types.str;
16        description = "Preview command for files matched by this association.";
17      };
18
19      fpath = mkOption {
20        type = types.str;
21        default = "";
22        description = "File path regex that this association should match.";
23      };
24
25      mime = mkOption {
26        type = types.str;
27        default = "";
28        description = "Mime type regex that this association should match.";
29      };
30    };
31  };
32in {
33  imports = [
34    (mkRemovedOptionModule [ "programs" "pistol" "config" ]
35      "Pistol is now configured with programs.pistol.associations.")
36  ];
37
38  meta.maintainers = [ hm.maintainers.mtoohey ];
39
40  options.programs.pistol = {
41    enable = mkEnableOption "file previewer for terminal file managers";
42
43    associations = mkOption {
44      type = types.listOf association;
45      default = [ ];
46      example = literalExpression ''
47        [
48          { mime = "application/json"; command = "bat %pistol-filename%"; }
49          { mime = "application/*"; command = "hexyl %pistol-filename%"; }
50          { fpath = ".*.md$"; command = "sh: bat --paging=never --color=always %pistol-filename% | head -8"; }
51        ]
52      '';
53      description = ''
54        Associations written to the Pistol configuration at
55        {file}`$XDG_CONFIG_HOME/pistol/pistol.conf`.
56      '';
57    };
58
59  };
60
61  config = mkIf cfg.enable (mkMerge [
62    {
63      assertions = [{
64        assertion = all ({ fpath, mime, ... }:
65          (fpath != "" && mime == "") || (fpath == "" && mime != ""))
66          cfg.associations;
67        message = ''
68          Each entry in programs.pistol.associations must contain exactly one
69          of fpath or mime.
70        '';
71      }];
72
73      home.packages = [ pkgs.pistol ];
74    }
75
76    (mkIf (cfg.associations != [ ] && pkgs.stdenv.hostPlatform.isDarwin) {
77      home.file."Library/Application Support/pistol/pistol.conf".text =
78        configFile;
79    })
80
81    (mkIf (cfg.associations != [ ] && !pkgs.stdenv.hostPlatform.isDarwin) {
82      xdg.configFile."pistol/pistol.conf".text = configFile;
83    })
84  ]);
85}