-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflake.nix
91 lines (82 loc) · 2.38 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
nixos-generators.url = "github:nix-community/nixos-generators";
nixos-generators.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs:
let
system = "x86_64-linux";
pkgs = import inputs.nixpkgs { inherit system; };
# Step 1. Write application
iplz-lib = pkgs.python3Packages.buildPythonPackage {
name = "iplz";
src = ./app;
propagatedBuildInputs = [ pkgs.python3Packages.falcon ];
};
iplz-server = pkgs.writeShellApplication {
name = "iplz-server";
runtimeInputs = [ (pkgs.python3.withPackages (p: [ p.uvicorn iplz-lib ])) ];
text = ''
uvicorn iplz:app "$@"
'';
};
# Step 2: Define image
base-config = {
system.stateVersion = "22.05";
networking.firewall.allowedTCPPorts = [ 80 ];
systemd.services.iplz = {
enable = true;
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
script = ''
${iplz-server}/bin/iplz-server --host 0.0.0.0 --port 80
'';
serviceConfig = {
Restart = "always";
Type = "simple";
};
};
};
iplz-vm = inputs.nixos-generators.nixosGenerate {
inherit pkgs;
format = "vm";
modules = [
base-config
{
services.getty.autologinUser = "root";
virtualisation.forwardPorts = [{ from = "host"; host.port = 8000; guest.port = 80; }];
}
];
};
# Step 3: Deploy
image-name = "iplz-${system}";
iplz-ec2-img = inputs.nixos-generators.nixosGenerate {
inherit pkgs;
format = "amazon";
modules = [
base-config
{ amazonImage.name = image-name; }
];
};
iplz-img-path = "${iplz-ec2-img}/${image-name}.vhd";
deploy-shell = pkgs.mkShell {
packages = [ pkgs.terraform ];
TF_VAR_iplz_img_path = iplz-img-path;
};
terraform = pkgs.writeShellScriptBin "terraform" ''
export TF_VAR_iplz_img_path="${iplz-img-path}"
${pkgs.terraform}/bin/terraform $@
'';
in
{
packages.${system} = {
inherit
iplz-server
iplz-vm
iplz-ec2-img
terraform;
};
devShell.${system} = deploy-shell;
};
}