Skip to content
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

Nix Package #1888

Closed
wants to merge 6 commits into from
Closed
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
20 changes: 20 additions & 0 deletions .github/workflows/nix-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Nix CI
on:
push:
pull_request:
jobs:
build:
name: Build Package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install nix
uses: cachix/install-nix-action@v22
with:
nix_path: nixpkgs=channel:nixos-unstable
# - uses: cachix/cachix-action@v12
# with:
# name: trawelling
# authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- name: Build with nix
run: nix build
52 changes: 19 additions & 33 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 7 additions & 83 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
description = "Traewelling";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
devenv.url = "github:cachix/devenv";
devenv = {
url = "github:cachix/devenv";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs @ {flake-parts, ...}:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
inputs.devenv.flakeModule
./nix/flake-module.nix
];
systems = [
"x86_64-linux"
Expand All @@ -19,89 +23,9 @@
perSystem = {
pkgs,
lib,
config,
...
}: {
devenv.shells.default = {config, ...}: {
# See https://github.com/cachix/devenv/issues/528#issuecomment-1556108767
containers = lib.mkForce {};
languages = {
php.enable = true;
javascript.enable = true;
};
dotenv.enable = true;
services.mysql = {
enable = true;
ensureUsers = [
{
name = config.env.DB_USERNAME;
password = config.env.DB_PASSWORD;
ensurePermissions = {
"*.*" = "ALL PRIVILEGES";
};
}
];
initialDatabases = [
{
name = config.env.DB_DATABASE;
}
];
};
scripts = let
composer = "${config.languages.php.packages.composer}/bin/composer";
php = "${config.languages.php.package}/bin/php";
npm = "${config.languages.javascript.package}/bin/npm";
mysql = config.services.mysql.package;

envKeys = builtins.attrNames config.env;
unsetEnv = builtins.concatStringsSep "\n" (
map (key: "unset ${key}") envKeys
);
in {
setup-devenv.exec = ''
set -eo pipefail
if [ ! -f .env ]
then
echo "Copying .env.example to .env"
cp .env.example .env
fi
set -a; source .env; set +a
echo "Installing composer packages"
${composer} install > /dev/null
echo "Installing npm packages"
${npm} ci > /dev/null

if [[ "$DB_CONNECTION" == "mysql" ]];
then
echo "Waiting for MySQL Database to be ready."
echo " Make sure to run 'devenv up' in another terminal to start the MySQL server."
while ! ${mysql}/bin/mysqladmin ping -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" -p="$DB_PASSWORD" --silent; do
sleep 1
done

echo "Migrating database"
${php} artisan migrate:fresh --seed
else
echo "You seem to be not using mysql. Skipping migrations."
fi

echo "Generating Keys"
${php} artisan key:generate > /dev/null
echo "Initializing Passport"
${php} artisan passport:install > /dev/null
'';
serve.exec = ''
# Unset .env variables, so laravel reads the .env files by itself
${unsetEnv}
${npm} run dev &
${php} artisan serve
'';
artisan.exec = ''
# Unset .env variables, so laravel reads the .env files by itself
${unsetEnv}
exec ${php} artisan $@
'';
};
};
formatter = pkgs.alejandra;
};
};
Expand Down
9 changes: 9 additions & 0 deletions nix/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{...}: {
imports = [
./shell.nix
];

perSystem = {pkgs, ...}: {
packages.default = pkgs.callPackage ./package {};
};
}
30 changes: 30 additions & 0 deletions nix/package/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
lib,
stdenv,
php,
# dataDir ? "/var/lib/traewelling",
# runtimeDir ? "/run/traewelling",
pkgs,
lndir,
}: let
web = pkgs.callPackage ./web {};
in
php.buildComposerProject {
pname = "traewelling";
version = "0.0.0";

src = lib.cleanSource ../..;

meta = {
description = "Free check-in service to log your public transit journeys";
license = lib.licenses.agpl3Only;
homepage = "https://traewellling.de";
inherit (php.meta) platforms;
};

postInstall = ''
${lndir}/bin/lndir -silent ${web} $out/share/php/traewelling/public
'';

vendorHash = "sha256-qyqH9o3zW5L1rg+IKFfRPWgQMm5ioeGmVeOEXjWoo8w=";
}
15 changes: 15 additions & 0 deletions nix/package/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p nix git

set -eo pipefail

GIT_TAG="$(git describe --tags --abbrev=0)"
OLD_VERSION="$(nix eval --raw ".#default.version")"
sed -i -E -e "s#version = \"$OLD_VERSION\"#version = \"${GIT_TAG#v}\"#" ./default.nix

OLD_HASH="$(nix eval --raw ".#default.vendorHash")"
EMPTY_HASH="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
sed -i -E -e "s#vendorHash = \".*\"#vendorHash = \"$EMPTY_HASH\"#" ./default.nix

NEW_HASH="$(nix build .#default 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo || true)"
sed -i -E -e "s#vendorHash = \"$EMPTY_HASH\"#vendorHash = \"${NEW_HASH:-$OLD_HASH}\"#" ./default.nix
21 changes: 21 additions & 0 deletions nix/package/web/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
lib,
buildNpmPackage,
}:
buildNpmPackage {
name = "traewelling-web";
src = lib.cleanSource ../../..;

npmDepsHash = "sha256-dSfHgmjvSbfEUwP6CnbulcVBHy2Qn135E/+XMA3kiK0=";

prePatch = ''
# delete public directory to only get web output results in this derivation
rm -rf public
'';

installPhase = ''
runHook preInstall
cp -r public $out
runHook postInstall
'';
}
5 changes: 5 additions & 0 deletions nix/package/web/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p nix prefetch-npm-deps

NPM_HASH=$(prefetch-npm-deps ../../../package-lock.json)
sed -i -E -e "s#npmDepsHash = \".*\"#npmDepsHash = \"$NPM_HASH\"#" ./default.nix
Loading