Skip to content

Add integrations with Nix and NixOS #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .actrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-P ubuntu-latest=catthehacker/ubuntu:runner-latest
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Run RaspberryPi-Gateway tests
on:
pull_request:
push:
workflow_dispatch:
jobs:
flake:
name: Run Nix flake checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Initialize Nix store paths
run: |
sudo mkdir -p /nix/store
sudo chmod -R 777 /nix
# https://github.com/cachix/install-nix-action/issues/56#issuecomment-1030697681
- name: Configure Nix store cache
uses: actions/cache@v4
with:
key: ${{ runner.os }}-${{ runner.arch }}-nix-store
# See https://github.com/actions/cache/pull/726 and
# https://github.com/actions/cache/issues/494 for caveats on negative
# patterns.
path: |
/nix/store/**
/nix/var/nix/*/*
/nix/var/nix/db/*
/nix/var/nix/db/*/**
!/nix/var/nix/daemon-socket/socket
!/nix/var/nix/userpool/*
!/nix/var/nix/gc.lock
!/nix/var/nix/db/big-lock
!/nix/var/nix/db/reserved
- name: Install Nix
uses: cachix/install-nix-action@v31
with:
extra_nix_config: |
system-features = benchmark big-parallel kvm nixos-test uid-range
- name: Run Nix flake checks
run: |
nix flake check -L
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
node_modules
db/*
*.log

# VM disk images
*.qcow2

# Nix build results
result
result-*
repl-result-*

# NixOS test driver REPL history
.nixos-test-history
23 changes: 1 addition & 22 deletions .setup/gateway
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,12 @@ server{
proxy_set_header X-Forwarded-User $remote_user;
}

# enable and configure PHP
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;

# typically the fpm.sock is used, but check to make sure by using:
# sudo grep -ri "listen = " /etc/php
# should get something like:
# /etc/php/7.3/fpm/pool.d/www.conf:listen = /run/php/php7.3-fpm.sock
# then you know it's fpm.sock
fastcgi_pass unix:/run/php/PHPFPMSOCK; #uncomment if PHP runs on fpm.sock
#fastcgi_pass 127.0.0.1:9000; #uncomment if PHP is running on port 9000

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/pi/gateway/www$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
include fastcgi_params;
}

# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
Expand Down
7 changes: 0 additions & 7 deletions .setup/gatewaysetup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ sudo apt-get -y install git apache2-utils
echo -e "${CYAN}************* STEP: Install latest NGINX *************${NC}"
sudo apt-get -y install nginx

echo -e "${CYAN}************* STEP: Install latest PHP *********************${NC}"
sudo apt-get -y install php-common php-cli php-fpm

echo -e "${CYAN}************* STEP: Install nodeJS & npm *************${NC}"
#install latest NodeJS --- https://www.raspberrypi.org/forums/viewtopic.php?t=141770
sudo wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | sudo bash
Expand Down Expand Up @@ -179,10 +176,6 @@ echo -e "${YLW}Done. You can add/change http_auth credentials using ${RED}htpass

echo -e "${CYAN}************* STEP: Copy gateway site config to sites-available *************${NC}"
cp -rf $APPSRVDIR/.setup/gateway /etc/nginx/sites-available/gateway
#determine php-fpm version and replace in gateway site config
phpfpmsock=$(grep -ri "listen = /" /etc/php) #search for file containing "listen =" in php path
phpfpmsock=${phpfpmsock##*/} #extract everything after last /
sudo sed -i "s/PHPFPMSOCK/${phpfpmsock}/g" /etc/nginx/sites-available/gateway #replace PHPFPMSOCK with it in site config file
cd /etc/nginx/sites-enabled
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/gateway
Expand Down
36 changes: 36 additions & 0 deletions app.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
lib,
config,
dream2nix,
pkgs,
...
} @ args: let
packageJSON = lib.importJSON ./package.json;
in {
imports = [
dream2nix.modules.dream2nix.nodejs-package-json-v3
dream2nix.modules.dream2nix.nodejs-granular-v3
];

inherit (packageJSON) name version;

# Take advantage of a shell injection vector in dream2nix in order to apply a
# workaround for an issue with `npm i` converting dependencies specified with
# `git+https://` or `github:` to `git+ssh://`.
# See:
# - https://github.com/npm/cli/issues/2610
# - https://github.com/npm/cli/issues/2631
nodejs-package-json.npmArgs = lib.mkAfter [
"--package-lock-only' ; ${lib.getExe pkgs.gnused} -i -e 's|git+ssh://|git+https://|g' ./package-lock.json #"
];

mkDerivation = {
src = lib.cleanSource ./.;
doCheck = false;
meta = {
# XXX broken somehow? `lib.licenses.gpl3` works...
#license = lib.licenses.cc-by-nc-40;
homepage = "https://github.com/LowPowerLab/RaspberryPi-Gateway";
};
};
}
86 changes: 86 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// **********************************************************************************
// Websocket server backend for the RaspberryPi-Gateway App
// http://lowpowerlab.com/gateway
// **********************************************************************************
// Common application configuration settings.
// **********************************************************************************

const JSON5 = require('json5'); // https://github.com/aseemk/json5
const nconf = require('nconf'); // https://github.com/indexzero/nconf
const path = require('path');

exports.load = function({defaultStateDir = __dirname, defaultContentDir = null} = {}) {
nconf.argv().env()

const stateDir = path.normalize(nconf.get('MOTEINO_GATEWAY_STATE_DIRECTORY') || defaultStateDir);

const resolvePath = (base) => (...dirs) => path.resolve(base, ...dirs);

const resolveCoreStatePath = resolvePath(defaultStateDir);
const resolveUserStatePath = resolvePath(stateDir);

const coreContentDir = resolveCoreStatePath('www');
const contentDir = path.normalize(nconf.get('MOTEINO_GATEWAY_CONTENT_DIRECTORY') || defaultContentDir || coreContentDir);

const resolveCoreContentPath = resolvePath(coreContentDir);
const resolveUserContentPath = resolvePath(contentDir);

const coreImagesDir = resolveCoreContentPath('images');
const userImagesDir = resolveUserContentPath('images');

const coreMetricsDir = resolveCoreStatePath('metrics');
const userMetricsDir = resolveUserStatePath('metrics');

const dbDir = resolveUserStatePath('data/db');

if (stateDir == defaultStateDir)
{
nconf.file('mutable5', {
file: resolveCoreStatePath('settings.json5'),
format: JSON5,
});

nconf.file('mutable', {
file: resolveCoreStatePath('settings.json'),
});
}
else
{
nconf.file('mutable5', {
file: resolveUserStatePath('settings.json5'),
format: JSON5,
});

nconf.file('mutable', {
file: resolveUserStatePath('settings.json'),
});

nconf.file('immutable5', {
file: resolveCoreStatePath('settings.json5'),
format: JSON5,
});

nconf.file('immutable', {
file: resolveCoreStatePath('settings.json'),
});
}

return {
'nconf': nconf,
'stateDir': stateDir,
'contentDir': contentDir,
'coreContentDir': coreContentDir,
'coreImagesDir': coreImagesDir,
'userImagesDir': userImagesDir,
'coreMetricsDir': coreMetricsDir,
'userMetricsDir': userMetricsDir,
'dbDir': dbDir,
'resolveCoreContentPath': resolveCoreContentPath,
'resolveCoreStatePath': resolveCoreStatePath,
'resolveUserContentPath': resolveUserContentPath,
'resolveUserStatePath': resolveUserStatePath,
};
};

// Load configuration and export it
Object.assign(exports, exports.load());
2 changes: 2 additions & 0 deletions data/db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading