Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring in python devshell with Nix #94

Merged
merged 23 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6632ec8
nix/devshell.nix: remove nix-fast-build
flokli Nov 13, 2023
4f3f6d8
nix/devshell.nix: remove pkgs.python3Packages let
flokli Nov 13, 2023
5b37b60
remove scripts/env.sh
flokli Nov 14, 2023
e4c0456
nix/packages.nix: expose ppyrate,requests-limiter
flokli Nov 14, 2023
84ecffb
nix/devshell.nix: rename buildInputs to packages
flokli Nov 14, 2023
83444f0
pytest.init: add pythonpath = .
flokli Nov 14, 2023
c101784
nix/devshell: add py dev dependencies to devshell
flokli Nov 14, 2023
0355e85
tests/test_sbomnix: replace nix run invocations
flokli Nov 14, 2023
82443e8
tests/test_sbombox.py: factor out subprocess call
flokli Nov 14, 2023
de1a193
tests/test_sbomnix: set PYTHONPATH for subprocess
flokli Nov 14, 2023
d6dfbf5
test_sbomnix.py: fix binary path
flokli Nov 14, 2023
c25d3f0
test_sbomnix.py: skip vulnxscan --triage tests
flokli Nov 14, 2023
0dabfbb
nix/checks.nix: reuse is a nativeBuildInput
flokli Nov 14, 2023
c022b10
Makefile: add `check` target, migrate reuse-lint
flokli Nov 14, 2023
e79b2a0
Makefile: move pycodestyle to flake check
flokli Nov 14, 2023
bc09083
Makefile: move pylint to flake check
flokli Nov 14, 2023
01269c7
nix/checks: add black to flake checks
flokli Nov 14, 2023
a3e6a77
nix/devshell.nix: rm pycodestyle & pylint
flokli Nov 14, 2023
e0be61e
Makefile: expose sbomnix python in pylint check
flokli Nov 14, 2023
254e294
nix/packages: make reuse a python library
flokli Nov 14, 2023
6111605
nix/devshell.nix: append "." to PYTHONPATH
flokli Nov 15, 2023
7f6fa45
README: update instructions for devshell
flokli Nov 15, 2023
fa376d9
fix: Use treefmt to run black
brianmcgee Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 9 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ define target_success
@printf "\033[32m==> Target \"$(1)\" passed\033[0m\n\n"
endef

define try_run_sbomnix
@if ! source scripts/env.sh && sbomnix -h 2>/dev/null; then \
echo "\033[31mError:\033[0m failed to run sbomnix, maybe it's not in your PATH?"; \
exit 1; \
fi
endef

.DEFAULT_GOAL := help

TARGET: ## DESCRIPTION
Expand All @@ -24,57 +17,28 @@ TARGET: ## DESCRIPTION
help: ## Show this help message
@grep -E '^[a-zA-Z_-]+:.*?##.*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'

install: ## Install sbomnix
pip install --user .
$(call try_run_sbomnix,$@)
$(call target_success,$@)

install-dev: uninstall install-dev-requirements ## Install for development
pip install --editable .
$(call try_run_sbomnix,$@)
pre-push: test format check ## Run tests, black, and flake checks
$(call target_success,$@)

uninstall: ## Uninstall sbomnix
find . -name '*.egg-info' -exec rm -fr {} +
pip uninstall -y sbomnix
test-ci: check ## Run CI tests
pytest -vx -k "not skip_in_ci" tests/
$(call target_success,$@)

install-dev-requirements: clean ## Install all requirements
pip install -q -r requirements.txt --no-cache-dir
$(call target_success,$@)

pre-push: test black style pylint reuse-lint ## Run tests, pycodestyle, pylint, reuse-lint
$(call target_success,$@)
check: clean
nix --extra-experimental-features flakes flake check

test-ci: install-dev-requirements style pylint reuse-lint ## Run CI tests
source scripts/env.sh && pytest -vx -k "not skip_in_ci" tests/
test: ## Run tests
pytest -vx tests/
$(call target_success,$@)

test: install-dev-requirements ## Run tests
source scripts/env.sh && pytest -vx tests/
$(call target_success,$@)

black: clean ## Reformat with black
format: clean ## Reformat with black
@for py in $(PYTHON_TARGETS); \
do echo "$$py:"; \
black -q $$py; \
done
$(call target_success,$@)

style: clean ## Check with pycodestyle (pep8)
pycodestyle --max-line-length 90 $(PYTHON_TARGETS)
$(call target_success,$@)

pylint: clean ## Check with pylint
pylint --disable duplicate-code -rn $(PYTHON_TARGETS) || exit 1
$(call target_success,$@)

reuse-lint: clean ## Check with reuse lint
reuse lint
$(call target_success,$@)

release-asset: clean install-dev-requirements ## Build release asset
nix build
release-asset: clean ## Build release asset
nix-shell -p nix-info --run "nix-info -m"
nix-env -qa --meta --json -f $(shell nix-shell -p nix-info --run "nix-info -m" | grep "nixpkgs: " | cut -d'`' -f2) '.*' >meta.json
mkdir -p build/
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,22 @@ $ cd sbomnix
$ nix-shell
```

From the development shell, run `sbomnix` as follows:
Keep in mind this doesn't add the various entrypoint binaries to your `PATH`
directly. They are produced during the setuptools build.

While you're in the devshell, you can run run `sbomnix` via the entrypoint file
directly:

```bash
$ sbomnix --help
$ sbomnix/main.py --help
```
flokli marked this conversation as resolved.
Show resolved Hide resolved

## Usage Examples
The usage examples work for both the built package, as well as inside the devshell.

Keep in mind inside the devshell, calls to `sbomnix` need to be replaced with
`sbomnix/main.py` (and similar for other entrypoints).

In the below examples, we use Nix package `wget` as an example target.
To print `wget` out-path on your local system, try:
```bash
Expand Down
25 changes: 0 additions & 25 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
url = "github:nix-community/flake-compat";
flake = false;
};
nix-fast-build = {
url = "github:Mic92/nix-fast-build";
# re-use some existing inputs
inputs = {
flake-parts.follows = "flake-parts";
treefmt-nix.follows = "treefmt-nix";
};
};
nix-visualize = {
url = "github:craigmbooth/nix-visualize";
flake = false;
Expand Down
19 changes: 18 additions & 1 deletion nix/checks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@
# todo this could be moved into a shared flake
reuse =
pkgs.runCommandLocal "reuse-lint" {
buildInputs = [pkgs.reuse];
nativeBuildInputs = [pkgs.reuse];
} ''
cd ${../.}
reuse lint
touch $out
'';
pycodestyle =
pkgs.runCommandLocal "pycodestyle" {
nativeBuildInputs = [pkgs.python3.pkgs.pycodestyle];
} ''
cd ${../.}
pycodestyle --max-line-length 90 $(find . -name "*.py" ! -path "*venv*" ! -path "*eggs*")
touch $out
'';
pylint =
pkgs.runCommandLocal "pylint" {
nativeBuildInputs = [self'.packages.python pkgs.python3.pkgs.pylint];
} ''
cd ${../.}
export HOME=/tmp
pylint --disable duplicate-code -rn $(find . -name "*.py" ! -path "*venv*" ! -path "*eggs*")
touch $out
'';
}
//
# merge in the package derivations to force a build of all packages during a `nix flake check`
Expand Down
77 changes: 29 additions & 48 deletions nix/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,39 @@
{
perSystem = {
pkgs,
inputs',
self',
...
}: {
devShells.default = let
pp = pkgs.python3Packages;
in
pkgs.mkShell rec {
name = "sbomnix-dev-shell";
devShells.default = pkgs.mkShell rec {
name = "sbomnix-dev-shell";

buildInputs =
(with pkgs; [
coreutils
curl
gnugrep
gnused
graphviz
grype
gzip
nix
reuse
])
++ (with self'.packages; [
vulnix
nix-visualize
])
++ (with pp; [
beautifulsoup4
colorlog
graphviz
numpy
packageurl-python
packaging
pandas
requests
requests-cache
tabulate
venvShellHook
wheel
])
++ [inputs'.nix-fast-build.packages.default];
packages =
(with pkgs; [
coreutils
curl
gnugrep
gnused
graphviz
grype
gzip
nix
black
reuse
])
++ (with self'.packages; [
nix-visualize
python # that python with all sbomnix [dev-]dependencies
vulnix
]);

venvDir = "venv";
postShellHook = ''
source $PWD/scripts/env.sh

# https://github.com/NixOS/nix/issues/1009:
export TMPDIR="/tmp"

# Enter python development environment
make install-dev
'';
};
# Add the repo root to PYTHONPATH, so invoking entrypoints (and them being
# able to find the python packages in the repo) becomes possible.
# `pytest.ini` already sets this for invoking `pytest`
# (cascading down to the processes it spawns), but this is for the developer
# invoking entrypoints from inside the devshell.
shellHook = ''
export PYTHONPATH="$PYTHONPATH:$(pwd)"
'';
};
};
}
Loading
Loading