added script for domains

This commit is contained in:
2026-03-27 17:46:52 -05:00
parent 691a555645
commit 335c5ffa2e
5 changed files with 231 additions and 75 deletions

View File

@@ -3,7 +3,9 @@
let let
domains = config.sovran_systemsOS.domainRequirements; domains = config.sovran_systemsOS.domainRequirements;
# Build the domain prompts dynamically from registered modules # Build list of domain names for the missing-check script
domainNamesList = lib.concatMapStringsSep " " (d: d.name) domains;
domainPrompts = lib.concatMapStringsSep "\n" (d: '' domainPrompts = lib.concatMapStringsSep "\n" (d: ''
echo "" echo ""
echo -e "''${GREEN} ${d.label} ''${NC}" echo -e "''${GREEN} ${d.label} ''${NC}"
@@ -30,16 +32,38 @@ curl \"''${DDNS_URL%auto}''${DOLLAR}{IP}\""
fi fi
'') domains; '') domains;
# Build the summary list # Only prompt for domains that don't have a file yet
missingDomainPrompts = lib.concatMapStringsSep "\n" (d: ''
if [ ! -f "/var/lib/domains/${d.name}" ]; then
MISSING=true
echo ""
echo -e "''${GREEN} ${d.label} (NEW) ''${NC}"
read -p " Subdomain (e.g. ${d.example}): " DOMAIN
if [ -n "$DOMAIN" ]; then
echo "$DOMAIN" > "/var/lib/domains/${d.name}"
echo " Saved: $DOMAIN"
${lib.optionalString d.needsDDNS ''
read -p " Njal.la DDNS URL for $DOMAIN (paste full URL, or Enter to skip): " DDNS_URL
if [ -n "$DDNS_URL" ]; then
NEW_NJALLA_ENTRIES="$NEW_NJALLA_ENTRIES
curl \"''${DDNS_URL%auto}''${DOLLAR}{IP}\""
fi
''}
else
echo " Skipped."
fi
fi
'') domains;
domainSummary = lib.concatMapStringsSep "\n" (d: '' domainSummary = lib.concatMapStringsSep "\n" (d: ''
if [ -f "/var/lib/domains/${d.name}" ]; then if [ -f "/var/lib/domains/${d.name}" ]; then
echo " ${d.label}: $(cat /var/lib/domains/${d.name})" echo " ${d.label}: $(cat /var/lib/domains/${d.name})"
fi fi
'') domains; '') domains;
in
{ # ── Full setup (first boot) ─────────────────────────────────
environment.systemPackages = [ setupScript = pkgs.writeShellScriptBin "sovran-setup-domains" ''
(pkgs.writeShellScriptBin "sovran-setup-domains" ''
set -euo pipefail set -euo pipefail
GREEN='\033[0;32m' GREEN='\033[0;32m'
@@ -88,7 +112,7 @@ in
echo " Saved." echo " Saved."
fi fi
# Module domains (auto-generated from enabled modules) # All module domains
${domainPrompts} ${domainPrompts}
# Write njalla.sh # Write njalla.sh
@@ -112,6 +136,9 @@ SCRIPT
echo " DNS records updated." echo " DNS records updated."
fi fi
# Mark setup complete
touch /var/lib/domains/.setup-complete
# Summary # Summary
echo "" echo ""
echo -e "''${CYAN}''${NC}" echo -e "''${CYAN}''${NC}"
@@ -125,9 +152,117 @@ SCRIPT
echo " DDNS script: /var/lib/njalla/njalla.sh" echo " DDNS script: /var/lib/njalla/njalla.sh"
echo " DDNS cron: Every 15 minutes (already configured)" echo " DDNS cron: Every 15 minutes (already configured)"
echo "" echo ""
echo " Next step:" echo -e "''${YELLOW} Rebuilding to activate services with new domains...''${NC}"
echo " sudo nixos-rebuild switch --flake /etc/nixos#nixos"
echo "" echo ""
'') nixos-rebuild switch --flake /etc/nixos#nixos
'';
# ── Add-domain script (existing machines, new features) ─────
addDomainScript = pkgs.writeShellScriptBin "sovran-add-domains" ''
set -euo pipefail
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
DOLLAR='$'
MISSING=false
NEW_NJALLA_ENTRIES=""
echo ""
echo -e "''${CYAN}''${NC}"
echo -e "''${CYAN} Sovran_SystemsOS New Feature Domains''${NC}"
echo -e "''${CYAN}''${NC}"
echo ""
echo " Checking for newly enabled features that need domains..."
mkdir -p /var/lib/domains
mkdir -p /var/lib/njalla
${missingDomainPrompts}
if [ "$MISSING" = false ]; then
echo ""
echo -e "''${GREEN} All domains are already configured. Nothing to do.''${NC}"
echo ""
exit 0
fi
# Append new entries to njalla.sh
if [ -n "$NEW_NJALLA_ENTRIES" ]; then
echo ""
echo -e "''${GREEN} Updating DDNS script ''${NC}"
if [ -f /var/lib/njalla/njalla.sh ]; then
echo "$NEW_NJALLA_ENTRIES" >> /var/lib/njalla/njalla.sh
echo " Appended new entries to /var/lib/njalla/njalla.sh"
else
cat > /var/lib/njalla/njalla.sh <<SCRIPT
#!/usr/bin/env bash
IP=\$(dig @resolver4.opendns.com myip.opendns.com +short -4)
$NEW_NJALLA_ENTRIES
SCRIPT
chmod 700 /var/lib/njalla/njalla.sh
echo " Created /var/lib/njalla/njalla.sh"
fi
echo ""
read -p "Update Njal.la DNS records now? (y/n): " RUN_NOW
if [ "$RUN_NOW" = "y" ]; then
bash /var/lib/njalla/njalla.sh
echo " DNS records updated."
fi
fi
# Summary
echo ""
echo -e "''${CYAN}''${NC}"
echo -e "''${CYAN} New Domains Added!''${NC}"
echo -e "''${CYAN}''${NC}"
echo ""
echo " All configured domains:"
${domainSummary}
echo ""
echo -e "''${YELLOW} Rebuilding to activate services with new domains...''${NC}"
echo ""
nixos-rebuild switch --flake /etc/nixos#nixos
'';
# ── Check script used by autostart ──────────────────────────
needsSetup = pkgs.writeShellScriptBin "sovran-domains-need-setup" ''
# First boot no setup done at all
if [ ! -f /var/lib/domains/.setup-complete ]; then
exit 0
fi
# Existing machine check for missing domain files
for NAME in ${domainNamesList}; do
if [ ! -f "/var/lib/domains/$NAME" ]; then
exit 0
fi
done
# Everything is configured
exit 1
'';
in
{
environment.systemPackages = [
setupScript
addDomainScript
needsSetup
]; ];
# ── Auto-launch on login if any domains are missing ─────────
environment.etc."xdg/autostart/sovran-setup-domains.desktop".text = ''
[Desktop Entry]
Type=Application
Name=Sovran_SystemsOS Domain Setup
Comment=Configure domains for newly enabled features
Exec=${pkgs.bash}/bin/bash -c 'if ${needsSetup}/bin/sovran-domains-need-setup; then if [ ! -f /var/lib/domains/.setup-complete ]; then ${pkgs.gnome-terminal}/bin/gnome-terminal -- sudo ${setupScript}/bin/sovran-setup-domains; else ${pkgs.gnome-terminal}/bin/gnome-terminal -- sudo ${addDomainScript}/bin/sovran-add-domains; fi; fi'
Terminal=false
X-GNOME-Autostart-enabled=true
'';
} }

View File

@@ -49,6 +49,9 @@ lib.mkIf config.sovran_systemsOS.features.element-calling {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/element-calling";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
MATRIX=$(cat /var/lib/domains/matrix) MATRIX=$(cat /var/lib/domains/matrix)
@@ -100,6 +103,9 @@ EOF
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/element-calling";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
MATRIX=$(cat /var/lib/domains/matrix) MATRIX=$(cat /var/lib/domains/matrix)
@@ -150,6 +156,9 @@ EOF
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/element-calling";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
ELEMENT_CALLING=$(cat /var/lib/domains/element-calling) ELEMENT_CALLING=$(cat /var/lib/domains/element-calling)
@@ -185,6 +194,9 @@ EOF
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/element-calling";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
MATRIX=$(cat /var/lib/domains/matrix) MATRIX=$(cat /var/lib/domains/matrix)

View File

@@ -16,6 +16,9 @@ lib.mkIf (config.sovran_systemsOS.features.haven && npub != "") {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/haven";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
HAVEN=$(cat /var/lib/domains/haven) HAVEN=$(cat /var/lib/domains/haven)

View File

@@ -61,6 +61,9 @@ lib.mkIf config.sovran_systemsOS.services.synapse {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/matrix";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
set -euo pipefail set -euo pipefail

View File

@@ -11,6 +11,9 @@ lib.mkIf config.sovran_systemsOS.services.vaultwarden {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
}; };
unitConfig = {
ConditionPathExists = "/var/lib/domains/vaultwarden";
};
path = [ pkgs.coreutils ]; path = [ pkgs.coreutils ];
script = '' script = ''
VAULTWARDEN=$(cat /var/lib/domains/vaultwarden) VAULTWARDEN=$(cat /var/lib/domains/vaultwarden)