summaryrefslogtreecommitdiffstats
path: root/nixos/modules/config/stevenblack.nix
blob: c09a913bcdbe4b280dc5196bb47cf64d61edc116 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib)
    getOutput
    maintainers
    mkEnableOption
    mkIf
    mkOption
    mkPackageOption
    types
    ;

  cfg = config.networking.stevenblack;

  filterHostsFile =
    hostsFile:
    if cfg.whitelist == [ ] then
      hostsFile
    else
      let
        pattern = lib.escape [ "." "|" ] (lib.concatStringsSep "|" cfg.whitelist);
      in
      pkgs.runCommand "filtered-hosts" { } ''
        sed '/${pattern}/d' ${hostsFile} > $out
      '';
in
{
  options.networking.stevenblack = {
    enable = mkEnableOption "the stevenblack hosts file blocklist";

    package = mkPackageOption pkgs "stevenblack-blocklist" { };

    block = mkOption {
      type = types.listOf (
        types.enum [
          "fakenews"
          "gambling"
          "porn"
          "social"
        ]
      );
      default = [ ];
      description = "Additional blocklist extensions.";
    };

    whitelist = mkOption {
      # https://datatracker.ietf.org/doc/html/rfc1035
      type = types.listOf (types.strMatching "^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)+$");
      default = [ ];
      description = "Domains to exclude from blocking.";
      example = [ "s.click.aliexpress.com" ];
    };
  };

  config = mkIf cfg.enable {
    networking.hostFiles = map (x: filterHostsFile "${getOutput x cfg.package}/hosts") (
      [ "ads" ] ++ cfg.block
    );
  };

  meta.maintainers = with maintainers; [
    moni
    artturin
    frontear
  ];
}