-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathhostctl.nix
More file actions
62 lines (53 loc) · 1.59 KB
/
hostctl.nix
File metadata and controls
62 lines (53 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
";
};
};
}