Skip to content

Commit 0a64bfb

Browse files
committed
nix flake refactor: avoid with lib; & use lib.optional*
Upstream Nixpkgs has been pushing the style of removing ``with lib;`` with the reasoning of clarity around where variables in scope come that hurt readability & static analysis[*]. While on the topic of ``lib``, introduce ``lib.optional`` & ``lib.optionalString`` in a few more places to tidy up the code while using common library patterns. .. [*] https://nix.dev/guides/best-practices#with-scopes Format: text/x-rst
1 parent 8ca227e commit 0a64bfb

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

flake.nix

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,34 @@
1010
in
1111
{
1212
nixosModule = { config, ... }:
13-
with nixpkgs.lib;
1413
let
14+
inherit (nixpkgs) lib;
1515
cfg = config.networking.stevenBlackHosts;
16-
alternatesList = (if cfg.blockFakenews then [ "fakenews" ] else []) ++
17-
(if cfg.blockGambling then [ "gambling" ] else []) ++
18-
(if cfg.blockPorn then [ "porn" ] else []) ++
19-
(if cfg.blockSocial then [ "social" ] else []);
16+
alternatesList =
17+
(lib.optional cfg.blockFakenews "fakenews")
18+
++ (lib.optional cfg.blockGambling "gambling")
19+
++ (lib.optional cfg.blockPorn "porn")
20+
++ (lib.optional cfg.blockSocial "social");
2021
alternatesPath = "alternates/" + builtins.concatStringsSep "-" alternatesList + "/";
2122
in
2223
{
2324
options.networking.stevenBlackHosts = {
24-
enable = mkEnableOption "Steven Black's hosts file";
25-
enableIPv6 = mkEnableOption "IPv6 rules" // {
25+
enable = lib.mkEnableOption "Steven Black's hosts file";
26+
enableIPv6 = lib.mkEnableOption "IPv6 rules" // {
2627
default = config.networking.enableIPv6;
2728
};
28-
blockFakenews = mkEnableOption "fakenews hosts entries";
29-
blockGambling = mkEnableOption "gambling hosts entries";
30-
blockPorn = mkEnableOption "porn hosts entries";
31-
blockSocial = mkEnableOption "social hosts entries";
29+
blockFakenews = lib.mkEnableOption "fakenews hosts entries";
30+
blockGambling = lib.mkEnableOption "gambling hosts entries";
31+
blockPorn = lib.mkEnableOption "porn hosts entries";
32+
blockSocial = lib.mkEnableOption "social hosts entries";
3233
};
33-
config = mkIf cfg.enable {
34+
config = lib.mkIf cfg.enable {
3435
networking.extraHosts =
3536
let
36-
orig = builtins.readFile ("${self}/" + (if alternatesList != [] then alternatesPath else "") + "hosts");
37+
orig = builtins.readFile ("${self}/" + (lib.optionalString (alternatesList != []) alternatesPath) + "hosts");
3738
ipv6 = builtins.replaceStrings [ "0.0.0.0" ] [ "::" ] orig;
38-
in orig + (optionalString cfg.enableIPv6 ("\n" + ipv6));
39+
in
40+
orig + (lib.optionalString cfg.enableIPv6 ("\n" + ipv6));
3941
};
4042
};
4143

0 commit comments

Comments
 (0)