clan-app: Fix default.nix

This commit is contained in:
Qubasa
2025-01-08 18:43:20 +01:00
parent 4f416eb32d
commit 8d4d98361d
9 changed files with 83 additions and 111 deletions

View File

@@ -92,13 +92,12 @@
treefmt.programs.mypy.directories = treefmt.programs.mypy.directories =
{ {
"clan-cli" = { "clan-cli" = {
extraPythonPackages = self'.packages.clan-cli.testDependencies;
directory = "pkgs/clan-cli"; directory = "pkgs/clan-cli";
extraPythonPackages = (self'.packages.clan-cli.devshellPyDeps pkgs.python3Packages);
}; };
"clan-app" = { "clan-app" = {
directory = "pkgs/clan-app"; directory = "pkgs/clan-app";
extraPythonPackages = extraPythonPackages = (self'.packages.clan-app.devshellPyDeps pkgs.python3Packages);
(self'.packages.clan-app.externalTestDeps or [ ]) ++ self'.packages.clan-cli.testDependencies;
extraPythonPaths = [ "../clan-cli" ]; extraPythonPaths = [ "../clan-cli" ];
}; };
} }
@@ -107,8 +106,9 @@
{ {
"clan-vm-manager" = { "clan-vm-manager" = {
directory = "pkgs/clan-vm-manager"; directory = "pkgs/clan-vm-manager";
extraPythonPackages = extraPythonPackages = self'.packages.clan-vm-manager.externalTestDeps ++ [
self'.packages.clan-vm-manager.externalTestDeps ++ self'.packages.clan-cli.testDependencies; (pkgs.python3.withPackages (ps: self'.packages.clan-cli.devshellPyDeps ps))
];
extraPythonPaths = [ "../clan-cli" ]; extraPythonPaths = [ "../clan-cli" ];
}; };
} }

View File

@@ -1,16 +1,16 @@
{ {
python3Full,
runCommand, runCommand,
setuptools,
copyDesktopItems, copyDesktopItems,
clan-cli, clan-cli,
makeDesktopItem, makeDesktopItem,
webview-ui, webview-ui,
webview-lib, webview-lib,
fontconfig, fontconfig,
pythonRuntime,
}: }:
let let
source = ./.; source = ./.;
desktop-file = makeDesktopItem { desktop-file = makeDesktopItem {
name = "org.clan.app"; name = "org.clan.app";
exec = "clan-app %u"; exec = "clan-app %u";
@@ -25,24 +25,29 @@ let
]; ];
pyDeps = ps: [
ps.tkinter
];
# Dependencies required for running tests # Dependencies required for running tests
pyTestDeps = pyTestDeps =
ps: ps:
with ps;
[ [
(python3Full.pkgs.toPythonModule pytest)
# Testing framework # Testing framework
pytest-cov # Generate coverage reports ps.pytest
pytest-subprocess # fake the real subprocess behavior to make your tests more independent. ps.pytest-cov # Generate coverage reports
pytest-xdist # Run tests in parallel on multiple cores ps.pytest-subprocess # fake the real subprocess behavior to make your tests more independent.
pytest-timeout # Add timeouts to your tests ps.pytest-xdist # Run tests in parallel on multiple cores
ps.pytest-timeout # Add timeouts to your tests
] ]
++ pytest.propagatedBuildInputs; ++ ps.pytest.propagatedBuildInputs;
clan-cli-module = [ (python3Full.pkgs.toPythonModule clan-cli) ]; clan-cli-module = [
(pythonRuntime.pkgs.toPythonModule (clan-cli.override { inherit pythonRuntime; }))
];
in in
python3Full.pkgs.buildPythonApplication rec { pythonRuntime.pkgs.buildPythonApplication {
name = "clan-app"; name = "clan-app";
src = source; src = source;
format = "pyproject"; format = "pyproject";
@@ -51,7 +56,7 @@ python3Full.pkgs.buildPythonApplication rec {
preFixup = '' preFixup = ''
makeWrapperArgs+=( makeWrapperArgs+=(
--set FONTCONFIG_FILE ${fontconfig.out}/etc/fonts/fonts.conf --set FONTCONFIG_FILE ${fontconfig.out}/etc/fonts/fonts.conf
--set WEBUI_PATH "$out/${python3Full.sitePackages}/clan_app/.webui" --set WEBUI_PATH "$out/${pythonRuntime.sitePackages}/clan_app/.webui"
--set WEBVIEW_LIB_DIR "${webview-lib}/lib" --set WEBVIEW_LIB_DIR "${webview-lib}/lib"
# This prevents problems with mixed glibc versions that might occur when the # This prevents problems with mixed glibc versions that might occur when the
# cli is called through a browser built against another glibc # cli is called through a browser built against another glibc
@@ -62,7 +67,7 @@ python3Full.pkgs.buildPythonApplication rec {
# Deps needed only at build time # Deps needed only at build time
nativeBuildInputs = [ nativeBuildInputs = [
setuptools (pythonRuntime.withPackages (ps: [ ps.setuptools ]))
copyDesktopItems copyDesktopItems
fontconfig fontconfig
]; ];
@@ -71,8 +76,9 @@ python3Full.pkgs.buildPythonApplication rec {
# same values for your Python package within Nix largely stems from ensuring # same values for your Python package within Nix largely stems from ensuring
# that all necessary dependencies are consistently available both # that all necessary dependencies are consistently available both
# at build time and runtime, # at build time and runtime,
buildInputs = clan-cli-module ++ runtimeDependencies; propagatedBuildInputs = [
propagatedBuildInputs = buildInputs; (pythonRuntime.withPackages (ps: clan-cli-module ++ (pyDeps ps)))
] ++ runtimeDependencies;
# also re-expose dependencies so we test them in CI # also re-expose dependencies so we test them in CI
passthru = { passthru = {
@@ -81,7 +87,7 @@ python3Full.pkgs.buildPythonApplication rec {
runCommand "clan-app-pytest" runCommand "clan-app-pytest"
{ {
buildInputs = runtimeDependencies ++ [ buildInputs = runtimeDependencies ++ [
(python3Full.withPackages (ps: clan-cli-module ++ (pyTestDeps ps))) (pythonRuntime.withPackages (ps: clan-cli-module ++ (pyTestDeps ps) ++ (pyDeps ps)))
fontconfig fontconfig
]; ];
} }
@@ -105,7 +111,7 @@ python3Full.pkgs.buildPythonApplication rec {
echo "STARTING ..." echo "STARTING ..."
export WEBVIEW_LIB_DIR="${webview-lib}/lib" export WEBVIEW_LIB_DIR="${webview-lib}/lib"
export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1 export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1
python3 -m pytest -s -m "not impure" ./tests python -m pytest -s -m "not impure" ./tests
touch $out touch $out
''; '';
}; };
@@ -113,11 +119,12 @@ python3Full.pkgs.buildPythonApplication rec {
# Additional pass-through attributes # Additional pass-through attributes
passthru.desktop-file = desktop-file; passthru.desktop-file = desktop-file;
passthru.devshellDeps = ps: (pyTestDeps ps); passthru.devshellPyDeps = ps: (pyTestDeps ps) ++ (pyDeps ps);
passthru.pythonRuntime = pythonRuntime;
postInstall = '' postInstall = ''
mkdir -p $out/${python3Full.sitePackages}/clan_app/.webui mkdir -p $out/${pythonRuntime.sitePackages}/clan_app/.webui
cp -r ${webview-ui}/lib/node_modules/@clan/webview-ui/dist/* $out/${python3Full.sitePackages}/clan_app/.webui cp -r ${webview-ui}/lib/node_modules/@clan/webview-ui/dist/* $out/${pythonRuntime.sitePackages}/clan_app/.webui
mkdir -p $out/share/icons/hicolor mkdir -p $out/share/icons/hicolor
cp -r ./clan_app/assets/white-favicons/* $out/share/icons/hicolor cp -r ./clan_app/assets/white-favicons/* $out/share/icons/hicolor
''; '';

View File

@@ -17,8 +17,9 @@
inherit (config.packages) clan-app webview-lib; inherit (config.packages) clan-app webview-lib;
inherit self'; inherit self';
}; };
packages.clan-app = pkgs.python3.pkgs.callPackage ./default.nix { packages.clan-app = pkgs.callPackage ./default.nix {
inherit (config.packages) clan-cli webview-ui webview-lib; inherit (config.packages) clan-cli webview-ui webview-lib;
pythonRuntime = pkgs.python3;
}; };
checks = config.packages.clan-app.tests; checks = config.packages.clan-app.tests;

View File

@@ -36,6 +36,3 @@ disallow_untyped_calls = true
disallow_untyped_defs = true disallow_untyped_defs = true
no_implicit_optional = true no_implicit_optional = true
[[tool.mypy.overrides]]
module = "clan_cli.*"
ignore_missing_imports = true

View File

@@ -5,7 +5,6 @@
ruff, ruff,
gtk4, gtk4,
webview-lib, webview-lib,
python3Full,
self', self',
}: }:
@@ -14,14 +13,14 @@ mkShell {
inputsFrom = [ self'.devShells.default ]; inputsFrom = [ self'.devShells.default ];
buildInputs = [ buildInputs = [
(python3Full.withPackages ( (clan-app.pythonRuntime.withPackages (
ps: ps:
with ps; with ps;
[ [
ruff ruff
mypy mypy
] ]
++ (clan-app.devshellDeps ps) ++ (clan-app.devshellPyDeps ps)
)) ))
]; ];

View File

@@ -1,67 +1,53 @@
{ {
# callPackage args # callPackage args
argcomplete,
gnupg, gnupg,
installShellFiles, installShellFiles,
lib, lib,
nix, nix,
pkgs, pkgs,
pytest-cov,
pytest-subprocess,
pytest-timeout,
pytest-xdist,
pytest,
python3,
runCommand, runCommand,
setuptools,
stdenv, stdenv,
nixVersions, nixVersions,
# custom args # custom args
clan-core-path, clan-core-path,
nixpkgs, nixpkgs,
includedRuntimeDeps, includedRuntimeDeps,
inventory-schema-abstract, inventory-schema-abstract,
classgen, classgen,
pythonRuntime,
}: }:
let let
pythonDependencies = [ pyDeps = ps: [
argcomplete # Enables shell completions ps.argcomplete # Enables shell completions
]; ];
pyTestDeps =
ps:
[
ps.pytest
ps.pytest-cov
ps.pytest-subprocess
ps.pytest-xdist
ps.pytest-timeout
]
++ (pyDeps ps);
pythonRuntimeWithDeps = pythonRuntime.withPackages (ps: pyDeps ps);
# load nixpkgs runtime dependencies from a json file # load nixpkgs runtime dependencies from a json file
# This file represents an allow list at the same time that is checked by the run_cmd # This file represents an allow list at the same time that is checked by the run_cmd
# implementation in nix.py # implementation in nix.py
allDependencies = lib.importJSON ./clan_cli/nix/allowed-programs.json; allDependencies = lib.importJSON ./clan_cli/nix/allowed-programs.json;
generateRuntimeDependenciesMap = generateRuntimeDependenciesMap =
deps: deps:
lib.filterAttrs (_: pkg: !pkg.meta.unsupported or false) (lib.genAttrs deps (name: pkgs.${name})); lib.filterAttrs (_: pkg: !pkg.meta.unsupported or false) (lib.genAttrs deps (name: pkgs.${name}));
runtimeDependenciesMap = generateRuntimeDependenciesMap allDependencies; runtimeDependenciesMap = generateRuntimeDependenciesMap allDependencies;
runtimeDependencies = lib.attrValues runtimeDependenciesMap; runtimeDependencies = lib.attrValues runtimeDependenciesMap;
includedRuntimeDependenciesMap = generateRuntimeDependenciesMap includedRuntimeDeps; includedRuntimeDependenciesMap = generateRuntimeDependenciesMap includedRuntimeDeps;
testDependencies = testDependencies = runtimeDependencies ++ [
runtimeDependencies gnupg
++ [ stdenv.cc # Compiler used for certain native extensions
gnupg (pythonRuntime.withPackages (ps: (pyTestDeps ps) ++ (pyDeps ps)))
stdenv.cc # Compiler used for certain native extensions ];
]
++ pythonDependencies
++ [
pytest
pytest-cov # Generate coverage reports
pytest-subprocess # fake the real subprocess behavior to make your tests more independent.
pytest-xdist # Run tests in parallel on multiple cores
pytest-timeout # Add timeouts to your tests
];
# Setup Python environment with all dependencies for running tests
pythonWithTestDeps = python3.withPackages (_ps: testDependencies);
source = runCommand "clan-cli-source" { } '' source = runCommand "clan-cli-source" { } ''
cp -r ${./.} $out cp -r ${./.} $out
@@ -73,7 +59,6 @@ let
''; '';
# Create a custom nixpkgs for use within the project # Create a custom nixpkgs for use within the project
nixpkgs' = nixpkgs' =
runCommand "nixpkgs" runCommand "nixpkgs"
{ {
@@ -101,7 +86,7 @@ let
--extra-experimental-features 'nix-command flakes' --extra-experimental-features 'nix-command flakes'
''; '';
in in
python3.pkgs.buildPythonApplication { pythonRuntime.pkgs.buildPythonApplication {
name = "clan-cli"; name = "clan-cli";
src = source; src = source;
format = "pyproject"; format = "pyproject";
@@ -123,30 +108,29 @@ python3.pkgs.buildPythonApplication {
]; ];
nativeBuildInputs = [ nativeBuildInputs = [
setuptools (pythonRuntime.withPackages (ps: [ ps.setuptools ]))
installShellFiles installShellFiles
]; ];
propagatedBuildInputs = pythonDependencies; propagatedBuildInputs = [ pythonRuntimeWithDeps ] ++ runtimeDependencies;
# Define and expose the tests and checks to run in CI # Define and expose the tests and checks to run in CI
passthru.tests = (lib.mapAttrs' (n: lib.nameValuePair "clan-dep-${n}") runtimeDependenciesMap) // { passthru.tests = (lib.mapAttrs' (n: lib.nameValuePair "clan-dep-${n}") runtimeDependenciesMap) // {
clan-pytest-without-core = clan-pytest-without-core =
runCommand "clan-pytest-without-core" runCommand "clan-pytest-without-core" { nativeBuildInputs = testDependencies; }
{ nativeBuildInputs = [ pythonWithTestDeps ] ++ testDependencies; }
'' ''
cp -r ${source} ./src cp -r ${source} ./src
chmod +w -R ./src chmod +w -R ./src
cd ./src cd ./src
export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1 PYTHONWARNINGS=error export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1 PYTHONWARNINGS=error
${pythonWithTestDeps}/bin/python -m pytest -m "not impure and not with_core" ./tests python -m pytest -m "not impure and not with_core" ./tests
touch $out touch $out
''; '';
clan-pytest-with-core = clan-pytest-with-core =
runCommand "clan-pytest-with-core" runCommand "clan-pytest-with-core"
{ {
nativeBuildInputs = [ pythonWithTestDeps ] ++ testDependencies; nativeBuildInputs = testDependencies;
buildInputs = [ buildInputs = [
pkgs.bash pkgs.bash
pkgs.coreutils pkgs.coreutils
@@ -177,25 +161,25 @@ python3.pkgs.buildPythonApplication {
mkdir -p "$CLAN_TEST_STORE/nix/store" mkdir -p "$CLAN_TEST_STORE/nix/store"
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths" xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration" nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
${pythonWithTestDeps}/bin/python -m pytest -m "not impure and with_core" ./tests python -m pytest -m "not impure and with_core" ./tests
touch $out touch $out
''; '';
}; };
passthru.nixpkgs = nixpkgs'; passthru.nixpkgs = nixpkgs';
passthru.testDependencies = testDependencies; passthru.devshellPyDeps = ps: (pyTestDeps ps) ++ (pyDeps ps);
passthru.pythonWithTestDeps = pythonWithTestDeps; passthru.pythonRuntime = pythonRuntime;
passthru.runtimeDependencies = runtimeDependencies; passthru.runtimeDependencies = runtimeDependencies;
passthru.runtimeDependenciesMap = runtimeDependenciesMap; passthru.runtimeDependenciesMap = runtimeDependenciesMap;
postInstall = '' postInstall = ''
cp -r ${nixpkgs'} $out/${python3.sitePackages}/clan_cli/nixpkgs cp -r ${nixpkgs'} $out/${pythonRuntime.sitePackages}/clan_cli/nixpkgs
installShellCompletion --bash --name clan \ installShellCompletion --bash --name clan \
<(${argcomplete}/bin/register-python-argcomplete --shell bash clan) <(${pythonRuntimeWithDeps.pkgs.argcomplete}/bin/register-python-argcomplete --shell bash clan)
installShellCompletion --fish --name clan.fish \ installShellCompletion --fish --name clan.fish \
<(${argcomplete}/bin/register-python-argcomplete --shell fish clan) <(${pythonRuntimeWithDeps.pkgs.argcomplete}/bin/register-python-argcomplete --shell fish clan)
installShellCompletion --zsh --name _clan \ installShellCompletion --zsh --name _clan \
<(${argcomplete}/bin/register-python-argcomplete --shell zsh clan) <(${pythonRuntimeWithDeps.pkgs.argcomplete}/bin/register-python-argcomplete --shell zsh clan)
''; '';
# Clean up after the package to avoid leaking python packages into a devshell # Clean up after the package to avoid leaking python packages into a devshell

View File

@@ -103,21 +103,23 @@
inherit self'; inherit self';
}; };
packages = { packages = {
clan-cli = pkgs.python3.pkgs.callPackage ./default.nix { clan-cli = pkgs.callPackage ./default.nix {
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
inherit (self'.packages) classgen; inherit (self'.packages) classgen;
inherit (self'.legacyPackages.schemas) inventory-schema-abstract; inherit (self'.legacyPackages.schemas) inventory-schema-abstract;
pythonRuntime = pkgs.python3;
clan-core-path = clanCoreWithVendoredDeps; clan-core-path = clanCoreWithVendoredDeps;
includedRuntimeDeps = [ includedRuntimeDeps = [
"age" "age"
"git" "git"
]; ];
}; };
clan-cli-full = pkgs.python3.pkgs.callPackage ./default.nix { clan-cli-full = pkgs.callPackage ./default.nix {
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs;
inherit (self'.packages) classgen; inherit (self'.packages) classgen;
inherit (self'.legacyPackages.schemas) inventory-schema-abstract; inherit (self'.legacyPackages.schemas) inventory-schema-abstract;
clan-core-path = clanCoreWithVendoredDeps; clan-core-path = clanCoreWithVendoredDeps;
pythonRuntime = pkgs.python3;
includedRuntimeDeps = lib.importJSON ./clan_cli/nix/allowed-programs.json; includedRuntimeDeps = lib.importJSON ./clan_cli/nix/allowed-programs.json;
}; };
clan-cli-docs = pkgs.stdenv.mkDerivation { clan-cli-docs = pkgs.stdenv.mkDerivation {

View File

@@ -45,19 +45,4 @@ disallow_untyped_defs = true
no_implicit_optional = true no_implicit_optional = true
exclude = "clan_cli.nixpkgs" exclude = "clan_cli.nixpkgs"
[[tool.mypy.overrides]]
module = "argcomplete.*"
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "ipdb.*"
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "pytest.*"
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "setuptools.*"
ignore_missing_imports = true

View File

@@ -5,25 +5,22 @@
clan-cli-full, clan-cli-full,
mkShell, mkShell,
ruff, ruff,
python3,
self', self',
}: }:
let
devshellTestDeps =
clan-cli.passthru.testDependencies
++ (with python3.pkgs; [
rope
setuptools
wheel
webcolors
pip
]);
in
mkShell { mkShell {
buildInputs = [ buildInputs = [
(clan-cli.pythonRuntime.withPackages (
ps:
with ps;
[
ruff
mypy
]
++ (clan-cli.devshellPyDeps ps)
))
nix-unit nix-unit
ruff ] ++ clan-cli.runtimeDependencies;
] ++ devshellTestDeps;
inputsFrom = [ self'.devShells.default ]; inputsFrom = [ self'.devShells.default ];