Skip to content

Commit b205af0

Browse files
author
David Arnold
committed
modules: introduce hostctl (static dns for development)
1 parent ff6cffb commit b205af0

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

devshell.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
imports = [
2-
"language.go"
2+
"language.go",
3+
"dns.hostctl"
34
]
45

56
[devshell]
@@ -45,3 +46,8 @@ category = "utilites"
4546
help = "golang linter"
4647
package = "golangci-lint"
4748
category = "linters"
49+
50+
[hostctl]
51+
enable = true
52+
dns."test.domain.local" = "172.0.0.1"
53+
dns."shared.domain.link-local" = "169.254.0.5"

extra/dns/hostctl.nix

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{ lib, pkgs, config, ... }:
2+
with lib;
3+
let
4+
cfg = config.hostctl;
5+
profile = config.devshell.name;
6+
7+
etcHosts = pkgs.writeText "${profile}-etchosts" (
8+
concatStringsSep "\n"
9+
(mapAttrsToList (host: ip: ip + " " + host) cfg.dns)
10+
);
11+
12+
# Execute this script to install the project's static dns entries
13+
install-hostctl-dns = pkgs.writeShellScriptBin "install-hostctl-dns" ''
14+
set -euo pipefail
15+
shopt -s nullglob
16+
17+
log() {
18+
IFS=$'\n' loglines=($*)
19+
for line in ${"$"}{loglines[@]}; do echo -e "[hostctl] $line" >&2; done
20+
}
21+
22+
# Install local CA into system, java and nss (includes Firefox) trust stores
23+
log "Update static dns entries..."
24+
sudo -K
25+
log $(sudo ${pkgs.hostctl}/bin/hostctl add ${profile} --from ${etcHosts} 2>&1)
26+
27+
uninstall() {
28+
log $(sudo ${pkgs.hostctl}/bin/hostctl remove ${profile} 2>&1)
29+
}
30+
31+
# TODO: Uninstall when leaving the devshell
32+
# trap uninstall EXIT
33+
34+
'';
35+
in
36+
{
37+
options.hostctl = {
38+
enable = mkEnableOption "manage temoprary /etc/host entries for development from within the shell";
39+
40+
dns = mkOption {
41+
type = types.attrs;
42+
default = {};
43+
description = "configure static dns entries";
44+
example = literalExample ''
45+
{
46+
dns."some.host" = "1.2.3.4";
47+
dns."another.host" = "4.3.2.1";
48+
}
49+
'';
50+
};
51+
};
52+
53+
config = mkIf cfg.enable {
54+
commands = [ { package = pkgs.hostctl; category = "dns"; } ];
55+
devshell = {
56+
packages = [ install-hostctl-dns ];
57+
startup.install-hostctl-dns.text = "
58+
$DEVSHELL_DIR/bin/install-hostctl-dns
59+
";
60+
};
61+
};
62+
}

0 commit comments

Comments
 (0)