|
| 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