-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modules: introduce hostctl (static dns for development)
- Loading branch information
David Arnold
committed
Mar 1, 2021
1 parent
ff6cffb
commit b205af0
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
"; | ||
}; | ||
}; | ||
} |