Skip to content

Commit

Permalink
fully working nix support
Browse files Browse the repository at this point in the history
This patch adds full-on NixOS support.

Signed-off-by: Amy Parker <amy@amyip.net>
  • Loading branch information
amyipdev committed Aug 11, 2023
1 parent d7f2aa8 commit 3d146d3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 deletions.
1 change: 0 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/bash
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# ssvp: server statistics viewer project
Expand Down
18 changes: 9 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Flask
mysql-connector-python
ping3
requests
psycopg2
pyopenssl
gunicorn
sphinx
sphinx_rtd_theme
Flask==2.3.2
mysql-connector-python==8.1.0
ping3==4.8.4
requests==2.31.0
psycopg2==2.9.6
pyopenssl==23.2.0
gunicorn==21.2.0
sphinx==6.2.1
sphinx_rtd_theme==1.2.2
79 changes: 79 additions & 0 deletions service.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{ pkgs, config, lib, ... }:

with lib;

let
python3 = pkgs.python311.withPackages (ps: with ps; [
flask
mysql-connector
ping3
requests
psycopg2
gunicorn
]);

cfg = config.services.ssvp;
npmlock2nix = import (builtins.fetchTarball
"https://github.com/nix-community/npmlock2nix/archive/9197bbf.tar.gz"
) {};
nodeModules = npmlock2nix.v2.node_modules {
src = ./.;
installPhase = "mv node_modules $out/";
};
ins = pkgs.runCommand "ssvp" {src = ./.; buildInputs = with pkgs; [ nodejs ];} ''
cp -r --no-preserve=all $src $out
cd $out
cp -r ${nodeModules} node_modules
make
'';

in {
options.services.ssvp = {
enable = mkEnableOption "SSVP Production Environment";
configFile = mkOption {
type = types.str;
default = "${ins}/srv/ssvp-config.json";
description = "Configuration file";
};
};
config = mkIf cfg.enable {
systemd.services.ssvp-gunicorn = {
path = with pkgs; [
python3
which
jq
bash
];
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
script = ''
cd ${ins}
${pkgs.bash}/bin/bash srv/gunicorn.sh
'';
environment = { SSVP_CONFIG = cfg.configFile; };
};
systemd.timers.ssvp-interval = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnUnitActiveSec = "5m";
Unit = "ssvp-interval.service";
};
};
systemd.services.ssvp-interval = {
path = with pkgs; [
python3
bash
];
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
script = ''
cd ${ins}
${python3}/bin/python3 srv/interval.py
'';
serviceConfig = {
Type = "oneshot";
};
environment = { SSVP_CONFIG = cfg.configFile; };
};
};
}
2 changes: 1 addition & 1 deletion srv/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

app = Flask(__name__, template_folder="../web")
cd = os.path.dirname(__file__)
config = json.load(open(f"{cd}/ssvp-config.json", "r"))
config = json.load(open(x if (x := os.getenv("SSVP_CONFIG")) else f"{cd}/ssvp-config.json", "r"))
db = getdb.get_handler(config["database"])


Expand Down
8 changes: 6 additions & 2 deletions srv/gunicorn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ set -e

cd "$(dirname "$0")"

SSL=$(jq -j .ssl ssvp-config.json)
PORT=$(jq -j .port ssvp-config.json)
if [ "$SSVP_CONFIG" == "" ]; then
SSVP_CONFIG=ssvp-config.json
fi

SSL=$(jq -j .ssl $SSVP_CONFIG)
PORT=$(jq -j .port $SSVP_CONFIG)

if [ $PORT == "null" ]; then
if [ $SSL != "null" ]; then
Expand Down
2 changes: 1 addition & 1 deletion srv/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import test_modules

cd = os.path.dirname(__file__)
config = json.load(open(f"{cd}/ssvp-config.json", "r"))
config = json.load(open(x if (x := os.getenv("SSVP_CONFIG")) else f"{cd}/ssvp-config.json", "r"))
db = getdb.get_handler(config["database"])


Expand Down
1 change: 0 additions & 1 deletion uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/bash
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# ssvp: server statistics viewer project
Expand Down

0 comments on commit 3d146d3

Please sign in to comment.