-
Notifications
You must be signed in to change notification settings - Fork 3
/
flake.nix
157 lines (136 loc) · 4.81 KB
/
flake.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{
description = "Stargazer server configuration";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable-small";
flake-utils.url = "github:numtide/flake-utils";
deploy-rs.url = "github:serokell/deploy-rs";
deploy-rs.inputs.nixpkgs.follows = "nixpkgs";
# Services
dust.url = "git+https://git.sr.ht/~mkaito/dust";
dust.inputs.nixpkgs.follows = "nixpkgs";
snm = {
url = "gitlab:simple-nixos-mailserver/nixos-mailserver";
flake = false;
};
minecraft-servers.url = "github:mkaito/nixos-modded-minecraft-servers";
vscode-server.url = "github:msteen/nixos-vscode-server";
};
outputs = {
self,
nixpkgs,
flake-utils,
deploy-rs,
...
} @ inputs: let
inherit (nixpkgs.lib) foldl' recursiveUpdate nixosSystem mapAttrs;
# We only evaluate server configs in the context of the system architecture they are deployed to
system = "x86_64-linux";
# Generate a system config from a config module file
mkSystem = module:
nixosSystem {
specialArgs = {
inherit inputs system;
sshKeys = import ./lib/ssh/users.nix;
};
inherit system;
modules = [module ./lib/ssh/hostkeys.nix];
};
# Abstract setting up pkgs with our overlay.
# Usage:
# (forSystems ["x86_64-linux" "aarch64-darwin"] (pkgs: {
# packages = { inherit (pkgs) hello; };
# }))
forSystems = systems: attrs':
flake-utils.lib.eachSystem systems (system: let
overlay = import ./pkgs inputs;
pkgs_ = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
};
};
pkgs = pkgs_.extend overlay;
in
attrs' pkgs);
forDefaultSystems = forSystems flake-utils.lib.defaultSystems;
in
foldl' recursiveUpdate {} [
{
nixosConfigurations.stargazer = mkSystem ./stargazer/hetzner.nix;
# Deployment expressions
deploy.nodes.stargazer = {
hostname = "stargazer.mkaito.net";
profiles = {
system = rec {
sshUser = "root";
user = sshUser;
path =
deploy-rs.lib.${system}.activate.nixos
self.nixosConfigurations.stargazer;
};
};
};
terraform.dns = let
inherit (nixpkgs.legacyPackages.${system}) writeText;
inherit (builtins) toJSON;
inherit (nixpkgs.lib) filterAttrs mapAttrs' nameValuePair flip;
server = self.nixosConfigurations.stargazer.config;
instances = server.services.modded-minecraft-servers.instances;
enabledInstances = filterAttrs (_: i: i.enable) instances;
a_records =
flip mapAttrs' enabledInstances
(n: v:
nameValuePair "${n}_a" {
zone_id = ''''${data.aws_route53_zone.mkaito_net.zone_id}'';
name = ''${n}.mc.''${data.aws_route53_zone.mkaito_net.name}'';
type = "A";
ttl = "60";
records = [''''${data.dns_a_record_set.stargazer.addrs[0]}''];
});
aaaa_records =
flip mapAttrs' enabledInstances
(n: v:
nameValuePair "${n}_aaaa" {
zone_id = ''''${data.aws_route53_zone.mkaito_net.zone_id}'';
name = ''${n}.mc.''${data.aws_route53_zone.mkaito_net.name}'';
type = "AAAA";
ttl = "60";
records = [''''${data.dns_aaaa_record_set.stargazer.addrs[0]}''];
});
srv_records =
flip mapAttrs' enabledInstances
(n: v:
nameValuePair "${n}_srv" {
zone_id = ''''${data.aws_route53_zone.mkaito_net.zone_id}'';
name = ''_minecraft._tcp.${n}.mc.''${data.aws_route53_zone.mkaito_net.name}'';
type = "SRV";
ttl = "60";
records = [''0 5 ${toString v.serverConfig.server-port} ${n}.mc.''${data.aws_route53_zone.mkaito_net.name}''];
});
in
writeText "minecraft.tf.json" (toJSON {
resource = {
aws_route53_record = a_records // aaaa_records // srv_records;
};
});
# Verify schema of .#deploy
checks = mapAttrs (_: lib: lib.deployChecks self.deploy) deploy-rs.lib;
}
(forDefaultSystems (pkgs:
with pkgs; {
devShells.default = mkShell {
buildInputs = [
# NixOS deployment tool
deploy-rs.defaultPackage.${system}
# Cloud resources
(terraform.withPlugins (p: with p; [aws dns]))
];
};
}))
(forSystems ["x86_64-linux"] (pkgs: {
packages = {
inherit (pkgs) factorio-headless;
};
}))
];
}