aboutsummaryrefslogtreecommitdiff
path: root/modules/newsboat.home/default.nix
blob: 823d5c3fad4aae165a0764d63144d620879b1205 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{ lib, pkgs, ... }@opts:
let
  cfg = import ./config.nix;
  urls = mergeFeeds [ (import ./public-feed.nix opts) privateFeed ];
  privateFeed = let feedpath = ./private-feed.nix;
    in if builtins.pathExists feedpath then import feedpath opts else {};

  mergeFeeds = feeds: lib.attrsets.foldAttrs (value: acc:
    if lib.isList value
      then value ++ (if acc != null then acc else [])
      else value // (if acc != null then acc else {})
  ) null ([{ queries = []; visible = []; hidden = []; }] ++ feeds);

  joinLines = lib.concatStringsSep "\n";
  joinWords = lib.concatStringsSep " ";

  toUrl = url: joinWords (lib.map toString [
    url.url
    (lib.optional (url ? "hidden" && url.hidden) "!")
    (lib.optional (url ? "tags") (joinWords url.tags))
    (lib.optional (url ? "title") ''"~${url.title}"'')
  ]);

  toUrls = urls: joinLines (lib.map toUrl urls);

  toQueries = queries: joinLines
    (lib.map (q: "\"query:${q.title}:${q.query}\"") queries);

  # { raw = a; } -> a | bool -> yesno | string -> "string" | number
  toConfigValue = val:
    if val ? "raw" then toString val.raw
    else if lib.isBool val then lib.hm.booleans.yesNo val
    else if lib.isString val then ''"${val}"''
    else toString val;

  cfgFileContent = joinLines [
    (joinLines
      (lib.mapAttrsToList (key: val: "${key} ${toConfigValue val}") cfg.config))
    (joinLines
      (lib.mapAttrsToList (key: val: "bind-key ${key} ${val}") cfg.keys))
    (joinLines
      (lib.mapAttrsToList (key: val: "macro ${key} ${val}") cfg.macros))
    cfg.extraConfig
  ];

  urlsFileContent = joinLines [
    (toQueries urls.queries)
    (toUrls urls.visible)
    (toUrls (lib.map (x: x // { hidden = true; }) urls.hidden))
  ];
in {
  home.packages = [
    pkgs.newsboat
    pkgs.piper-tts
    pkgs.nodePackages.peerflix
  ];

  xdg.configFile = {
    "newsboat/urls".text = urlsFileContent;
    "newsboat/config".text = cfgFileContent;
    "newsboat/opener.sh".source = ./opener.sh;
  };
}