Skip to content
Open
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
nixpkgsFor = forAllSystems (system: import nixpkgs {
inherit system;
});

toUnboundConf = (
file:
nixpkgs.lib.strings.concatMapStringsSep "\n" (
line:
if (nixpkgs.lib.strings.hasPrefix "#" line) || (line == "") then
line
else
let
split_line = (nixpkgs.lib.strings.splitString " " line);
address = builtins.elemAt split_line 0;
domain = builtins.elemAt split_line 1;
in
''
local-zone: "${domain}" redirect
local-data: "${domain} A ${address}"
''
) (nixpkgs.lib.strings.splitString "\n" (builtins.readFile file))
);
in
{
nixosModule = { config, ... }:
Expand Down Expand Up @@ -50,5 +69,35 @@
];
};
});

overlays.default = (
final: prev: {
unboundconfs = prev.lib.makeScope prev.pkgs.newScope (newscope: self.packages.${prev.system});
}
);

packages = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};
dir = ./alternates;
lists =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block would probably run faster as a single fold instead of iterating the whole list 3 times (4 including the post-let mapAttrs). If it isn’t that expensive, I still think lib.pipe would be a lot easier to read this case instead of trying to read the block backwards from lines bottom to top.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I originally wrote this I was using the experimental pipe-operator and then converted the code to the original pipe-less nix but lib.pipe does seem like an okay compromise

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. The pipe change here I think is a lot easier for the reader to follow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree but I wasn't sure what's the consensus regarding usage of lib.pipe. In my own nix code I just use pipe operator everywhere but I understand this is not common practice

pkgs.lib.trivial.pipe (builtins.readDir ./alternates) [
(pkgs.lib.attrsets.filterAttrs (k: v: v == "directory"))
(pkgs.lib.attrsets.mapAttrs (k: v: dir + "/${k}/hosts"))
(pkgs.lib.attrsets.filterAttrs (k: v: builtins.pathExists v))
]
// {
all = ./hosts;
};
in
pkgs.lib.attrsets.mapAttrs (
k: v:
pkgs.writeTextFile {
name = k;
text = toUnboundConf v;
}
) lists
);
};
}