network_inventory/flake.nix

183 lines
6.6 KiB
Nix
Raw Normal View History

2021-12-08 18:07:49 +01:00
{
description = "A Python API for various tools I use at work.";
inputs = {
2022-11-19 18:10:59 +01:00
nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
2022-11-17 18:15:27 +01:00
flake-utils.url = github:numtide/flake-utils;
2022-11-19 19:17:13 +01:00
poetry2nix = {
url = "github:nix-community/poetry2nix";
2022-08-29 17:44:26 +02:00
inputs.nixpkgs.follows = "nixpkgs";
2022-01-31 19:27:53 +01:00
};
2021-12-08 18:07:49 +01:00
};
2022-11-19 19:17:13 +01:00
outputs = { self, nixpkgs, flake-utils, poetry2nix }:
{
2022-11-27 21:56:11 +01:00
overlays.default = nixpkgs.lib.composeManyExtensions [
2022-11-19 19:17:13 +01:00
poetry2nix.overlay
(final: prev: {
inventoryDevEnv = prev.poetry2nix.mkPoetryEnv
{
projectDir = ./.;
groups = [ "main" "dev" ];
};
inventoryEnv = prev.poetry2nix.mkPoetryEnv {
projectDir = ./.;
groups = [ "main" ];
};
2022-11-22 22:28:50 +01:00
})
2022-11-19 19:17:13 +01:00
];
} // (flake-utils.lib.eachDefaultSystem (system:
2021-12-08 18:07:49 +01:00
let
pkgs = import nixpkgs {
inherit system;
2022-11-27 21:56:11 +01:00
overlays = [ self.overlays.default ];
};
2022-12-12 09:11:13 +01:00
src = with pkgs.lib;
cleanSource (cleanSourceWith {
filter = name: type:
let
baseName = baseNameOf (toString name);
in
!(builtins.elem baseName [
".coverage"
".coveragerc"
".dir-locals.el"
".direnv"
".git"
".github"
".env"
".envrc"
".flake8"
".gitignore"
".gitlab-ci.yml"
2022-12-12 16:18:25 +01:00
"conftest.py"
2022-12-12 09:11:13 +01:00
"docker-compose.yaml"
"flake.lock"
"flake.nix"
"Makefile"
"poetry.lock"
"poetry.toml"
"pyproject.toml"
"pytest.ini"
2022-12-12 16:18:25 +01:00
"__pycache__"
2022-12-12 09:18:55 +01:00
"*.pyc"
2022-12-12 16:18:25 +01:00
"tests"
2022-12-12 09:11:13 +01:00
]);
src = ./.;
});
inventory = pkgs.stdenv.mkDerivation {
inherit src;
version = "latest";
pname = "network-inventory";
installPhase = ''
mkdir -p $out
cp -r ./src $out/code
'';
};
2022-03-01 09:56:58 +01:00
in
2022-12-12 09:24:59 +01:00
rec {
2022-11-22 22:12:53 +01:00
devShells.default = pkgs.mkShell {
2022-03-01 09:56:58 +01:00
buildInputs = [
pkgs.gnumake
pkgs.inventoryDevEnv
2022-11-19 19:36:34 +01:00
pkgs.poetry
2022-11-22 12:31:25 +01:00
pkgs.python310Packages.pip
2022-03-01 09:56:58 +01:00
];
shellHook = ''
export DJANGO_SETTINGS_MODULE=network_inventory.settings.local
'';
2021-12-08 18:07:49 +01:00
};
2022-11-27 21:36:06 +01:00
checks = {
lint = pkgs.stdenv.mkDerivation {
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontInstall = true;
doCheck = true;
name = "lint";
src = ./.;
checkInputs = [ pkgs.inventoryDevEnv ];
2022-11-27 21:36:06 +01:00
checkPhase = ''
mkdir -p $out
flake8 . --count --show-source --statistics
'';
};
tests = pkgs.stdenv.mkDerivation {
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontInstall = true;
doCheck = true;
name = "test";
src = ./.;
checkInputs = [ pkgs.inventoryDevEnv ];
2022-11-27 21:36:06 +01:00
checkPhase = ''
mkdir -p $out
pytest --ds=network_inventory.settings.ram_test \
-nauto \
--nomigrations \
--cov=./src \
./src
'';
};
2022-11-27 21:20:24 +01:00
};
2022-12-12 09:11:13 +01:00
packages = {
venv = pkgs.inventoryEnv;
container = pkgs.dockerTools.buildImage {
name = "network-inventory";
tag = "latest";
created = "now";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [
pkgs.bashInteractive
pkgs.coreutils
pkgs.inventoryEnv
inventory
(pkgs.writeShellScriptBin "start-inventory" ''
if [ -f .second_run ]; then
sleep 2
${pkgs.python3}/bin/python manage.py collectstatic --noinput
${pkgs.python3}/bin/python manage.py makemigrations
${pkgs.python3}/bin/python manage.py migrate
else
${pkgs.python3}/bin/python manage.py collectstatic --noinput
${pkgs.python3}/bin/python manage.py makemigrations backups
${pkgs.python3}/bin/python manage.py makemigrations computers
${pkgs.python3}/bin/python manage.py makemigrations core
${pkgs.python3}/bin/python manage.py makemigrations customers
${pkgs.python3}/bin/python manage.py makemigrations devices
${pkgs.python3}/bin/python manage.py makemigrations licenses
${pkgs.python3}/bin/python manage.py makemigrations nets
${pkgs.python3}/bin/python manage.py makemigrations softwares
${pkgs.python3}/bin/python manage.py makemigrations users
${pkgs.python3}/bin/python manage.py makemigrations
${pkgs.python3}/bin/python manage.py migrate
${pkgs.python3}/bin/python manage.py loaddata backups
${pkgs.python3}/bin/python manage.py loaddata computers
${pkgs.python3}/bin/python manage.py loaddata core
${pkgs.python3}/bin/python manage.py loaddata devices
${pkgs.python3}/bin/python manage.py loaddata nets
${pkgs.python3}/bin/python manage.py loaddata softwares
${pkgs.python3}/bin/python manage.py shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@example.com', 'password')"
touch .second_run
fi
${pkgs.python310Packages.gunicorn}/bin/gunicorn network_inventory.wsgi:application --reload --bind 0.0.0.0:8000 --workers 3
'')
];
};
config = {
Cmd = [ "start-inventory" ];
2022-12-12 16:18:11 +01:00
WorkingDir = "/code";
2022-12-12 09:11:13 +01:00
Env = [
"POSTGRES_DB=network_inventory"
"DJANGO_SETTINGS_MODULE=network_inventory.settings.production"
"PYTHONPATH=/lib/python3.10:/lib/python3.10/site-packages"
];
};
};
2022-12-12 09:24:59 +01:00
default = packages.container;
2022-12-12 09:11:13 +01:00
};
2022-11-19 19:17:13 +01:00
}));
2021-12-08 18:07:49 +01:00
}
2022-11-27 21:20:24 +01:00