Skip to content

Commit

Permalink
modules: introduce hostctl (static dns for development)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Arnold committed Mar 1, 2021
1 parent ff6cffb commit b205af0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion devshell.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports = [
"language.go"
"language.go",
"dns.hostctl"
]

[devshell]
Expand Down Expand Up @@ -45,3 +46,8 @@ category = "utilites"
help = "golang linter"
package = "golangci-lint"
category = "linters"

[hostctl]
enable = true
dns."test.domain.local" = "172.0.0.1"
dns."shared.domain.link-local" = "169.254.0.5"
62 changes: 62 additions & 0 deletions extra/dns/hostctl.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{ lib, pkgs, config, ... }:
with lib;
let
cfg = config.hostctl;
profile = config.devshell.name;

etcHosts = pkgs.writeText "${profile}-etchosts" (
concatStringsSep "\n"
(mapAttrsToList (host: ip: ip + " " + host) cfg.dns)
);

# Execute this script to install the project's static dns entries
install-hostctl-dns = pkgs.writeShellScriptBin "install-hostctl-dns" ''
set -euo pipefail
shopt -s nullglob
log() {
IFS=$'\n' loglines=($*)
for line in ${"$"}{loglines[@]}; do echo -e "[hostctl] $line" >&2; done
}
# Install local CA into system, java and nss (includes Firefox) trust stores
log "Update static dns entries..."
sudo -K
log $(sudo ${pkgs.hostctl}/bin/hostctl add ${profile} --from ${etcHosts} 2>&1)
uninstall() {
log $(sudo ${pkgs.hostctl}/bin/hostctl remove ${profile} 2>&1)
}
# TODO: Uninstall when leaving the devshell
# trap uninstall EXIT
'';
in
{
options.hostctl = {
enable = mkEnableOption "manage temoprary /etc/host entries for development from within the shell";

dns = mkOption {
type = types.attrs;
default = {};
description = "configure static dns entries";
example = literalExample ''
{
dns."some.host" = "1.2.3.4";
dns."another.host" = "4.3.2.1";
}
'';
};
};

config = mkIf cfg.enable {
commands = [ { package = pkgs.hostctl; category = "dns"; } ];
devshell = {
packages = [ install-hostctl-dns ];
startup.install-hostctl-dns.text = "
$DEVSHELL_DIR/bin/install-hostctl-dns
";
};
};
}

0 comments on commit b205af0

Please sign in to comment.