|
| 1 | +{ lib, pkgs, config }: |
| 2 | +let |
| 3 | + inherit (config) |
| 4 | + name |
| 5 | + dev-ca-path |
| 6 | + static-dns |
| 7 | + ; |
| 8 | + installProjectCA = { |
| 9 | + name = "ca-install"; |
| 10 | + help = "install dev CA"; |
| 11 | + category = "host state"; |
| 12 | + package = pkgs.mkcert; |
| 13 | + command = '' |
| 14 | + echo "$(tput bold)Installing the ${name}'s dev CA into local trust stores via mkcert command ...$(tput sgr0)" |
| 15 | + export CAROOT=${dev-ca-path} |
| 16 | + ${pkgs.mkcert}/bin/mkcert -install |
| 17 | + ''; |
| 18 | + }; |
| 19 | + uninstallProjectCA = { |
| 20 | + name = "ca-uninstall"; |
| 21 | + help = "uninstall dev CA"; |
| 22 | + category = "host state"; |
| 23 | + package = pkgs.mkcert; |
| 24 | + command = '' |
| 25 | + echo "$(tput bold)Purging the ${name}'s dev CA from local trust stores via mkcert command ...$(tput sgr0)" |
| 26 | + export CAROOT=${dev-ca-path} |
| 27 | + ${pkgs.mkcert}/bin/mkcert -uninstall |
| 28 | + ''; |
| 29 | + }; |
| 30 | + |
| 31 | + etcHosts = pkgs.writeText "${name}-etchosts" |
| 32 | + (lib.concatStringsSep "\n" |
| 33 | + (lib.mapAttrsToList (name: value: value + " " + name) static-dns) |
| 34 | + ); |
| 35 | + # since this temporarily modifies /etc/hosts, use of sudo can't be avoided |
| 36 | + fqdnsActivate = { |
| 37 | + name = "dns-activate"; |
| 38 | + category = "host state"; |
| 39 | + help = "activate pre-configured static dns"; |
| 40 | + package = pkgs.hostctl; |
| 41 | + command = '' |
| 42 | + echo "$(tput bold)Installing ${name}'s static local DNS resolution via hostctl command ...$(tput sgr0)" |
| 43 | + sudo ${pkgs.hostctl}/bin/hostctl add ${name} --from ${etcHosts} |
| 44 | + ''; |
| 45 | + }; |
| 46 | + fqdnsDeactivate = { |
| 47 | + name = "dns-deactivate"; |
| 48 | + category = "host state"; |
| 49 | + help = "deactivate pre-configured static dns"; |
| 50 | + package = pkgs.hostctl; |
| 51 | + command = '' |
| 52 | + echo "$(tput bold)Purging ${name}'s static local DNS resolution via hostctl command ...$(tput sgr0)" |
| 53 | + sudo ${pkgs.hostctl}/bin/hostctl remove ${name} |
| 54 | + ''; |
| 55 | + }; |
| 56 | +in |
| 57 | +( |
| 58 | + if static-dns == null || static-dns == "" then [ ] |
| 59 | + else [ fqdnsActivate fqdnsDeactivate ] |
| 60 | +) ++ |
| 61 | +( |
| 62 | + if dev-ca-path == null || dev-ca-path == "" then [ ] |
| 63 | + else [ installProjectCA uninstallProjectCA ] |
| 64 | +) |
0 commit comments