fork/home-manager

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

KFearsoffnewsboat: allow imperative "urls" managementc0f9cbcf

master
4.1 KiB148 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.newsboat;
7  wrapQuote = x: ''"${x}"'';
8
9  urlsFileContents = let
10    mkUrlEntry = u:
11      concatStringsSep " " ([ u.url ] ++ map wrapQuote u.tags
12        ++ optional (u.title != null) (wrapQuote "~${u.title}"));
13    urls = map mkUrlEntry cfg.urls;
14
15    mkQueryEntry = n: v: ''"query:${n}:${escape [ ''"'' ] v}"'';
16    queries = mapAttrsToList mkQueryEntry cfg.queries;
17  in concatStringsSep "\n"
18  (if versionAtLeast config.home.stateVersion "20.03" then
19    queries ++ urls
20  else
21    urls ++ queries) + "\n";
22
23  configFileContents = ''
24    max-items ${toString cfg.maxItems}
25    browser ${cfg.browser}
26    reload-threads ${toString cfg.reloadThreads}
27    auto-reload ${lib.hm.booleans.yesNo cfg.autoReload}
28    ${optionalString (cfg.reloadTime != null)
29    (toString "reload-time ${toString cfg.reloadTime}")}
30    prepopulate-query-feeds yes
31
32    ${cfg.extraConfig}
33  '';
34
35in {
36  meta.maintainers = [ maintainers.sumnerevans ];
37
38  options = {
39    programs.newsboat = {
40      enable = mkEnableOption "the Newsboat feed reader";
41
42      urls = mkOption {
43        type = types.listOf (types.submodule {
44          options = {
45            url = mkOption {
46              type = types.str;
47              example = "http://example.com";
48              description = "Feed URL.";
49            };
50
51            tags = mkOption {
52              type = types.listOf types.str;
53              default = [ ];
54              example = [ "foo" "bar" ];
55              description = "Feed tags.";
56            };
57
58            title = mkOption {
59              type = types.nullOr types.str;
60              default = null;
61              example = "ORF News";
62              description = "Feed title.";
63            };
64          };
65        });
66        default = [ ];
67        example = [{
68          url = "http://example.com";
69          tags = [ "foo" "bar" ];
70        }];
71        description = ''
72          List of news feeds. Leave it empty if you want to manage feeds
73          imperatively, for example, using Syncthing.
74        '';
75      };
76
77      maxItems = mkOption {
78        type = types.int;
79        default = 0;
80        description = "Maximum number of items per feed, 0 for infinite.";
81      };
82
83      reloadThreads = mkOption {
84        type = types.int;
85        default = 5;
86        description = "How many threads to use for updating the feeds.";
87      };
88
89      autoReload = mkOption {
90        type = types.bool;
91        default = false;
92        description = ''
93          Whether to enable automatic reloading while newsboat is running.
94        '';
95      };
96
97      reloadTime = mkOption {
98        type = types.nullOr types.int;
99        default = 60;
100        description = "Time in minutes between reloads.";
101      };
102
103      browser = mkOption {
104        type = types.str;
105        default = "${pkgs.xdg-utils}/bin/xdg-open";
106        description = "External browser to use.";
107      };
108
109      queries = mkOption {
110        type = types.attrsOf types.str;
111        default = { };
112        example = { "foo" = ''rssurl =~ "example.com"''; };
113        description = "A list of queries to use.";
114      };
115
116      extraConfig = mkOption {
117        type = types.lines;
118        default = "";
119        description = ''
120          Extra configuration values that will be appended to the end.
121        '';
122      };
123    };
124  };
125
126  config = mkIf cfg.enable {
127    assertions = [{
128      assertion = cfg.queries != { } -> cfg.urls != [ ];
129      message = ''
130        Cannot specify queries if urls is empty. Unset queries if you
131        want to manage urls imperatively.
132      '';
133    }];
134
135    home.packages = [ pkgs.newsboat ];
136
137    # Use ~/.newsboat on stateVersion < 21.05 and use ~/.config/newsboat for
138    # stateVersion >= 21.05.
139    home.file = mkIf (versionOlder config.home.stateVersion "21.05") {
140      ".newsboat/urls" = mkIf (cfg.urls != [ ]) { text = urlsFileContents; };
141      ".newsboat/config".text = configFileContents;
142    };
143    xdg.configFile = mkIf (versionAtLeast config.home.stateVersion "21.05") {
144      "newsboat/urls" = mkIf (cfg.urls != [ ]) { text = urlsFileContents; };
145      "newsboat/config".text = configFileContents;
146    };
147  };
148}