2024-01-09 17:20:58 +00:00
|
|
|
{
|
|
|
|
description = "SelfPrivacy API flake";
|
|
|
|
|
|
|
|
inputs.nixpkgs.url = "github:nixos/nixpkgs";
|
|
|
|
|
|
|
|
outputs = { self, nixpkgs, ... }:
|
|
|
|
let
|
|
|
|
system = "x86_64-linux";
|
|
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
selfprivacy-graphql-api = pkgs.callPackage ./default.nix {
|
2024-02-12 21:47:58 +00:00
|
|
|
pythonPackages = pkgs.python310Packages;
|
2024-01-09 17:20:58 +00:00
|
|
|
rev = self.shortRev or self.dirtyShortRev or "dirty";
|
|
|
|
};
|
2024-01-23 19:25:51 +00:00
|
|
|
python = self.packages.${system}.default.pythonModule;
|
|
|
|
python-env =
|
|
|
|
python.withPackages (ps:
|
|
|
|
self.packages.${system}.default.propagatedBuildInputs ++ (with ps; [
|
|
|
|
coverage
|
|
|
|
pytest
|
|
|
|
pytest-datadir
|
|
|
|
pytest-mock
|
2024-02-26 19:49:32 +00:00
|
|
|
pytest-subprocess
|
2024-02-12 21:47:58 +00:00
|
|
|
black
|
|
|
|
mypy
|
|
|
|
pylsp-mypy
|
|
|
|
python-lsp-black
|
|
|
|
python-lsp-server
|
2024-03-01 13:55:02 +00:00
|
|
|
pyflakes
|
2024-02-12 21:47:58 +00:00
|
|
|
typer # for strawberry
|
2024-03-06 15:08:04 +00:00
|
|
|
types-redis # for mypy
|
2024-02-12 21:47:58 +00:00
|
|
|
] ++ strawberry-graphql.optional-dependencies.cli));
|
|
|
|
|
2024-01-23 19:25:51 +00:00
|
|
|
vmtest-src-dir = "/root/source";
|
|
|
|
shellMOTD = ''
|
|
|
|
Welcome to SP API development shell!
|
|
|
|
|
|
|
|
[formatters]
|
|
|
|
|
|
|
|
black
|
|
|
|
nixpkgs-fmt
|
|
|
|
|
|
|
|
[testing in NixOS VM]
|
|
|
|
|
2024-03-14 03:02:20 +00:00
|
|
|
nixos-test-driver - run an interactive NixOS VM with all dependencies included and 2 disk volumes
|
2024-01-23 19:25:51 +00:00
|
|
|
pytest-vm - run pytest in an ephemeral NixOS VM with Redis, accepting pytest arguments
|
|
|
|
'';
|
2024-01-09 17:20:58 +00:00
|
|
|
in
|
|
|
|
{
|
2024-01-23 19:25:51 +00:00
|
|
|
# see https://github.com/NixOS/nixpkgs/blob/66a9817cec77098cfdcbb9ad82dbb92651987a84/nixos/lib/test-driver/test_driver/machine.py#L359
|
|
|
|
packages.${system} = {
|
|
|
|
default = selfprivacy-graphql-api;
|
|
|
|
pytest-vm = pkgs.writeShellScriptBin "pytest-vm" ''
|
|
|
|
set -o errexit
|
|
|
|
set -o nounset
|
|
|
|
set -o xtrace
|
|
|
|
|
|
|
|
# see https://github.com/NixOS/nixpkgs/blob/66a9817cec77098cfdcbb9ad82dbb92651987a84/nixos/lib/test-driver/test_driver/machine.py#L359
|
|
|
|
export TMPDIR=''${TMPDIR:=/tmp}/nixos-vm-tmp-dir
|
|
|
|
readonly NIXOS_VM_SHARED_DIR_HOST="$TMPDIR/shared-xchg"
|
|
|
|
readonly NIXOS_VM_SHARED_DIR_GUEST="/tmp/shared"
|
|
|
|
|
|
|
|
mkdir -p "$TMPDIR"
|
|
|
|
ln -sfv "$PWD" -T "$NIXOS_VM_SHARED_DIR_HOST"
|
|
|
|
|
|
|
|
SCRIPT=$(cat <<EOF
|
|
|
|
start_all()
|
|
|
|
machine.succeed("ln -sf $NIXOS_VM_SHARED_DIR_GUEST -T ${vmtest-src-dir} >&2")
|
|
|
|
machine.succeed("cd ${vmtest-src-dir} && coverage run -m pytest -v $@ >&2")
|
|
|
|
machine.succeed("cd ${vmtest-src-dir} && coverage report >&2")
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
|
|
|
|
if [ -f "/etc/arch-release" ]; then
|
|
|
|
${self.checks.${system}.default.driverInteractive}/bin/nixos-test-driver --no-interactive <(printf "%s" "$SCRIPT")
|
|
|
|
else
|
|
|
|
${self.checks.${system}.default.driver}/bin/nixos-test-driver -- <(printf "%s" "$SCRIPT")
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2024-01-09 17:20:58 +00:00
|
|
|
nixosModules.default =
|
|
|
|
import ./nixos/module.nix self.packages.${system}.default;
|
2024-03-07 23:46:02 +00:00
|
|
|
devShells.${system}.default = pkgs.mkShellNoCC {
|
2024-01-23 19:25:51 +00:00
|
|
|
name = "SP API dev shell";
|
2024-02-12 21:47:58 +00:00
|
|
|
packages = with pkgs; [
|
|
|
|
nixpkgs-fmt
|
|
|
|
rclone
|
|
|
|
redis
|
|
|
|
restic
|
|
|
|
self.packages.${system}.pytest-vm
|
|
|
|
# FIXME consider loading this explicitly only after ArchLinux issue is solved
|
|
|
|
self.checks.x86_64-linux.default.driverInteractive
|
|
|
|
# the target API application python environment
|
|
|
|
python-env
|
|
|
|
];
|
2024-01-09 17:20:58 +00:00
|
|
|
shellHook = ''
|
|
|
|
# envs set with export and as attributes are treated differently.
|
|
|
|
# for example. printenv <Name> will not fetch the value of an attribute.
|
2024-01-23 19:25:51 +00:00
|
|
|
export TEST_MODE="true"
|
|
|
|
|
|
|
|
# more tips for bash-completion to work on non-NixOS:
|
|
|
|
# https://discourse.nixos.org/t/whats-the-nix-way-of-bash-completion-for-packages/20209/16?u=alexoundos
|
|
|
|
# Load installed profiles
|
|
|
|
for file in "/etc/profile.d/"*.sh; do
|
|
|
|
# If that folder doesn't exist, bash loves to return the whole glob
|
|
|
|
[[ -f "$file" ]] && source "$file"
|
|
|
|
done
|
|
|
|
|
|
|
|
printf "%s" "${shellMOTD}"
|
2024-01-09 17:20:58 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-01-23 19:25:51 +00:00
|
|
|
checks.${system} = {
|
|
|
|
fmt-check = pkgs.runCommandLocal "sp-api-fmt-check"
|
|
|
|
{ nativeBuildInputs = [ pkgs.black ]; }
|
|
|
|
"black --check ${self.outPath} > $out";
|
|
|
|
default =
|
|
|
|
pkgs.testers.runNixOSTest {
|
2024-03-08 10:40:27 +00:00
|
|
|
name = "default";
|
|
|
|
nodes.machine = { lib, pkgs, ... }: {
|
2024-03-14 03:02:20 +00:00
|
|
|
# 2 additional disks (1024 MiB and 200 MiB) with empty ext4 FS
|
|
|
|
virtualisation.emptyDiskImages = [ 1024 200 ];
|
2024-03-08 10:40:27 +00:00
|
|
|
virtualisation.fileSystems."/volumes/vdb" = {
|
|
|
|
autoFormat = true;
|
|
|
|
device = "/dev/vdb"; # this name is chosen by QEMU, not here
|
|
|
|
fsType = "ext4";
|
|
|
|
noCheck = true;
|
2024-01-23 19:25:51 +00:00
|
|
|
};
|
2024-03-14 03:02:20 +00:00
|
|
|
virtualisation.fileSystems."/volumes/vdc" = {
|
|
|
|
autoFormat = true;
|
|
|
|
device = "/dev/vdc"; # this name is chosen by QEMU, not here
|
|
|
|
fsType = "ext4";
|
|
|
|
noCheck = true;
|
|
|
|
};
|
2024-03-08 10:40:27 +00:00
|
|
|
boot.consoleLogLevel = lib.mkForce 3;
|
|
|
|
documentation.enable = false;
|
|
|
|
services.journald.extraConfig = lib.mkForce "";
|
|
|
|
services.redis.servers.sp-api = {
|
|
|
|
enable = true;
|
|
|
|
save = [ ];
|
|
|
|
settings.notify-keyspace-events = "KEA";
|
|
|
|
};
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
python-env
|
|
|
|
# TODO: these can be passed via wrapper script around app
|
|
|
|
rclone
|
|
|
|
restic
|
|
|
|
];
|
|
|
|
environment.variables.TEST_MODE = "true";
|
|
|
|
systemd.tmpfiles.settings.src.${vmtest-src-dir}.L.argument =
|
|
|
|
self.outPath;
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
machine.succeed("cd ${vmtest-src-dir} && coverage run --data-file=/tmp/.coverage -m pytest -p no:cacheprovider -v >&2")
|
|
|
|
machine.succeed("coverage xml --rcfile=${vmtest-src-dir}/.coveragerc --data-file=/tmp/.coverage >&2")
|
|
|
|
machine.copy_from_vm("coverage.xml", ".")
|
|
|
|
machine.succeed("coverage report >&2")
|
|
|
|
'';
|
2024-01-23 19:25:51 +00:00
|
|
|
};
|
|
|
|
};
|
2024-01-09 17:20:58 +00:00
|
|
|
};
|
|
|
|
nixConfig.bash-prompt = ''\n\[\e[1;32m\][\[\e[0m\]\[\e[1;34m\]SP devshell\[\e[0m\]\[\e[1;32m\]:\w]\$\[\[\e[0m\] '';
|
|
|
|
}
|