-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
131 lines (110 loc) · 3.82 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
{
description = "api_guard flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
# Used for shell.nix
flake-compat = {
url = github:edolstra/flake-compat;
flake = false;
};
};
outputs = {
self,
nixpkgs,
flake-utils,
...
} @ inputs: let
in
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {inherit system; };
pythonEnv = pkgs.python3.withPackages (ps: [
ps.flask
ps.python-dotenv
]);
in rec {
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
bat
gnused
pythonEnv
curl
];
buildInputs = with pkgs; [
# we need a version of bash capable of being interactive
# as opposed to a bash just used for building this flake
# in non-interactive mode
bashInteractive
];
shellHook = ''
# once we set SHELL to point to the interactive bash, neovim will
# launch the correct $SHELL in its :terminal
export SHELL=${pkgs.bashInteractive}/bin/bash
export FLASK_APP=api_guard:app
'';
};
# For compatibility with older versions of the `nix` binary
devShell = self.devShells.${system}.default;
# packages
packages.api_guard = pkgs.stdenv.mkDerivation rec {
name = "api_guard";
buildInputs = with pkgs; [
pythonEnv
busybox # grep, xargs
bashInteractive
];
script = pkgs.writeShellScript "api_guard-run" ''
#!/usr/bin/env bash
# set a XDG_CONFIG_HOME if in docker
export XDG_CONFIG_HOME=''${XDG_CONFIG_HOME:-/tmp}
# Define the path to the .env file
ENV_PATH="''${XDG_CONFIG_HOME}/api_guard/.env"
# Load .env file from the specified path
if [ -f "$ENV_PATH" ]; then
export $(cat "$ENV_PATH" | ${pkgs.busybox}/bin/grep -v '^#' | ${pkgs.busybox}/bin/xargs)
else
# DOCKER
export $(cat "/tmp/.env" | ${pkgs.busybox}/bin/grep -v '^#' | ${pkgs.busybox}/bin/xargs)
fi
# Set default values if variables are not set in .env file
PORT=''${PORT:-5000}
LOGDIR=''${LOGDIR:-/tmp}
# Start Gunicorn
export PYTHONPATH=${pythonEnv}/${pythonEnv.sitePackages}
${pkgs.python3Packages.gunicorn}/bin/gunicorn -w 1 -b 0.0.0.0:$PORT --access-logfile $LOGDIR/access.log --error-logfile $LOGDIR/error.log api_guard:app
'';
src = ./.;
installPhase = ''
mkdir -p $out/bin
cp -r . $out/bin/
cp ${script} $out/bin/api_guard
'';
};
defaultPackage = self.packages.${system}.api_guard;
# Usage:
# nix build .#docker
# docker load < result
# docker run -p5000:5000 -v ${realpath ./rundir):/tmp api_guard:lastest
packages.docker = pkgs.dockerTools.buildImage {
name = "api_guard"; # give docker image a name
tag = "latest"; # provide a tag
created = "now";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [ packages.api_guard pythonEnv pkgs.gnused pkgs.coreutils];
pathsToLink = [ "/bin" "/tmp" ];
};
config = {
Cmd = [ "${packages.api_guard}/bin/api_guard" ];
WorkingDir = "/bin";
Volumes = {
"/tmp" = { };
};
ExposedPorts = {
"5000/tcp" = {};
};
};
};
});
}