2024-03-17 18:48:49 +00:00
|
|
|
{
|
2024-03-24 22:36:00 +00:00
|
|
|
# Inputs for the package
|
2024-03-17 18:48:49 +00:00
|
|
|
age,
|
|
|
|
lib,
|
|
|
|
argcomplete,
|
|
|
|
installShellFiles,
|
|
|
|
nix,
|
|
|
|
openssh,
|
|
|
|
pytest,
|
|
|
|
pytest-cov,
|
|
|
|
pytest-xdist,
|
|
|
|
pytest-subprocess,
|
|
|
|
pytest-timeout,
|
|
|
|
python3,
|
|
|
|
runCommand,
|
|
|
|
setuptools,
|
|
|
|
sops,
|
|
|
|
stdenv,
|
|
|
|
fakeroot,
|
|
|
|
rsync,
|
|
|
|
bash,
|
|
|
|
sshpass,
|
|
|
|
zbar,
|
|
|
|
tor,
|
|
|
|
git,
|
|
|
|
qemu,
|
|
|
|
gnupg,
|
|
|
|
e2fsprogs,
|
|
|
|
mypy,
|
2024-04-23 16:55:00 +00:00
|
|
|
nixpkgs,
|
2024-03-17 18:48:49 +00:00
|
|
|
clan-core-path,
|
2023-07-20 23:08:11 +00:00
|
|
|
}:
|
|
|
|
let
|
2024-03-24 22:36:00 +00:00
|
|
|
# Dependencies that are directly used in the project
|
|
|
|
pythonDependencies = [
|
|
|
|
argcomplete # Enables shell completion; without it, this feature won't work.
|
2023-08-23 15:17:34 +00:00
|
|
|
];
|
2023-07-20 16:07:27 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Runtime dependencies required by the application
|
2023-09-20 08:58:06 +00:00
|
|
|
runtimeDependencies = [
|
|
|
|
bash
|
|
|
|
nix
|
2023-09-26 13:32:36 +00:00
|
|
|
fakeroot
|
2023-09-20 08:58:06 +00:00
|
|
|
openssh
|
|
|
|
sshpass
|
|
|
|
zbar
|
|
|
|
tor
|
|
|
|
age
|
|
|
|
rsync
|
|
|
|
sops
|
|
|
|
git
|
2023-10-12 20:46:32 +00:00
|
|
|
mypy
|
2023-09-28 16:27:06 +00:00
|
|
|
qemu
|
2023-10-03 14:51:34 +00:00
|
|
|
e2fsprogs
|
2023-09-20 08:58:06 +00:00
|
|
|
];
|
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Dependencies required for running tests
|
|
|
|
testDependencies =
|
|
|
|
runtimeDependencies
|
|
|
|
++ [
|
|
|
|
gnupg
|
|
|
|
stdenv.cc # Compiler used for certain native extensions
|
|
|
|
]
|
|
|
|
++ pythonDependencies
|
|
|
|
++ [
|
|
|
|
pytest # Testing framework
|
|
|
|
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
|
|
|
|
];
|
|
|
|
|
|
|
|
# Convert runtimeDependencies into an attribute set for easier access
|
2024-03-17 18:48:49 +00:00
|
|
|
runtimeDependenciesAsSet = builtins.listToAttrs (
|
|
|
|
builtins.map (p: lib.nameValuePair (lib.getName p.name) p) runtimeDependencies
|
|
|
|
);
|
2023-09-20 08:58:06 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Setup Python environment with all dependencies for running tests
|
|
|
|
pythonWithTestDeps = python3.withPackages (_ps: testDependencies);
|
2023-08-09 16:38:44 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Prepare the source code for the project, including copying over jsonschema and nixpkgs
|
2023-08-09 16:38:44 +00:00
|
|
|
source = runCommand "clan-cli-source" { } ''
|
2023-08-10 09:30:12 +00:00
|
|
|
cp -r ${./.} $out
|
2023-08-09 16:38:44 +00:00
|
|
|
chmod -R +w $out
|
|
|
|
rm $out/clan_cli/config/jsonschema
|
2023-09-22 06:39:24 +00:00
|
|
|
ln -s ${nixpkgs'} $out/clan_cli/nixpkgs
|
2023-08-27 08:21:02 +00:00
|
|
|
cp -r ${../../lib/jsonschema} $out/clan_cli/config/jsonschema
|
2023-08-26 09:23:15 +00:00
|
|
|
'';
|
2024-03-24 22:36:00 +00:00
|
|
|
|
|
|
|
# Create a custom nixpkgs for use within the project
|
2023-09-22 06:39:24 +00:00
|
|
|
nixpkgs' = runCommand "nixpkgs" { nativeBuildInputs = [ nix ]; } ''
|
2023-09-06 12:40:00 +00:00
|
|
|
mkdir $out
|
2023-09-06 15:29:49 +00:00
|
|
|
cat > $out/flake.nix << EOF
|
|
|
|
{
|
|
|
|
description = "dependencies for the clan-cli";
|
|
|
|
|
|
|
|
inputs = {
|
2024-04-23 16:55:00 +00:00
|
|
|
nixpkgs.url = "path://${nixpkgs}";
|
2023-09-06 15:29:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
outputs = _inputs: { };
|
|
|
|
}
|
|
|
|
EOF
|
2023-09-22 06:39:24 +00:00
|
|
|
ln -s ${nixpkgs} $out/path
|
2024-04-23 16:55:00 +00:00
|
|
|
nix flake update $out \
|
2023-09-06 12:40:00 +00:00
|
|
|
--store ./. \
|
2024-04-23 16:55:00 +00:00
|
|
|
--extra-experimental-features 'nix-command flakes'
|
2023-08-09 16:38:44 +00:00
|
|
|
'';
|
2023-08-03 11:07:00 +00:00
|
|
|
in
|
2023-09-28 09:19:16 +00:00
|
|
|
python3.pkgs.buildPythonApplication {
|
2023-08-08 12:20:45 +00:00
|
|
|
name = "clan-cli";
|
2023-08-09 16:38:44 +00:00
|
|
|
src = source;
|
2023-08-03 11:07:00 +00:00
|
|
|
format = "pyproject";
|
2023-08-16 10:13:48 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Arguments for the wrapper to unset LD_LIBRARY_PATH to avoid glibc version issues
|
|
|
|
makeWrapperArgs = [ "--unset LD_LIBRARY_PATH" ];
|
2023-10-05 11:43:59 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Build-time dependencies.
|
2023-08-03 11:07:00 +00:00
|
|
|
nativeBuildInputs = [
|
|
|
|
setuptools
|
|
|
|
installShellFiles
|
|
|
|
];
|
2023-07-25 11:46:58 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
propagatedBuildInputs = pythonDependencies;
|
|
|
|
|
|
|
|
# Define and expose the tests and checks to run in CI
|
2024-03-17 18:48:49 +00:00
|
|
|
passthru.tests =
|
|
|
|
(lib.mapAttrs' (n: lib.nameValuePair "clan-dep-${n}") runtimeDependenciesAsSet)
|
2024-04-19 15:02:02 +00:00
|
|
|
// {
|
2024-03-17 18:48:49 +00:00
|
|
|
clan-pytest-without-core =
|
2024-03-24 22:36:00 +00:00
|
|
|
runCommand "clan-pytest-without-core"
|
|
|
|
{ nativeBuildInputs = [ pythonWithTestDeps ] ++ testDependencies; }
|
2024-03-17 18:48:49 +00:00
|
|
|
''
|
|
|
|
cp -r ${source} ./src
|
|
|
|
chmod +w -R ./src
|
|
|
|
cd ./src
|
|
|
|
|
|
|
|
export NIX_STATE_DIR=$TMPDIR/nix IN_NIX_SANDBOX=1
|
2024-03-24 22:36:00 +00:00
|
|
|
${pythonWithTestDeps}/bin/python -m pytest -m "not impure and not with_core" ./tests
|
2024-03-17 18:48:49 +00:00
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
clan-pytest-with-core =
|
2024-03-24 22:36:00 +00:00
|
|
|
runCommand "clan-pytest-with-core"
|
|
|
|
{ nativeBuildInputs = [ pythonWithTestDeps ] ++ testDependencies; }
|
2024-03-17 18:48:49 +00:00
|
|
|
''
|
|
|
|
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
|
2024-03-24 22:36:00 +00:00
|
|
|
${pythonWithTestDeps}/bin/python -m pytest -m "not impure and with_core" ./tests
|
2024-03-17 18:48:49 +00:00
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Utility to check for leftover debugging breakpoints in the codebase
|
2024-03-17 18:48:49 +00:00
|
|
|
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
|
|
|
|
'';
|
|
|
|
};
|
2023-09-20 08:58:06 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Additional pass-through attributes
|
2023-09-22 06:39:24 +00:00
|
|
|
passthru.nixpkgs = nixpkgs';
|
2024-03-24 22:36:00 +00:00
|
|
|
passthru.testDependencies = testDependencies;
|
|
|
|
passthru.pythonWithTestDeps = pythonWithTestDeps;
|
2023-09-20 08:58:06 +00:00
|
|
|
passthru.runtimeDependencies = runtimeDependencies;
|
2023-08-23 10:32:06 +00:00
|
|
|
|
2024-03-24 22:36:00 +00:00
|
|
|
# Install shell completions for bash and fish using the argcomplete package
|
2023-08-03 11:07:00 +00:00
|
|
|
postInstall = ''
|
2023-09-22 06:39:24 +00:00
|
|
|
cp -r ${nixpkgs'} $out/${python3.sitePackages}/clan_cli/nixpkgs
|
2023-08-03 11:07:00 +00:00
|
|
|
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-07-20 16:07:27 +00:00
|
|
|
'';
|
2024-03-24 22:36:00 +00:00
|
|
|
|
|
|
|
# Clean up after the package to avoid leaking python packages into a devshell
|
2023-10-04 08:34:32 +00:00
|
|
|
postFixup = ''
|
|
|
|
rm $out/nix-support/propagated-build-inputs
|
|
|
|
'';
|
2024-03-24 22:36:00 +00:00
|
|
|
|
|
|
|
# Run a basic check to ensure the application is executable
|
2023-08-03 13:33:08 +00:00
|
|
|
checkPhase = ''
|
|
|
|
PYTHONPATH= $out/bin/clan --help
|
|
|
|
'';
|
2024-03-24 22:36:00 +00:00
|
|
|
|
|
|
|
# Specify the main program for this package
|
2023-08-03 11:07:00 +00:00
|
|
|
meta.mainProgram = "clan";
|
|
|
|
}
|