From c8676b05827bc4a764c355397c7f0622f5676798 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 29 Jul 2021 14:44:17 -0300 Subject: WIP reactivate nixvps with Terraform and nixos-rebuild on Vultr --- .env | 6 + .envrc | 26 --- .gitattributes | 1 + .gitignore | 4 +- TODOs.md | 8 + opt/secrets/mail-user-password-hash.txt | 1 - scripts/deploy | 30 +++- servers/active/nixvps/.terraform.lock.hcl | Bin 0 -> 1127 bytes servers/active/nixvps/configuration.nix | 229 +++++++++++++++++++++++++ servers/active/nixvps/infrastructure.tf | 46 ++--- servers/active/nixvps/opt/secrets | 1 + servers/active/nixvps/terraform.tfstate | Bin 0 -> 178 bytes servers/active/nixvps/terraform.tfstate.backup | Bin 0 -> 9255 bytes servers/active/nixvps/tf-env.sh | 7 + servers/active/nixvps/tld.txt | 2 +- 15 files changed, 296 insertions(+), 65 deletions(-) create mode 100644 .env delete mode 100644 .envrc delete mode 120000 opt/secrets/mail-user-password-hash.txt create mode 100644 servers/active/nixvps/.terraform.lock.hcl create mode 100644 servers/active/nixvps/configuration.nix create mode 120000 servers/active/nixvps/opt/secrets create mode 100644 servers/active/nixvps/terraform.tfstate create mode 100644 servers/active/nixvps/terraform.tfstate.backup create mode 100644 servers/active/nixvps/tf-env.sh diff --git a/.env b/.env new file mode 100644 index 0000000..213d791 --- /dev/null +++ b/.env @@ -0,0 +1,6 @@ +#!/bin/sh + +TF_VAR_vultr_api_key="$(cat ./secrets/terraform/vultr-api-key.txt ||:)" +if [ -n "$TF_VAR_vultr_api_key" ]; then + export TF_VAR_vultr_api_key +fi diff --git a/.envrc b/.envrc deleted file mode 100644 index 7e350d1..0000000 --- a/.envrc +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh -eu - -# -# shared -# -VPS_HOSTNAME="$(cat servers/vps/hostname.txt)" -VPS_TLD="$(cat servers/vps/tld.txt)" -export TLD - -# -# Terraform -# -export TF_VAR_vps_tld="$VPS_TLD" -export TF_VAR_vps_hostname="$VPS_HOSTNAME" -export TF_VAR_vps_storage_name="$VPS_HOSTNAME-storage" - -TF_VAR_vultr_api_key="$(cat ./secrets/terraform/vultr-api-key.txt ||:)" -if [ -n "$TF_VAR_vultr_api_key" ]; then - export TF_VAR_vultr_api_key -fi - -TF_VAR_vps_dkim_public_key="$(head -n -1 < ./secrets/vps/mail/dkim/tld.pub | tail -n +2 | tr -d '\n')" -export TF_VAR_vps_dkim_public_key - -# Selector: date when it was created -export TF_VAR_vps_dkim_selector='20201126' diff --git a/.gitattributes b/.gitattributes index 4239af9..a500efa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ secrets/**/* filter=git-crypt diff=git-crypt +servers/**/*terraform* filter=git-crypt diff=git-crypt diff --git a/.gitignore b/.gitignore index 3998dad..e688346 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ # Terraform -/.terraform/ +.terraform/ # HTML /tasks-and-bugs.html /tasks-and-bugs.html~ /tasks-and-bugs.org -/public/ \ No newline at end of file +/public/ diff --git a/TODOs.md b/TODOs.md index e4bd036..98bd7a9 100644 --- a/TODOs.md +++ b/TODOs.md @@ -273,3 +273,11 @@ client to talk with Telegram and WhatsApp chats. https://news.ycombinator.com/item?id=26005038 https://news.ycombinator.com/item?id=27450364 + +TURN STUN server + +parameterize SSH port + +public key is, at the same time, being generated by mailserver and via openssl + +Make VPS run on home server? diff --git a/opt/secrets/mail-user-password-hash.txt b/opt/secrets/mail-user-password-hash.txt deleted file mode 120000 index c739151..0000000 --- a/opt/secrets/mail-user-password-hash.txt +++ /dev/null @@ -1 +0,0 @@ -../../secrets/nixvps/mail-user-password-hash.txt \ No newline at end of file diff --git a/scripts/deploy b/scripts/deploy index 1b37888..d4a0128 100755 --- a/scripts/deploy +++ b/scripts/deploy @@ -1,9 +1,20 @@ #!/bin/sh set -eu +usage() { + cat <&2 + printf 'Missing HOSTNAME\n' >&2 + usage >&2 exit 2 fi @@ -15,17 +26,22 @@ fi DIR="$(dirname "$FILE")" -terraform apply "$DIR" +cd "$DIR" +. ./tf-env.sh +terraform init +terraform apply +cd - > /dev/null TLD="$(cat "$DIR"/tld.txt)" DIRS='/opt /srv' # shellcheck disable=2029 ssh "$TLD" "\ -sudo mkdir -p $DIRS && \ -sudo chown $USER:users -R $DIRS && \ -chmod -R 755 $DIRS" + sudo mkdir -p $DIRS && \ + sudo chown $USER:users -R $DIRS && \ + chmod -R 755 $DIRS +" -rsync -avzPL opt "$TLD":/ +rsync -avzPL opt "$DIR/opt" "$TLD":/ if [ -f "$DIR"/machines.scm ]; then guix deploy "$DIR"/machines.scm @@ -33,6 +49,6 @@ elif [ -f "$DIR"/configuration.nix ]; then scp "$DIR"/configuration.nix "$TLD":/etc/nixos/ ssh "$TLD" sudo nixos-rebuild switch else - printf 'Uknown OS type of "%s"\n' "$NAME" >&2 + printf 'Uknown deploy type for "%s"\n' "$NAME" >&2 exit 2 fi diff --git a/servers/active/nixvps/.terraform.lock.hcl b/servers/active/nixvps/.terraform.lock.hcl new file mode 100644 index 0000000..62406b0 Binary files /dev/null and b/servers/active/nixvps/.terraform.lock.hcl differ diff --git a/servers/active/nixvps/configuration.nix b/servers/active/nixvps/configuration.nix new file mode 100644 index 0000000..4d793db --- /dev/null +++ b/servers/active/nixvps/configuration.nix @@ -0,0 +1,229 @@ +{ config, pkgs, ... }: + +let + envsubstConfiguration = + pkgs.callPackage /opt/secrets/envsubst-configuration.nix { }; + config = rec { + TLD = envsubstConfiguration.TLD; + cgitPort = "81"; + openSSHPort = 23841; + }; +in { + imports = [ + ./hardware-configuration.nix + (builtins.fetchTarball { + url = "https://gitlab.com/simple-nixos-mailserver/nixos-mailserver/-/archive/master/nixos-mailserver-master.tar.gz"; + }) + ]; + + boot.loader.grub = { + enable = true; + version = 2; + device = "/dev/vda"; + }; + + networking = { + interfaces.ens3.useDHCP = true; + }; + + nix = { + gc = { + automatic = true; + options = "--delete-older-than 7d"; + }; + # min-free 1G + extraOptions = '' + min-free = ${toString (1024 * 1024 * 1024)} + ''; + }; + + environment = { + systemPackages = let + c99 = pkgs.tinycc.overrideAttrs (oldAttrs: { + postInstall = '' + ln -s $out/bin/tcc $out/bin/c99 + ''; + }); + in with pkgs; [ vim git gitAndTools.git-annex gnumake gnum4 c99 bpytop ]; + shellAliases = { l = "ls -lahF"; }; + }; + + networking.firewall.allowedTCPPorts = [ + # SSH: OpenSSH + config.openSSHPort + + # HTTP and HTPPS: NGINX + 80 + 443 + + # Git daemon + 9418 + ]; + + security = { + acme = { + acceptTerms = true; + email = "eu@euandre.org"; + }; + sudo.enable = false; + doas = { + enable = true; + extraConfig = '' + permit nopass setenv { NIX_PATH } :wheel + ''; + }; + }; + + services = { + openssh = { + enable = true; + permitRootLogin = "no"; + passwordAuthentication = false; + ports = [ config.openSSHPort ]; + }; + + nginx = { + enable = true; + recommendedGzipSettings = true; + recommendedOptimisation = true; + recommendedProxySettings = true; + recommendedTlsSettings = true; + virtualHosts = { + "${config.TLD}" = { + forceSSL = true; + enableACME = true; + root = "/srv/http/"; + extraConfig = '' + # Allow