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

202 lines
4.9 KiB
Nix
Raw Normal View History

2024-03-17 18:48:49 +00:00
{
age,
lib,
argcomplete,
installShellFiles,
nix,
openssh,
pytest,
pytest-cov,
pytest-xdist,
pytest-subprocess,
pytest-timeout,
remote-pdb,
ipdb,
python3,
runCommand,
setuptools,
sops,
stdenv,
wheel,
fakeroot,
rsync,
bash,
sshpass,
zbar,
tor,
git,
nixpkgs,
qemu,
gnupg,
e2fsprogs,
mypy,
rope,
clan-core-path,
}:
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
];
2024-03-17 18:48:49 +00:00
pytestDependencies =
runtimeDependencies
++ dependencies
++ [
pytest
pytest-cov
pytest-subprocess
pytest-xdist
pytest-timeout
remote-pdb
ipdb
openssh
git
gnupg
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
mypy
2023-09-28 16:27:06 +00:00
qemu
e2fsprogs
];
2024-03-17 18:48:49 +00:00
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
'';
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";
makeWrapperArgs = [
# This prevents problems with mixed glibc versions that might occur when the
# cli is called through a browser built against another glibc
"--unset LD_LIBRARY_PATH"
];
nativeBuildInputs = [
setuptools
installShellFiles
];
propagatedBuildInputs = dependencies;
# also re-expose dependencies so we test them in CI
2024-03-17 18:48:49 +00:00
passthru.tests =
(lib.mapAttrs' (n: lib.nameValuePair "clan-dep-${n}") runtimeDependenciesAsSet)
// rec {
clan-pytest-without-core =
runCommand "clan-pytest-without-core" { 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 and not with_core" ./tests
touch $out
'';
# separate the tests that can never be cached
clan-pytest-with-core =
runCommand "clan-pytest-with-core" { nativeBuildInputs = [ checkPython ] ++ pytestDependencies; }
''
cp -r ${source} ./src
chmod +w -R ./src
cd ./src
export CLAN_CORE=${clan-core-path}
export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1
${checkPython}/bin/python -m pytest -m "not impure and with_core" ./tests
touch $out
'';
clan-pytest = runCommand "clan-pytest" { } ''
echo ${clan-pytest-without-core}
echo ${clan-pytest-with-core}
touch $out
'';
check-for-breakpoints = runCommand "breakpoints" { } ''
if grep --include \*.py -Rq "breakpoint()" ${source}; then
echo "breakpoint() found in ${source}:"
grep --include \*.py -Rn "breakpoint()" ${source}
exit 1
fi
touch $out
'';
};
passthru.nixpkgs = nixpkgs';
passthru.checkPython = checkPython;
passthru.devDependencies = [
rope
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)
'';
# Don't leak python packages into a devshell.
2024-03-12 16:29:08 +00:00
# It can be very confusing if you `nix run` then load the cli from the devshell instead.
postFixup = ''
rm $out/nix-support/propagated-build-inputs
'';
2023-08-03 13:33:08 +00:00
checkPhase = ''
PYTHONPATH= $out/bin/clan --help
'';
meta.mainProgram = "clan";
}