-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws.nix
109 lines (89 loc) · 2.91 KB
/
aws.nix
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
let
awsKeyId = "AKIAI6QCDKDFAZFL5D5A"; # for [email protected]
region = "us-east-2";
pkgs = import <nixpkgs> {};
in
{
network.description = "tex.rossprogram.org";
resources.ec2KeyPairs.myKeyPair = {
accessKeyId = awsKeyId;
inherit region;
};
resources.ec2SecurityGroups.openPorts = { resources, lib, ... }: {
accessKeyId = awsKeyId;
inherit region;
description = "Open ports for webserver";
rules = [
{ toPort = 22; fromPort = 22; sourceIp = "0.0.0.0/0"; } # SSH
{ toPort = 80; fromPort = 80; sourceIp = "0.0.0.0/0"; } # HTTP
{ toPort = 443; fromPort = 443; sourceIp = "0.0.0.0/0"; } # HTTPS
];
};
server = { resources, config, nodes, ... }:
let
# build the backend node app
app = pkgs.callPackage ./default.nix {
yarn2nix = pkgs.yarn2nix-moretea;
texlive = pkgs.texlive;
};
in {
# Cloud provider settings; here for AWS
deployment.targetEnv = "ec2";
deployment.ec2.accessKeyId = awsKeyId;
deployment.ec2.region = region;
deployment.ec2.instanceType = "t2.micro"; # a cheap one
deployment.ec2.ebsInitialRootDiskSize = 20; # GB
deployment.ec2.keyPair = resources.ec2KeyPairs.myKeyPair;
deployment.ec2.associatePublicIpAddress = true;
deployment.ec2.securityGroups = [ resources.ec2SecurityGroups.openPorts.name ];
environment.systemPackages = with pkgs; [
pkgs.mongodb pkgs.nginx pkgs.texlive.combined.scheme-full
app
];
services.mongodb.enable = true;
services.nginx = {
enable = true;
# Use recommended settings
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
};
services.nginx.virtualHosts."tex-backend.rossprogram.org" = {
forceSSL = true;
enableACME = true;
default = true;
root = "/var/www/tex-backend.rossprogram.org";
locations = {
"/".proxyPass = "http://localhost:${config.systemd.services.node.environment.PORT}/";
"/".proxyWebsockets = true;
};
};
security.acme.acceptTerms = true;
security.acme.certs = {
"tex-backend.rossprogram.org".email = "[email protected]";
};
systemd.services.node = {
description = "node service";
after = [ "network.target" ];
wantedBy = [ "default.target" ];
environment = {
NODE_ENV = "production";
TEXMF = "${pkgs.texlive.combined.scheme-full}/share/texmf";
PORT = toString 8000;
MONGODB_DATABASE = "tex";
MONGODB_PORT = toString 27017;
};
serviceConfig = {
ExecStart = "${app}/bin/tex.rossprogram.org";
User = "node";
Restart = "always";
};
};
# for "security" do not run the node app as root
users.extraUsers = {
node = { };
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
};
}