Skip to content

Commit 1eb7c1a

Browse files
author
David Arnold
committed
Add mkcert and hostctl instrumentation
1 parent 4859a38 commit 1eb7c1a

File tree

8 files changed

+210
-5
lines changed

8 files changed

+210
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mimick use case where users are expected to boostrap their dev ca
2+
# this is also better for testing devhsell ca bootstrapping
3+
dev-ca

default.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
}:
44
import nixpkgs {
55
inherit system;
6-
overlays = [ (import ./overlay.nix) ];
6+
overlays = [
7+
(import ./overlay.nix)
8+
(import ./extensions/overlay.nix)
9+
];
710
}

devshell.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,33 @@ help = "github utility"
5454
name = "hub"
5555
package = "gitAndTools.hub"
5656
category = "utilites"
57+
58+
59+
# ============================================================================
60+
# Example of custom extensions NOT part of devshell, see also:
61+
# ============================================================================
62+
# ./default.nix
63+
# ./shell.nix
64+
# ./extensions/*
65+
# ============================================================================
66+
67+
[extensions]
68+
# This setting helps to add a project's shared *development* root CA
69+
# to host's local trust stores by instrumenting the mkcert third party tool.
70+
# Defining this section also adds `mkcert` to the available packages.
71+
# Set to the path where mkcert-generated CAROOT files are expected to exist
72+
#
73+
# NOTES:
74+
# - be careful to only put *development* certificates under version control
75+
# - create those files with the devshell generated *-install-CA command
76+
# - optionally put this path under .gitignore, if you want users to
77+
# generate certificates themselves on first clone (using *-install-CA)
78+
dev-ca-path = "./dev-ca"
79+
80+
# These settings help to manage local DNS overrides via
81+
# instrumentation of the hostcl third party tool.
82+
# Defining this section also adds `hostctl` to the available packages.
83+
[extensions.static-dns]
84+
"test.domain.local" = "172.0.0.1"
85+
"shared.domain.link-local" = "169.254.0.5"
86+

extensions/hostctl/default.nix

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{ buildGoModule, fetchFromGitHub, lib, installShellFiles }:
2+
3+
buildGoModule rec {
4+
pname = "hostctl";
5+
version = "1.0.14";
6+
7+
src = fetchFromGitHub {
8+
owner = "guumaster";
9+
repo = pname;
10+
rev = "v${version}";
11+
sha256 = "02bjii97l4fy43v2rb93m9b0ad8y6mjvbvp4sz6a5n0w9dm1z1q9";
12+
};
13+
14+
vendorSha256 = "1lqk3cda0frqp2vwkqa4b3xkdw814wgkbr7g9r2mwxn85fpdcq5c";
15+
16+
doCheck = false;
17+
buildFlagsArray = [ "-ldflags=-s -w -X github.com/guumaster/hostctl/cmd/hostctl/actions.version=${version}" ];
18+
19+
nativeBuildInputs = [ installShellFiles ];
20+
postInstall = ''
21+
$out/bin/hostctl completion bash > hostctl.bash
22+
$out/bin/hostctl completion zsh > hostctl.zsh
23+
installShellCompletion hostctl.{bash,zsh}
24+
# replace above by following once merged https://github.com/NixOS/nixpkgs/pull/83630
25+
# installShellCompletion --cmd hostctl \
26+
# --bash <($out/bin/hostctl completion bash) \
27+
# --zsh <($out/bin/hostctl completion zsh)
28+
'';
29+
30+
meta = with lib; {
31+
description = "Your dev tool to manage /etc/hosts like a pro!";
32+
longDescription = ''
33+
This tool gives you more control over the use of your hosts file.
34+
You can have multiple profiles and switch them on/off as you need.
35+
'';
36+
homepage = "https://guumaster.github.io/hostctl/";
37+
license = licenses.mit;
38+
maintainers = with maintainers; [ blaggacao ];
39+
};
40+
}

extensions/options.nix

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{ lib, pkgs, config, ... }:
2+
with lib;
3+
let
4+
inherit (config)
5+
name
6+
;
7+
inherit (config.extensions)
8+
static-dns
9+
dev-ca-path
10+
;
11+
12+
installProjectCA = {
13+
name = "ca-install";
14+
help = "install dev CA";
15+
category = "host state";
16+
package = pkgs.mkcert;
17+
command = ''
18+
echo "$(tput bold)Installing the ${name}'s dev CA into local trust stores via mkcert command ...$(tput sgr0)"
19+
export CAROOT=${dev-ca-path}
20+
${pkgs.mkcert}/bin/mkcert -install
21+
'';
22+
};
23+
uninstallProjectCA = {
24+
name = "ca-uninstall";
25+
help = "uninstall dev CA";
26+
category = "host state";
27+
package = pkgs.mkcert;
28+
command = ''
29+
echo "$(tput bold)Purging the ${name}'s dev CA from local trust stores via mkcert command ...$(tput sgr0)"
30+
export CAROOT=${dev-ca-path}
31+
${pkgs.mkcert}/bin/mkcert -uninstall
32+
'';
33+
};
34+
35+
etcHosts =
36+
pkgs.writeText "${name}-etchosts"
37+
(
38+
lib.concatStringsSep "\n"
39+
(lib.mapAttrsToList (name: value: value + " " + name) static-dns)
40+
);
41+
# since this temporarily modifies /etc/hosts, use of sudo can't be avoided
42+
fqdnsActivate = {
43+
name = "dns-activate";
44+
category = "host state";
45+
help = "activate pre-configured static dns";
46+
package = pkgs.hostctl;
47+
command = ''
48+
echo "$(tput bold)Installing ${name}'s static local DNS resolution via hostctl command ...$(tput sgr0)"
49+
sudo ${pkgs.hostctl}/bin/hostctl add ${name} --from ${etcHosts}
50+
'';
51+
};
52+
fqdnsDeactivate = {
53+
name = "dns-deactivate";
54+
category = "host state";
55+
help = "deactivate pre-configured static dns";
56+
package = pkgs.hostctl;
57+
command = ''
58+
echo "$(tput bold)Purging ${name}'s static local DNS resolution via hostctl command ...$(tput sgr0)"
59+
sudo ${pkgs.hostctl}/bin/hostctl remove ${name}
60+
'';
61+
};
62+
extensionOptions = {
63+
dev-ca-path = mkOption {
64+
type = types.str;
65+
default = "";
66+
description = ''
67+
Path to a development CA.
68+
69+
Users can load/unload this dev CA easily and cleanly into their local
70+
trust stores via a wrapper around mkcert third party tool so that browsers
71+
and other tools would accept issued certificates under this CA as valid.
72+
73+
Use cases:
74+
- Ship static dev certificates under version control and make them trusted
75+
on user machines: add the rootCA under version control alongside the
76+
your dev certificates.
77+
- Provide users with easy and reliable CA bootstrapping through the mkcert
78+
command: exempt this path from version control via .gitignore and have
79+
users easily and reliably bootstrap a dev CA infrastructure on first use.
80+
'';
81+
};
82+
static-dns = mkOption {
83+
type = types.attrs;
84+
default = { };
85+
description = ''
86+
A list of static DNS entries, for which to enable instrumentation.
87+
88+
Users can enable/disable listed static DNS easily and cleanly
89+
via a wrapper around the hostctl third party tool.
90+
'';
91+
example = {
92+
"test.domain.local" = "172.0.0.1";
93+
"shared.domain.link-local" = "169.254.0.5";
94+
};
95+
};
96+
};
97+
in
98+
{
99+
options = {
100+
extensions = mkOption {
101+
type = types.submodule { options = extensionOptions; };
102+
default = [ ];
103+
description = ''
104+
Custom extensions to devshell.
105+
'';
106+
};
107+
};
108+
config = {
109+
commands =
110+
(
111+
if static-dns == null || static-dns == "" then [ ]
112+
else [ fqdnsActivate fqdnsDeactivate ]
113+
) ++
114+
(
115+
if dev-ca-path == null || dev-ca-path == "" then [ ]
116+
else [ installProjectCA uninstallProjectCA ]
117+
);
118+
};
119+
}

extensions/overlay.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
final: prev:
2+
{
3+
hostctl = prev.callPackage ./hostctl { };
4+
}

mkDevShell/options.nix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let
4848

4949
opCat = { name, value }:
5050
let
51-
opCmd = { name, help, ...}:
51+
opCmd = { name, help, ... }:
5252
let
5353
len = maxCommandLength - (builtins.stringLength name);
5454
in
@@ -57,7 +57,7 @@ let
5757
else
5858
"${pad name len} - ${help}";
5959
in
60-
"\n[${name}]\n" + builtins.concatStringsSep "\n" (map opCmd value);
60+
"\n[${name}]\n" + builtins.concatStringsSep "\n" (map opCmd value);
6161
in
6262
builtins.concatStringsSep "\n" (map opCat commandByCategoriesSorted)
6363
;
@@ -226,7 +226,8 @@ in
226226
];
227227

228228
packages =
229-
builtins.filter (x: x != null)
229+
builtins.filter
230+
(x: x != null)
230231
(map (x: x.package) config.commands);
231232
};
232233
}

shell.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/usr/bin/env nix-build
22
# Used to test the shell
33
{ pkgs ? import ./. { } }:
4-
pkgs.mkDevShell.fromTOML ./devshell.toml
4+
pkgs.mkDevShell {
5+
imports = [
6+
(pkgs.mkDevShell.importTOML ./devshell.toml)
7+
./extensions/options.nix
8+
];
9+
}

0 commit comments

Comments
 (0)