Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@
nixosModule = { config, ... }:
with nixpkgs.lib;
let
inherit (nixpkgs) lib;
cfg = config.networking.stevenBlackHosts;
alternatesList = (if cfg.blockFakenews then [ "fakenews" ] else []) ++
(if cfg.blockGambling then [ "gambling" ] else []) ++
(if cfg.blockPorn then [ "porn" ] else []) ++
(if cfg.blockSocial then [ "social" ] else []);
alternatesPath = "alternates/" + builtins.concatStringsSep "-" alternatesList + "/";
alternatesPath = "/alternates/" + builtins.concatStringsSep "-" alternatesList;
orig = builtins.readFile (
self.outPath + (if alternatesList != [ ] then alternatesPath else "") + "/hosts"
);
filtered =
if cfg.enableIPv6 then
orig
else
lib.concatStringsSep "\n" (
lib.filter (line: !(lib.strings.hasPrefix "::" line)) (lib.strings.splitString "\n" orig)
);
in
{
options.networking.stevenBlackHosts = {
Expand All @@ -32,11 +43,7 @@
blockSocial = mkEnableOption "social hosts entries";
};
config = mkIf cfg.enable {
networking.extraHosts =
let
orig = builtins.readFile ("${self}/" + (if alternatesList != [] then alternatesPath else "") + "hosts");
ipv6 = builtins.replaceStrings [ "0.0.0.0" ] [ "::" ] orig;
in orig + (optionalString cfg.enableIPv6 ("\n" + ipv6));
networking.extraHosts = filtered;
};
};

Expand Down
18 changes: 15 additions & 3 deletions makeHosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def recursively_loop_extensions(extension, extensions, current_extensions):

name = "-".join(c_current_extensions)

params = ("-a", "-n", "-o", "alternates/"+name, "-e") + tuple(c_current_extensions)
params = ("-a", "-d", "-n", "-o", "alternates/"+name, "-e") + tuple(c_current_extensions)
update_hosts_file(*params)

params = ("-a", "-n", "-s", "--nounifiedhosts", "-o", "alternates/"+name+"-only", "-e") + tuple(c_current_extensions)
params = ("-a", "-d", "-n", "-s", "--nounifiedhosts", "-o", "alternates/"+name+"-only", "-e") + tuple(c_current_extensions)
update_hosts_file(*params)

while len(c_extensions) > 0:
Expand All @@ -83,10 +83,22 @@ def main():
"file from hosts stored in "
"data subfolders."
)
parser.add_argument(
"--noupdate",
"-n",
dest="noupdate",
default=False,
action="store_true",
help="Don't update from host data sources.",
)
parser.parse_args()
options = vars(parser.parse_args())

# Update the unified hosts file
update_hosts_file("-a")
if options["noupdate"]:
update_hosts_file("-a", "-d", "-n")
else:
update_hosts_file("-a", "-d")

# List of extensions we want to generate, we will loop over them recursively to prevent manual definitions
# Only add new extensions to the end of the array, to avoid relocating existing hosts-files
Expand Down
5 changes: 3 additions & 2 deletions testUpdateHostsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_get_defaults(self):
"minimise": False,
"outputsubfolder": "",
"hostfilename": "hosts",
"targetip": "0.0.0.0",
"targetips": ["0.0.0.0", "::"],
"sourcedatafilename": "update.json",
"sourcesdata": [],
"readmefilename": "readme.md",
Expand All @@ -131,6 +131,7 @@ def test_get_defaults(self):
"commonexclusions": ["hulu.com"],
"blacklistfile": "foo" + self.sep + "blacklist",
"whitelistfile": "foo" + self.sep + "whitelist",
"addsystemhostname": True,
}
self.assertDictEqual(actual, expected)

Expand Down Expand Up @@ -1092,7 +1093,7 @@ def test_basic_include_static_hosts(self):

def test_basic_include_static_hosts_linux(self):
kwargs = dict(
extensions="", outputsubfolder="", numberofrules=5, skipstatichosts=False, nounifiedhosts=False
extensions="", outputsubfolder="", numberofrules=5, skipstatichosts=False, nounifiedhosts=False, addsystemhostname=True
)
with self.mock_property("platform.system") as system:
system.return_value = "Linux"
Expand Down
Loading
Loading