clan-core/pkgs/clan-cli/default.nix

174 lines
4.1 KiB
Nix
Raw Normal View History

{ age
, lib
, argcomplete
2023-08-23 15:17:34 +00:00
, fastapi
, uvicorn
, installShellFiles
, nix
, openssh
, pytest
, pytest-cov
, pytest-subprocess
2023-08-27 07:45:15 +00:00
, pytest-parallel
, python3
, runCommand
, setuptools
, sops
2023-08-09 14:38:08 +00:00
, stdenv
, wheel
, fakeroot
2023-08-10 10:30:52 +00:00
, rsync
, ui-assets
, bash
, sshpass
, zbar
, tor
, git
, nixpkgs
2023-09-27 17:02:03 +00:00
, makeDesktopItem
, copyDesktopItems
2023-09-28 16:27:06 +00:00
, qemu
, gnupg
, e2fsprogs
}:
let
2023-08-22 21:15:34 +00:00
2023-08-23 15:17:34 +00:00
dependencies = [
argcomplete # optional dependency: if not enabled, shell completion will not work
fastapi
uvicorn # optional dependencies: if not enabled, webui subcommand will not work
];
pytestDependencies = runtimeDependencies ++ dependencies ++ [
pytest
pytest-cov
pytest-subprocess
2023-08-27 07:45:15 +00:00
pytest-parallel
2023-08-09 14:38:08 +00:00
openssh
git
2023-09-29 16:31:05 +00:00
gnupg
2023-08-09 14:38:08 +00:00
stdenv.cc
];
# Optional dependencies for clan cli, we re-expose them here to make sure they all build.
runtimeDependencies = [
bash
nix
fakeroot
openssh
sshpass
zbar
tor
age
rsync
sops
git
2023-09-28 16:27:06 +00:00
qemu
e2fsprogs
];
runtimeDependenciesAsSet = builtins.listToAttrs (builtins.map (p: lib.nameValuePair (lib.getName p.name) p) runtimeDependencies);
checkPython = python3.withPackages (_ps: pytestDependencies);
# - vendor the jsonschema nix lib (copy instead of symlink).
# Interesting fact: using nixpkgs from flakes instead of nixpkgs.path is reduces evaluation time by 5s.
source = runCommand "clan-cli-source" { } ''
2023-08-10 09:30:12 +00:00
cp -r ${./.} $out
chmod -R +w $out
rm $out/clan_cli/config/jsonschema
ln -s ${nixpkgs'} $out/clan_cli/nixpkgs
cp -r ${../../lib/jsonschema} $out/clan_cli/config/jsonschema
ln -s ${ui-assets} $out/clan_cli/webui/assets
'';
nixpkgs' = runCommand "nixpkgs" { nativeBuildInputs = [ nix ]; } ''
mkdir $out
2023-09-06 15:29:49 +00:00
cat > $out/flake.nix << EOF
{
description = "dependencies for the clan-cli";
inputs = {
nixpkgs.url = "nixpkgs";
};
outputs = _inputs: { };
}
EOF
ln -s ${nixpkgs} $out/path
2023-09-06 15:29:49 +00:00
nix flake lock $out \
--store ./. \
--extra-experimental-features 'nix-command flakes' \
--override-input nixpkgs ${nixpkgs}
'';
in
python3.pkgs.buildPythonApplication {
2023-08-08 12:20:45 +00:00
name = "clan-cli";
src = source;
format = "pyproject";
nativeBuildInputs = [
setuptools
installShellFiles
2023-09-27 17:02:03 +00:00
copyDesktopItems
];
propagatedBuildInputs = dependencies;
# also re-expose dependencies so we test them in CI
2023-09-22 13:39:03 +00:00
passthru.tests = (lib.mapAttrs' (n: lib.nameValuePair "clan-dep-${n}") runtimeDependenciesAsSet) // {
clan-pytest = runCommand "clan-pytest" { nativeBuildInputs = [ checkPython ] ++ pytestDependencies; } ''
cp -r ${source} ./src
chmod +w -R ./src
cd ./src
export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1
${checkPython}/bin/python -m pytest -m "not impure" -s ./tests
touch $out
'';
};
2023-08-25 09:39:46 +00:00
passthru.clan-openapi = runCommand "clan-openapi" { } ''
cp -r ${source} ./src
chmod +w -R ./src
cd ./src
export PATH=${checkPython}/bin:$PATH
2023-08-25 09:39:46 +00:00
${checkPython}/bin/python ./bin/gen-openapi --out $out/openapi.json --app-dir . clan_cli.webui.app:app
touch $out
'';
passthru.nixpkgs = nixpkgs';
passthru.checkPython = checkPython;
passthru.devDependencies = [
setuptools
wheel
] ++ pytestDependencies;
passthru.pytestDependencies = pytestDependencies;
passthru.runtimeDependencies = runtimeDependencies;
2023-08-23 10:32:06 +00:00
postInstall = ''
cp -r ${nixpkgs'} $out/${python3.sitePackages}/clan_cli/nixpkgs
installShellCompletion --bash --name clan \
<(${argcomplete}/bin/register-python-argcomplete --shell bash clan)
installShellCompletion --fish --name clan.fish \
<(${argcomplete}/bin/register-python-argcomplete --shell fish clan)
'';
2023-08-03 13:33:08 +00:00
checkPhase = ''
PYTHONPATH= $out/bin/clan --help
2023-08-09 08:36:46 +00:00
if grep --include \*.py -Rq "breakpoint()" $out; then
echo "breakpoint() found in $out:"
grep --include \*.py -Rn "breakpoint()" $out
exit 1
fi
2023-08-03 13:33:08 +00:00
'';
meta.mainProgram = "clan";
2023-09-27 17:02:03 +00:00
desktopItems = [
(makeDesktopItem {
name = "clan";
exec = "clan";
desktopName = "CLan Manager";
startupWMClass = "clan";
mimeTypes = [ "x-scheme-handler/clan" ];
})
];
}