summaryrefslogtreecommitdiffstats
path: root/lass/3modules/browsers.nix
blob: ccb108f8a2d552e2591184abd8c77057b2167d16 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{ config, lib, pkgs, ... }:
with import <stockholm/lib>;
let

  cfg = config.lass.browser;

  browserScripts = {
    chromium = "${pkgs.chromium}/bin/chromium";
    firefox = "${pkgs.firefox.override {
        extraNativeMessagingHosts = [ pkgs.tridactyl-native ];
      }}/bin/firefox";
    qutebrowser = "${pkgs.qutebrowser}/bin/qutebrowser";
  };

  browser-select = let
    sortedPaths = sort (a: b: a.value.precedence > b.value.precedence)
                       (mapAttrsToList (name: value: { inherit name value; })
                                       cfg.config);
  in if (lib.length sortedPaths) > 1 then
    pkgs.writeScriptBin "browser-select" ''
      BROWSER=$(echo -e "${concatStringsSep "\\n" (map (getAttr "name") sortedPaths)}" | ${pkgs.dmenu}/bin/dmenu)
      case $BROWSER in
      ${concatMapStringsSep "\n" (n: ''
        ${n.name})
          export BIN=${config.lass.xjail-bins.${n.name}}/bin/${n.name}
          ;;
      '') (sortedPaths)}
      esac
      $BIN "$@"
    ''
  else
    let
      name = (lib.head sortedPaths).name;
    in pkgs.writeScriptBin "browser-select2" ''
      ${config.lass.xjail-bins.${name}}/bin/${name} "$@"
    ''
  ;

in {
  options.lass.browser = {
    select = mkOption {
      type = types.path;
    };
    config = mkOption {
      type = types.attrsOf (types.submodule ({ config, ... }: {
        options = {
          name = mkOption {
            type = types.str;
            default = config._module.args.name;
          };
          precedence = mkOption {
            type = types.int;
            default = 0;
          };
          user = mkOption {
            type = types.str;
            default = config._module.args.name;
          };
          browser = mkOption {
            type = types.enum (attrNames browserScripts);
            default = "chromium";
          };
          groups = mkOption {
            type = types.listOf types.str;
            default = [];
          };
        };
      }));
      default = {};
    };
  };

  config = (mkIf (cfg.config != {}) {
    lass.xjail = mapAttrs' (name: browser:
      nameValuePair name {
        script = browserScripts.${browser.browser};
        groups = browser.groups;
      }
    ) cfg.config;
    environment.systemPackages = (map (browser:
      config.lass.xjail-bins.${browser.name}
    ) (attrValues cfg.config))  ++ [
      browser-select
    ];
    lass.browser.select = browser-select;
  });
}