Defguard VPN client: Nix package, root service, dot defguard provisioning

- pkgs/defguard-client.nix repackages the official 2.1.2 .deb (GUI, defguard-service, dg) with
  Nix's WebKitGTK 4.1, so it runs natively on SteamOS; the GUI wrapper adds desktop-file-utils
  and xdg-utils, which the app needs to register its defguard:// handler.
- `dot system` installs the root interface service (config/defguard/defguard-service.service),
  the defguard group and a resolvconf -> resolvectl shim where resolvconf is missing.
- `dot defguard` mints a single-use desktop token with DEFGUARD_API_TOKEN from the Bitwarden-backed
  secrets.env and writes ~/.local/share/net.defguard/provisioning.json, so the app configures the
  home-lab instance on its next start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUebqghNwNssAZuWTxUq1y
This commit is contained in:
Ziad ArafatandClaude Opus 5.5 committed 2026-11-06 18:34:41 -07:00
1 parent 86876cd7a4
commit 68d6fce13e
5 files changed
+184 -7

No files matched your search

+3 -1
View File
@@ -8,7 +8,7 @@ A reproducible dev environment for the Steam Deck (SteamOS), Fedora KDE, Arch an
| Language runtimes + npm/GitHub CLIs | mise (`programs.mise.globalConfig`) | `~/.local/share/mise` |
| GUI apps | Flatpak, user scope (`config/flatpaks.txt`) | `~/.local/share/flatpak` |
| Secrets (SSH keys, MCP tokens, WireGuard) | Bitwarden CLI (`dot secrets`) | `~/.config/dotfiles/secrets.env`, `~/.ssh` |
| Root bits (distro pkgs, browser policies, netbird unit, login shell) | `dot system` (sudo) | `/etc` |
| Root bits (distro pkgs, browser policies, netbird + Defguard units, login shell) | `dot system` (sudo) | `/etc` |
```mermaid
flowchart LR
@@ -64,6 +64,7 @@ Manual sign-ins that can't be automated: Edge (work account), Chrome and Firefox
| `dot switch` | apply after editing Nix files |
| `dot profile headless` / `full` / `auto` | switch layers; then `dot switch` |
| `dot secrets push` | upload this machine's secrets.env, SSH keys and WireGuard configs to Bitwarden |
| `dot defguard` | preconfigure the Defguard VPN app for the home lab (see below) |
| `dot wallpapers --push` | re-sort the wallpapers from the external drive and push them |
| `dot sounds` | build the Portal 2 sound theme from your Steam install |
| `dot doctor` | what's installed or missing |
@@ -88,6 +89,7 @@ Apps that write their own settings (Claude Code, pi, Vesktop, Konsole profiles,
- **Panel:** the Deck's layout plus four system monitors (per-core CPU bars, per-disk horizontal bars, network line graph, RAM ring with %) and a clock with Local, Arizona, Pacific, Central, Eastern, London and Beijing (scroll to cycle).
- **Activities:** *Personal* (Pokémon, 10 s slideshow) and *Work* (Ace Combat, 15 min slideshow). `daynight-hooks` points each at its `light/` or `dark/` folder as the theme flips, and also sets Konsole's default profile at login.
- **Konsole:** `Light` (Solarized Light) / `Dark` (Breeze) profiles switched by [konsole_daynight](https://github.com/RustyRaptor/konsole_daynight), packaged from source with Nix's PyGObject. `general` / `etv` profiles open the distroboxes.
- **Defguard VPN** (home lab, `vpn.godemperordoge.org`): the desktop client is repackaged from the official `.deb` in `pkgs/defguard-client.nix`, so it runs natively on SteamOS (no WebKitGTK 4.1 there). `dot system` installs its root service (`config/defguard/defguard-service.service`, group `defguard`, a `resolvconf` → `resolvectl` shim where needed). Setup tokens are single-use and expire in 24 h, so Bitwarden holds a Defguard **API token** instead: create one in Defguard (your profile → *API tokens*) once, `dot secrets set DEFGUARD_API_TOKEN`, then on each new machine run `dot defguard` from home or over the VPN. It mints a fresh token and writes `~/.local/share/net.defguard/provisioning.json`, and the app sets itself up on its next start; you only sign in with Doge Home Lab.
- **Sounds:** `dot sounds` extracts Portal 2 sounds from your local Steam install (game assets stay out of git) into `~/.local/share/sounds/portal2`. Mapping: `config/sounds/portal2.json`.
## Testing
+72 -5
View File
@@ -143,16 +143,38 @@ netbird_service() {
ok "netbird daemon running ($bin)"
}
defguard_service() {
local bin="$HOME/.nix-profile/bin/defguard-service"
[ -x "$bin" ] || { warn "defguard-service not in nix profile (full profile); skipping"; return; }
has_systemd || { warn "no systemd; skipping defguard service"; return; }
# The service hands its socket to group "defguard"; the app's user must be in it (new login needed).
getent group defguard >/dev/null || SUDO groupadd --system defguard
if ! id -nG "$USER" | tr ' ' '\n' | grep -qx defguard && [ "$(getent group defguard | cut -d: -f3)" != "$(id -g)" ]; then
SUDO usermod -aG defguard "$USER" && warn "added $USER to group defguard: log out and back in before using the VPN app"
fi
# VPN DNS goes through `resolvconf`; SteamOS only has resolvectl, which acts as resolvconf under that name.
if ! have resolvconf && have resolvectl; then
SUDO install -d -m 755 /var/lib/defguard-service/bin
SUDO ln -sf "$(command -v resolvectl)" /var/lib/defguard-service/bin/resolvconf
fi
sed "s|@DEFGUARD_SERVICE@|$bin|" "$DOTFILES_DIR/config/defguard/defguard-service.service" | SUDO tee /etc/systemd/system/defguard-service.service >/dev/null
SUDO systemctl daemon-reload
SUDO systemctl enable defguard-service.service
SUDO systemctl restart defguard-service.service # picks up a new binary after `dot switch`
ok "defguard service running ($bin)"
}
cmd_system() {
local skip_pkgs=0 netbird=0
for a in "$@"; do case "$a" in --no-pkgs) skip_pkgs=1;; --netbird) netbird=1;; esac; done
[ "$(profile)" = full ] && netbird=1
local skip_pkgs=0 netbird=0 defguard=0
for a in "$@"; do case "$a" in --no-pkgs) skip_pkgs=1;; --netbird) netbird=1;; --defguard) defguard=1;; esac; done
[ "$(profile)" = full ] && { netbird=1; defguard=1; }
step "distro layer for $(os_id) (needs sudo)"
[ $skip_pkgs = 1 ] || install_pkgs
if [ "$(profile)" = full ]; then
browser_policies
fi
[ $netbird = 1 ] && netbird_service
[ $defguard = 1 ] && defguard_service
# Prefer the distro's zsh: a login shell inside the Nix profile dies with the profile.
local zsh_path=""
for z in /usr/bin/zsh /bin/zsh; do [ -x "$z" ] && { zsh_path="$z"; break; }; done
@@ -390,6 +412,50 @@ cmd_secrets() {
esac
}
# ---------------------------------------------------------------- defguard (VPN client)
# The home-lab Defguard instance. Core (API, admin UI) answers only the home network and the VPN;
# the edge (enrollment, client API, WireGuard endpoint) is public at vpn.godemperordoge.org.
DEFGUARD_URL="${DEFGUARD_URL:-https://defguard.godemperordoge.org}"
DEFGUARD_USER="${DEFGUARD_USER:-doge}"
DEFGUARD_DATA="$HOME/.local/share/net.defguard"
defguard_configured() { # does the app already have an instance?
[ -f "$DEFGUARD_DATA/defguard.db" ] || return 1
python3 - "$DEFGUARD_DATA/defguard.db" <<'PY' 2>/dev/null
import sqlite3, sys
c = sqlite3.connect("file:%s?mode=ro" % sys.argv[1], uri=True)
sys.exit(0 if c.execute("select count(*) from instance").fetchone()[0] else 1)
PY
}
# Preconfigure the Defguard app on this machine. Enrollment tokens are single-use and expire after
# 24 h, so the vault holds an API token instead (secrets.env DEFGUARD_API_TOKEN, created in Defguard
# under your profile -> API tokens) and this mints a fresh desktop token for each machine. The app
# reads provisioning.json on its next start while it has no instance configured.
cmd_defguard() {
if defguard_configured && [ "${1:-}" != --force ]; then
ok "Defguard app already has an instance (dot defguard --force to provision anyway)"; return
fi
[ -s "$SECRETS_ENV" ] || die "no $SECRETS_ENV (dot secrets pull)"
local token; token="$(sed -n 's/^DEFGUARD_API_TOKEN=//p' "$SECRETS_ENV" | tail -1)"
[ -n "$token" ] || die "no DEFGUARD_API_TOKEN: create one in Defguard (your profile -> API tokens), then: dot secrets set DEFGUARD_API_TOKEN"
local resp
# header via a file descriptor so the token never shows up in the process list
resp="$(curl -fsS -m 20 -X POST -H @<(printf 'Authorization: Bearer %s\n' "$token") \
-H 'Content-Type: application/json' -d '{"send_enrollment_notification": false}' \
"$DEFGUARD_URL/api/v1/user/$DEFGUARD_USER/start_desktop")" \
|| die "could not get a token from $DEFGUARD_URL (it answers only the home network and the VPN; is the API token still valid?)"
mkdir -p "$DEFGUARD_DATA"
(umask 077; echo "$resp" | jq -e '{enrollment_token, enrollment_url}' >"$DEFGUARD_DATA/provisioning.json") \
|| die "unexpected answer from Defguard"
ok "Defguard app preconfigured for $(echo "$resp" | jq -r .enrollment_url) (token valid 24 h)"
if pgrep -x defguard-client >/dev/null || pgrep -f '/bin/.defguard-client-wrapped' >/dev/null; then
warn "quit the Defguard app (tray icon -> Quit) and start it again to pick it up"
else
echo " start 'Defguard' from the menu to finish (sign in with Doge Home Lab)"
fi
}
# ---------------------------------------------------------------- wallpapers
cmd_wallpapers() {
if [ -d "$WALLPAPER_SRC_WORK" ] || [ -d "$WALLPAPER_SRC_PERSONAL" ]; then
@@ -558,10 +624,11 @@ dot — portable dev environment
dot update [--inputs] pull repo, (bump flake inputs), switch, update flatpaks/mise/claude
dot switch [profile] apply the home-manager config
dot profile [full|headless|auto]
dot system [--netbird] sudo layer: distro packages, browser PWA policies, netbird, login shell
dot system [--netbird] [--defguard] sudo layer: distro packages, browser PWA policies, netbird, Defguard VPN service, login shell
dot nix install Nix if missing
dot apps Flatpaks, mise tools, Claude Code
dot secrets status|push|pull|add|set|render Bitwarden-backed tokens, keys, files, WireGuard
dot defguard [--force] preconfigure the Defguard VPN app (one-time token minted with DEFGUARD_API_TOKEN)
dot wallpapers [--push] sort wallpapers into light/dark (or clone them)
dot sounds [build|test|notify] Portal 2 sound theme: build it, play every sound, or fire a test notification
dot boxes create distroboxes from distrobox.ini
@@ -572,7 +639,7 @@ EOF
main() {
local cmd="${1:-help}"; shift || true
case "$cmd" in
bootstrap|update|switch|profile|system|nix|apps|secrets|wallpapers|sounds|boxes|doctor) "cmd_$cmd" "$@" ;;
bootstrap|update|switch|profile|system|nix|apps|secrets|defguard|wallpapers|sounds|boxes|doctor) "cmd_$cmd" "$@" ;;
help|-h|--help) usage ;;
*) usage; exit 1 ;;
esac
+26
View File
@@ -0,0 +1,26 @@
# Defguard VPN client interface service (root). Installed by `dot system` (defguard_service);
# @DEFGUARD_SERVICE@ = ~/.nix-profile/bin/defguard-service (pkgs/defguard-client.nix).
# The app talks to it over /var/run/defguard.socket (group defguard). PATH puts a resolvconf ->
# resolvectl shim first on systems without resolvconf (SteamOS), which the service uses for VPN DNS.
[Unit]
Description=Defguard interface daemon service
Documentation=https://docs.defguard.net
Wants=network-online.target
After=network-online.target
[Service]
Group=defguard
Environment=PATH=/var/lib/defguard-service/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=@DEFGUARD_SERVICE@
KillMode=process
KillSignal=SIGINT
LimitNOFILE=65536
LimitNPROC=infinity
Restart=on-failure
RestartSec=2
TasksMax=infinity
OOMScoreAdjust=-1000
[Install]
WantedBy=multi-user.target
+5 -1
View File
@@ -7,7 +7,11 @@ in
lib.mkIf config.dotfiles.gui {
# GUI apps are Flatpaks (config/flatpaks.txt, installed by `dot apps`);
# these are the few that work better outside the sandbox.
home.packages = [ pkgs.xpipe ];
# Defguard VPN client: its root service comes from `dot system`, its setup from `dot defguard`.
home.packages = [
pkgs.xpipe
(pkgs.callPackage ../../pkgs/defguard-client.nix { })
];
# Let the host's Plasma session see Nix-installed .desktop files and icons.
xdg.configFile."plasma-workspace/env/10-nix.sh".text = ''
+78
View File
@@ -0,0 +1,78 @@
# Defguard desktop VPN client (GUI + root interface service + `dg` CLI), repackaged from the
# official .deb so it runs on SteamOS, which lacks WebKitGTK 4.1. The service is installed as a
# systemd unit by `dot system` (config/defguard/defguard-service.service); `dot defguard`
# preconfigures the app for the home-lab instance. Keep the version on the server's major.minor
# (newer clients also work with older servers).
{
lib,
stdenv,
fetchurl,
dpkg,
autoPatchelfHook,
wrapGAppsHook3,
webkitgtk_4_1,
gtk3,
glib,
glib-networking,
libsoup_3,
libayatana-appindicator,
openssl,
desktop-file-utils,
xdg-utils,
}:
stdenv.mkDerivation rec {
pname = "defguard-client";
version = "2.1.2";
src = fetchurl {
url = "https://github.com/DefGuard/client/releases/download/v${version}/defguard-client_${version}_amd64.deb";
hash = "sha256-O3EdA8uwm5YoQdTPcDs/MouuEOC/yZr7w8W6V9fBsDQ=";
};
nativeBuildInputs = [
dpkg
autoPatchelfHook
wrapGAppsHook3
];
buildInputs = [
webkitgtk_4_1
gtk3
glib
glib-networking
libsoup_3
libayatana-appindicator
openssl
stdenv.cc.cc.lib
];
# the tray library is loaded with dlopen, so autoPatchelf can't see it
runtimeDependencies = [ libayatana-appindicator ];
unpackPhase = "dpkg-deb -x $src .";
dontWrapGApps = true; # wrap only the GUI below, not the service
installPhase = ''
runHook preInstall
install -Dm755 usr/bin/defguard-client $out/bin/defguard-client
install -Dm755 usr/bin/dg $out/bin/dg
install -Dm755 usr/sbin/defguard-service $out/bin/defguard-service
mkdir -p $out/lib $out/share
cp -r usr/lib/defguard-client $out/lib/
cp -r usr/share/applications usr/share/icons $out/share/
runHook postInstall
'';
# On start the app registers its defguard:// handler with update-desktop-database and xdg-mime.
postFixup = ''
wrapProgram $out/bin/defguard-client "''${gappsWrapperArgs[@]}" \
--prefix PATH : ${lib.makeBinPath [ desktop-file-utils xdg-utils ]}
'';
meta = {
description = "Defguard WireGuard VPN desktop client";
homepage = "https://github.com/DefGuard/client";
license = lib.licenses.agpl3Only; # client is AGPL-3.0; enterprise parts under their own licence
platforms = [ "x86_64-linux" ];
mainProgram = "defguard-client";
};
}