From 5f094fa3460671f712d2b926c6334eaa87d42c69 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 27 Jan 2021 20:42:50 -0500 Subject: [PATCH 1/3] modules: introduce hostctl (static dns for development) --- devshell.toml | 8 +++++- extra/dns/hostctl.nix | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 extra/dns/hostctl.nix diff --git a/devshell.toml b/devshell.toml index 9b32d56e..ba8ad632 100644 --- a/devshell.toml +++ b/devshell.toml @@ -1,5 +1,6 @@ imports = [ - "language.go" + "language.go", + "dns.hostctl" ] [devshell] @@ -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" diff --git a/extra/dns/hostctl.nix b/extra/dns/hostctl.nix new file mode 100644 index 00000000..a9b250e0 --- /dev/null +++ b/extra/dns/hostctl.nix @@ -0,0 +1,62 @@ +{ lib, pkgs, config, ... }: +with lib; +let + cfg = config.hostctl; + profile = toLower 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 + "; + }; + }; +} From ca17c32e68f4c8ecb028b2f36d281c6ef4942a08 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 12 May 2021 14:05:42 -0400 Subject: [PATCH 2/3] typo Co-authored-by: Robert Hensing --- extra/dns/hostctl.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/dns/hostctl.nix b/extra/dns/hostctl.nix index a9b250e0..259ab15a 100644 --- a/extra/dns/hostctl.nix +++ b/extra/dns/hostctl.nix @@ -35,7 +35,7 @@ let in { options.hostctl = { - enable = mkEnableOption "manage temoprary /etc/host entries for development from within the shell"; + enable = mkEnableOption "manage temporary /etc/host entries for development from within the shell"; dns = mkOption { type = types.attrs; From 6a0941649bd18163887679cb968cf9bbeceec027 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 12 May 2021 14:06:08 -0400 Subject: [PATCH 3/3] style Co-authored-by: Robert Hensing --- extra/dns/hostctl.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/extra/dns/hostctl.nix b/extra/dns/hostctl.nix index 259ab15a..9755e8e9 100644 --- a/extra/dns/hostctl.nix +++ b/extra/dns/hostctl.nix @@ -41,12 +41,11 @@ in type = types.attrs; default = {}; description = "configure static dns entries"; - example = literalExample '' + example = { dns."some.host" = "1.2.3.4"; dns."another.host" = "4.3.2.1"; - } - ''; + }; }; };