Skip to content

Instantly share code, notes, and snippets.

@rtlong
Last active October 15, 2024 13:22
Show Gist options
  • Save rtlong/fea6031507a861b66f3de42b6bcf6a05 to your computer and use it in GitHub Desktop.
Save rtlong/fea6031507a861b66f3de42b6bcf6a05 to your computer and use it in GitHub Desktop.
Nix Dnsmasq module

To use this, reference this file's path in the modules attr for your system config:

      darwinConfigurations.${hostname} = nix-darwin.lib.darwinSystem {
        modules = [
          ./lib/dnsmasq.nix
        ];
      };
{ config, lib, pkgs, ... }:
with lib;
let
mapA = f: attrs: with builtins; attrValues (mapAttrs f attrs);
package = pkgs.dnsmasq;
addresses = {
test = "127.0.0.1"; # redirect all queries for *.test TLD to localhost
localhost = "127.0.0.1"; # redirect all queries for *.localhost TLD to localhost
};
bind = "127.0.0.1";
port = 53;
args = [
"--listen-address=${bind}"
"--port=${toString port}"
"--no-daemon"
] ++ (mapA (domain: addr: "--address=/${domain}/${addr}") addresses);
in
{
environment.systemPackages = [ package ];
launchd.daemons.dnsmasq = {
# serviceConfig.Debug = true;
serviceConfig.ProgramArguments = [
"/bin/sh"
"-c"
"/bin/wait4path ${package} && exec ${package}/bin/dnsmasq ${toString args}"
];
serviceConfig.StandardOutPath = /var/log/dnsmasq.log;
serviceConfig.StandardErrorPath = /var/log/dnsmasq.log;
serviceConfig.RunAtLoad = true;
serviceConfig.KeepAlive = true;
};
environment.etc = builtins.listToAttrs (builtins.map
(domain: {
name = "resolver/${domain}";
value = {
enable = true;
text = ''
port ${toString port}
nameserver ${bind}
'';
};
})
(builtins.attrNames addresses));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment