forked from betamike/betamike-infra
initial commit: nixos base image + matrix server setup
This commit is contained in:
commit
04c1d91502
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.envrc
|
||||
.terraform
|
10
base_image/configuration.nix
Normal file
10
base_image/configuration.nix
Normal file
@ -0,0 +1,10 @@
|
||||
{ config, pkgs, ... }:
|
||||
{
|
||||
environment.systemPackages = [ pkgs.jq ];
|
||||
services.openssh.enable = true;
|
||||
networking.firewall.allowedTCPPorts = [ 22 ];
|
||||
|
||||
users.users.root.openssh.authorizedKeys.keys = [
|
||||
(builtins.readFile "/home/mike/.ssh/id_mops.pub")
|
||||
];
|
||||
}
|
5
base_image/gen_base_image.sh
Executable file
5
base_image/gen_base_image.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
nixos-generate -f do -c configuration.nix
|
||||
|
||||
# TODO: automate upload of image
|
148
nixos_configs/matrix.nix
Normal file
148
nixos_configs/matrix.nix
Normal file
@ -0,0 +1,148 @@
|
||||
# adapted from https://nixos.org/manual/nixos/stable/index.html#module-services-matrix
|
||||
{ modulesPath, config, lib, pkgs, ... }:
|
||||
let
|
||||
storage-device = "/dev/disk/by-id/scsi-0DO_Volume_matrix-storage";
|
||||
storage-dir = "/opt/matrix-data";
|
||||
matrix-reg-key = (builtins.readFile ./matrix_reg_key);
|
||||
fqdn =
|
||||
let
|
||||
join = hostName: domain: hostName + lib.optionalString (domain != null) ".${domain}";
|
||||
in join config.networking.hostName config.networking.domain;
|
||||
in {
|
||||
imports = [
|
||||
"${toString modulesPath}/virtualisation/digital-ocean-image.nix"
|
||||
];
|
||||
|
||||
environment.systemPackages = [ pkgs.jq ];
|
||||
services.openssh.enable = true;
|
||||
networking.firewall.allowedTCPPorts = [ 22 80 443 ];
|
||||
|
||||
users.users.root.openssh.authorizedKeys.keys = [
|
||||
(builtins.readFile "/home/mike/.ssh/id_mops.pub")
|
||||
];
|
||||
|
||||
### app specific config
|
||||
|
||||
# mount DigitalOcean volume for use by postgres
|
||||
fileSystems."${storage-dir}" = {
|
||||
device = storage-device;
|
||||
};
|
||||
|
||||
networking = {
|
||||
hostName = "matrix";
|
||||
domain = "waffle.farm";
|
||||
};
|
||||
|
||||
services.postgresql = {
|
||||
enable = true;
|
||||
dataDir = "${storage-dir}/db";
|
||||
|
||||
initialScript = pkgs.writeText "synapse-init.sql" ''
|
||||
CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD 'synapse';
|
||||
CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
|
||||
TEMPLATE template0
|
||||
LC_COLLATE = "C"
|
||||
LC_CTYPE = "C";
|
||||
'';
|
||||
};
|
||||
|
||||
services.nginx = {
|
||||
enable = true;
|
||||
recommendedTlsSettings = true;
|
||||
recommendedOptimisation = true;
|
||||
recommendedGzipSettings = true;
|
||||
recommendedProxySettings = true;
|
||||
|
||||
virtualHosts = {
|
||||
"${config.networking.domain}" = {
|
||||
enableACME = true;
|
||||
forceSSL = true;
|
||||
|
||||
locations."= /.well-known/matrix/server".extraConfig =
|
||||
let
|
||||
# use 443 instead of the default 8448 port to unite
|
||||
# the client-server and server-server port for simplicity
|
||||
server = { "m.server" = "${fqdn}:443"; };
|
||||
in ''
|
||||
add_header Content-Type application/json;
|
||||
return 200 '${builtins.toJSON server}';
|
||||
'';
|
||||
locations."= /.well-known/matrix/client".extraConfig =
|
||||
let
|
||||
client = {
|
||||
"m.homeserver" = { "base_url" = "https://${fqdn}"; };
|
||||
"m.identity_server" = { "base_url" = "https://vector.im"; };
|
||||
};
|
||||
# ACAO required to allow element-web on any URL to request this json file
|
||||
in ''
|
||||
add_header Content-Type application/json;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
return 200 '${builtins.toJSON client}';
|
||||
'';
|
||||
locations."/".extraConfig = ''
|
||||
return 301 https://chat.waffle.farm;
|
||||
'';
|
||||
|
||||
};
|
||||
# Reverse proxy for Matrix client-server and server-server communication
|
||||
${fqdn} = {
|
||||
enableACME = true;
|
||||
forceSSL = true;
|
||||
|
||||
# Or do a redirect instead of the 404, or whatever is appropriate for you.
|
||||
# But do not put a Matrix Web client here! See the Element web section below.
|
||||
locations."/".extraConfig = ''
|
||||
return 301 https://chat.waffle.farm;
|
||||
'';
|
||||
|
||||
# forward all Matrix API calls to the synapse Matrix homeserver
|
||||
locations."/_matrix" = {
|
||||
proxyPass = "http://[::1]:8008"; # without a trailing /
|
||||
};
|
||||
};
|
||||
|
||||
"chat.${config.networking.domain}" = {
|
||||
enableACME = true;
|
||||
forceSSL = true;
|
||||
serverAliases = [
|
||||
"chat.${config.networking.domain}"
|
||||
];
|
||||
root = pkgs.element-web.override {
|
||||
conf = {
|
||||
default_server_config."m.homeserver" = {
|
||||
"base_url" = "https://${fqdn}";
|
||||
"server_name" = "${config.networking.domain}";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
services.matrix-synapse = {
|
||||
enable = true;
|
||||
server_name = config.networking.domain;
|
||||
registration_shared_secret = matrix-reg-key;
|
||||
|
||||
listeners = [
|
||||
{
|
||||
port = 8008;
|
||||
bind_address = "::1";
|
||||
type = "http";
|
||||
tls = false;
|
||||
x_forwarded = true;
|
||||
resources = [
|
||||
{
|
||||
names = [ "client" "federation" ];
|
||||
compress = false;
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
security.acme.acceptTerms = true;
|
||||
security.acme.certs = {
|
||||
"${fqdn}".email = "mike@betamike.com";
|
||||
"${config.networking.domain}".email = "mike@betamike.com";
|
||||
"chat.${config.networking.domain}".email = "mike@betamike.com";
|
||||
};
|
||||
}
|
16
nixos_configs/template.nix
Normal file
16
nixos_configs/template.nix
Normal file
@ -0,0 +1,16 @@
|
||||
{ modulesPath, config, lib, pkgs, ... }:
|
||||
{
|
||||
imports = [
|
||||
"${toString modulesPath}/virtualisation/digital-ocean-image.nix"
|
||||
];
|
||||
|
||||
environment.systemPackages = [ pkgs.jq ];
|
||||
services.openssh.enable = true;
|
||||
networking.firewall.allowedTCPPorts = [ 22 ];
|
||||
|
||||
users.users.root.openssh.authorizedKeys.keys = [
|
||||
(builtins.readFile "/home/mike/.ssh/id_mops.pub")
|
||||
];
|
||||
|
||||
# Put host specific logic here
|
||||
}
|
39
terraform/matrix.tf
Normal file
39
terraform/matrix.tf
Normal file
@ -0,0 +1,39 @@
|
||||
data "digitalocean_image" "nixos_base" {
|
||||
name = "nixos-base"
|
||||
}
|
||||
|
||||
resource "digitalocean_volume" "matrix" {
|
||||
region = "nyc3"
|
||||
name = "matrix-storage"
|
||||
size = 10
|
||||
initial_filesystem_type = "ext4"
|
||||
}
|
||||
|
||||
resource "digitalocean_droplet" "matrix-0" {
|
||||
name = "matrix-0"
|
||||
|
||||
image = data.digitalocean_image.nixos_base.id
|
||||
region = "nyc3"
|
||||
size = "s-1vcpu-1gb"
|
||||
|
||||
ssh_keys = [data.digitalocean_ssh_key.mops.id]
|
||||
}
|
||||
|
||||
resource "digitalocean_volume_attachment" "matrix" {
|
||||
droplet_id = digitalocean_droplet.matrix-0.id
|
||||
volume_id = digitalocean_volume.matrix.id
|
||||
}
|
||||
|
||||
module "deploy_nixos" {
|
||||
source = "github.com/tweag/terraform-nixos//deploy_nixos?ref=d055d2180da230e47ba9082fc53a8b7d1fadbc43"
|
||||
|
||||
nixos_config = "../nixos_configs/matrix.nix"
|
||||
|
||||
target_user = "root"
|
||||
target_host = digitalocean_droplet.matrix-0.ipv4_address
|
||||
|
||||
triggers = {
|
||||
// Also re-deploy whenever the VM is re-created
|
||||
instance_id = digitalocean_droplet.matrix-0.id
|
||||
}
|
||||
}
|
14
terraform/provider.tf
Normal file
14
terraform/provider.tf
Normal file
@ -0,0 +1,14 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
digitalocean = {
|
||||
source = "digitalocean/digitalocean"
|
||||
version = "2.1.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "digitalocean" {}
|
||||
|
||||
data "digitalocean_ssh_key" "mops" {
|
||||
name = "mops"
|
||||
}
|
Loading…
Reference in New Issue
Block a user