summary refs log tree commit diff
path: root/modules/foundation/tailnet.nix
blob: 404c5ad7a042bb2266b7aee1ce9447b964752214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #0000ff }
.highlight .c { color: #0F0 } /* Comment */
.highlight .err { color: #DDD } /* Error */
.highlight .esc { color: #DDD } /* Escape */
.highlight .g { color: #DDD } /* Generic */
.highlight .k { color: #F00 } /* Keyword */
.highlight .l { color: #DDD } /* Literal */
.highlight .n { color: #DDD } /* Name */
.highlight .o { color: #DDD } /* Operator */
.highlight .x { color: #DDD } /* Other */
.highlight .p { color: #DDD } /* Punctuation */
.highlight .ch { color: #0F0 } /* Comment.Hashbang */
.highlight .cm { color: #0F0 } /* Comment.Multiline */
.highlight .cp { color: #E5E5E5 } /* Comment.Preproc */
.highlight .cpf { color: #0F0 } /* Comment.PreprocFile */
.highlight .c1 { color: #0F0 } /* Comment.Single */
.highlight .cs { color: #0F0 } /* Comment.Special */
.highlight .gd { color: #DDD } /* Generic.Deleted */
.highlight .ge { color: #DDD } /* Generic.Emph */
.highlight .ges { color: #DDD } /* Generic.EmphStrong */
.highlight .gr { color: #DDD } /* Generic.Error */
.highlight .gh { color: #DDD } /* Generic.Heading */
.highlight .gi { color: #DDD } /* Generic.Inserted */
.highlight .go { color: #DDD } /* Generic.Output */
.highlight .gp { color: #DDD } /* Generic.Prompt */
.highlight .gs { color: #DDD } /* Generic.Strong */
.highlight .gu { color: #DDD } /* Generic.Subheading */
.highlight .gt { color: #DDD } /* Generic.Traceback */
.highlight .kc { color: #F00 } /* Keyword.Constant */
.highlight .kd { color: #F00 } /* Keyword.Declaration */
.highlight .kn { color: #F00 } /* Keyword.Namespace */
.highlight .kp { color: #F00 } /* Keyword.Pseudo */
.highlight .kr { color: #F00 } /* Keyword.Reserved */
.highlight .kt { color: #EE82EE } /* Keyword.Type */
.highlight .ld { color: #DDD } /* Literal.Date */
.highlight .m { color: #F0F } /* Literal.Number */
.highlight .s { color: #87CEEB } /* Literal.String */
.highlight .na { color: #DDD } /* Name.Attribute */
.highlight .nb { color: #DDD } /* Name.Builtin */
.highlight .nc { color: #DDD } /* Name.Class */
.highlight .no { color: #7FFFD4 } /* Name.Constant */
.highlight .nd { color: #DDD } /* Name.Decorator */
.highlight .ni { color: #DDD } /* Name.Entity */
.highlight .ne { color: #DDD } /* Name.Exception */
.highlight .nf { color: #FF0 } /* Name.Function */
.highlight .nl { color: #DDD } /* Name.Label */
.highlight .nn { color: #DDD } /* Name.Namespace */
.highlight .nx { color: #DDD } /* Name.Other */
.highlight .py { color: #DDD } /* Name.Property */
.highlight .nt { color: #DDD } /* Name.Tag */
.highlight .nv { color: #EEDD82 } /* Name.Variable */
.highlight .ow { color: #F00 } /* Operator.Word */
.highlight .pm { color: #DDD } /* Punctuation.Marker */
.highlight .w { color: #DDD } /* Text.Whitespace */
.highlight .mb { color: #F0F } /* Literal.Number.Bin */
.highlight .mf { color: #F0F } /* Literal.Number.Float */
.highlight .mh { color: #F0F } /* Literal.Number.Hex */
.highlight .mi { color: #F0F } /* Literal.Number.Integer */
.highlight .mo { color: #F0F } /* Literal.Number.Oct */
.highlight .sa { color: #87CEEB } /* Literal.String.Affix */
.highlight .sb { color: #87CEEB } /* Literal.String.Backtick */
.highlight .sc { color: #87CEEB } /* Literal.String.Char */
.highlight .dl { color: #87CEEB } /* Literal.String.Delimiter */
.highlight .sd { color: #87CEEB } /* Literal.String.Doc */
.highlight .s2 { color: #87CEEB } /* Literal.String.Double */
.highlight .se { color: #87CEEB } /* Literal.String.Escape */
.highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */
.highlight .si { color: #87CEEB } /* Literal.String.Interpol */
.highlight .sx { color: #87CEEB } /* Literal.String.Other */
.highlight .sr { color: #87CEEB } /* Literal.String.Regex */
.highlight .s1 { color: #87CEEB } /* Literal.String.Single */
.highlight .ss { color: #87CEEB } /* Literal.String.Symbol */
.highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */
.highlight .fm { color: #FF0 } /* Name.Function.Magic */
.highlight .vc { color: #EEDD82 } /* Name.Variable.Class */
.highlight .vg { color: #EEDD82 } /* Name.Variable.Global */
.highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */
.highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */
.highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
diff --git a/pdsadmin.sh b/pdsadmin.sh
index 913d2b4..b09c20c 100644
--- a/pdsadmin.sh
+++ b/pdsadmin.sh
@@ -15,16 +15,11 @@ if [[ "${EUID}" -ne 0 ]]; then
   exit 1
 fi
 
-# Download the script, if it exists.
-SCRIPT_URL="${PDSADMIN_BASE_URL}/${COMMAND}.sh"
-SCRIPT_FILE="$(mktemp /tmp/pdsadmin.${COMMAND}.XXXXXX)"
+SCRIPT_FILE="NIXPKGS_PDSADMIN_ROOT/lib/pds/${COMMAND}.sh"
 
-if ! curl --fail --silent --show-error --location --output "${SCRIPT_FILE}" "${SCRIPT_URL}"; then
+if ! [ -f "${SCRIPT_FILE}" ]; then
   echo "ERROR: ${COMMAND} not found"
   exit 2
 fi
 
-chmod +x "${SCRIPT_FILE}"
-if "${SCRIPT_FILE}" "$@"; then
-  rm --force "${SCRIPT_FILE}"
-fi
+"${SCRIPT_FILE}" "$@"
/* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
{
  config,
  pkgs,
  lib,
  ...
}:

let
  inherit (lib) mkOption types;
  inherit (pkgs) tailscale;

  cfg = config.foundation;

  tailnet-wait-for-ip = pkgs.writeShellScriptBin "tailnet-wait-for-ip.sh" ''
    echo "waiting for tailscale to acquire tailnet ip address..."

    while ! ${lib.getExe tailscale} ip -1; do
      echo "tailnet ip not yet available... sleeping for 1 second."
      sleep 1
    done

    ip=$(${lib.getExe tailscale} ip -1)
    echo "acquired tailnet address! ip: $ip"
    exit 0
  '';
in
{
  options.foundation = {
    tailnetServices = mkOption {
      type = with types; listOf str;

      default = [ ];
      example = [ "nginx" ];

      description = ''
        services that depend on the tailnet.
        will be launched only after tailscaled.service
        is fully up and online.
      '';
    };
  };

  config =
    let
      tailnetWaitOnlineService = {
        enable = true;

        after = [
          "tailscaled.service"
          "network.target"
        ];
        # kill service if tailscaled dies.
        bindsTo = [ "tailscaled.service" ];

        serviceConfig = {
          # unit is marked as active after script exits.
          Type = "oneshot";
          RemainAfterExit = true;
          # consider connection failed after 3 minutes.
          TimeoutStartSec = "3m";

          ExecStart = "${tailnet-wait-for-ip}/bin/tailnet-wait-for-ip.sh";
        };

        description = ''
          wait for tailscale device to be online and ready
        '';
      };

      tailnetOnlineTarget = {
        enable = true;

        after = [
          "tailnet-wait-online.service"
          "tailscaled.service"
        ];
        requires = [ "tailnet-wait-online.service" ];
        bindsTo = [ "tailscaled.service" ];

        description = "tailnet is online";
      };

      tailnetDependantUnit = {
        after = [ "tailnet-online.target" ];
        requires = [ "tailnet-online.target" ];
      };

      tailnetDependantServices = lib.genAttrs cfg.tailnetServices (x: tailnetDependantUnit);
    in
    {
      systemd = {
        services = {
          "tailnet-wait-online" = tailnetWaitOnlineService;
        } // tailnetDependantServices;

        targets = {
          "tailnet-online" = tailnetOnlineTarget;
        };
      };
    };
}