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 Jan 28, 2021
1 parent d2224d2 commit 710d8e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions devshell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,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 modules/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
";
};
};
}
1 change: 1 addition & 0 deletions modules/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let
}];
}
./git-hooks.nix
./hostctl.nix
];

pkgsModule = { config, ... }: {
Expand Down

0 comments on commit 710d8e2

Please sign in to comment.