Skip to content

Commit ae96dcc

Browse files
committed
Use mach-nix instead of poetry2nix.
Signed-off-by: Rolf Schröder <rolf.schr@gmail.com>
1 parent a584ac4 commit ae96dcc

9 files changed

Lines changed: 163 additions & 1583 deletions

README.rst

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,20 @@ There are several options to use the Nix version
149149
# Enter an interactive environment with all dependencies setup.
150150
cd etc/nix
151151
nix develop
152-
> ./manage.py ... # invoke the local checkout
152+
> ../../manage.py ... # invoke the local checkout
153153
> vulnerablecode-manage.py ... # invoke manage.py as installed in the nix store
154154

155155
# Test the import prodecure using the Nix version.
156-
./test-import-using-nix.sh --all # import everything
156+
etc/nix/test-import-using-nix.sh --all # import everything
157157
# Test the import using the local checkout.
158-
INSTALL_DIR=. ./test-import-using-nix.sh ruby # import ruby only
158+
INSTALL_DIR=. etc/nix/test-import-using-nix.sh ruby # import ruby only
159159

160160

161161
**Keeping the Nix setup in sync**
162162

163-
The Nix installation uses `poetry2nix <https://github.com/nix-community/poetry2nix>`__ to handle Python dependencies because some dependencies are currently not available as Nix packages.
164-
The are some ``*.generated`` files in ``etc/nix`` that are created/updated using ``etc/nix/generate-poetry-files.sh``.
165-
These files need to be recreated whenever ``./requirements.txt`` changes.
166-
The ``expectedRequirementstxtMd5sum`` in ``etc/nix/flake.nix`` also needs to be updated in that case.
167-
The Nix installation uses the files to convert VulnerableCode into a `Poetry <https://python-poetry.org/>`__ project on the fly.
168-
169-
::
170-
171-
# Update poetry-conversion.patch.
172-
etc/nix/generate-poetry-files.sh
173-
# Get new hash. See flake.nix.
174-
md5sum requirements.txt
163+
The Nix installation uses `mach-nix <https://github.com/DavHau/mach-nix>`__ to handle Python dependencies because some dependencies are currently not available as Nix packages.
164+
All Python dependencies are automatically fetched from ``./requirements.txt``.
165+
Non-Python dependencies are curated in ``etc/nix/flake.nix:Vulnerablecode.propagatedBuildInputs``.
175166

176167

177168
Tests

etc/nix/default.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(import (fetchTarball
2+
"https://github.com/edolstra/flake-compat/archive/master.tar.gz") {
3+
src = ./.;
4+
}).defaultNix

etc/nix/flake.lock

Lines changed: 70 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/nix/flake.nix

Lines changed: 59 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
description =
33
"Vulnerablecode - A free and open vulnerabilities database and the packages they impact.";
44

5-
# Nixpkgs / NixOS version to use.
65
inputs.nixpkgs = {
76
type = "github";
87
owner = "NixOS";
98
repo = "nixpkgs";
109
ref = "20.09";
1110
};
1211

13-
outputs = { self, nixpkgs }:
12+
inputs.machnix = {
13+
type = "github";
14+
owner = "DavHau";
15+
repo = "mach-nix";
16+
ref = "3.1.1";
17+
};
18+
19+
outputs = { self, nixpkgs, machnix }:
1420
let
1521

1622
vulnerablecode-src = ./../..;
@@ -19,8 +25,7 @@
1925
version = builtins.head (builtins.match ''.*version=["']?([^"',]+).*''
2026
(builtins.readFile (vulnerablecode-src + "/setup.py")));
2127

22-
# From commit 7f8ae6399b02b1d508689b303f117e2f03f7854a
23-
expectedRequirementstxtMd5sum = "7ea5fec4096b9c532450d68fad721017";
28+
libSh = ./lib.sh;
2429

2530
# System types to support.
2631
supportedSystems = [ "x86_64-linux" ];
@@ -36,72 +41,55 @@
3641
overlays = [ self.overlay ];
3742
});
3843

44+
# mach-nix instantiated for supported system types.
45+
machnixFor = forAllSystems (system:
46+
import machnix {
47+
pkgs = (nixpkgsFor.${system}).pkgs;
48+
python = "python38";
49+
});
50+
3951
in {
4052

4153
# A Nixpkgs overlay.
4254
overlay = final: prev:
4355
with final.pkgs; {
4456

45-
# Create a mock project.
46-
mockPoetryProject =
47-
runCommand "mockPoetryProject" { buildInputs = [ rename ]; } ''
48-
EXPECTED=${expectedRequirementstxtMd5sum}
49-
ACTUAL=$(md5sum ${vulnerablecode-src}/requirements.txt | cut -d ' ' -f 1)
50-
if [[ $EXPECTED != $ACTUAL ]] ; then
51-
echo ""
52-
echo "The requirements.txt has changed!"
53-
echo "1) Run make-poetry-conversion-patch.sh."
54-
echo "2) Update expectedRequirementstxtMd5sum in flake.nix."
55-
exit 1
56-
fi
57-
58-
mkdir $out
59-
cd $out
60-
cp ${vulnerablecode-src}/etc/nix/{pyproject.toml,poetry.lock}.generated .
61-
rename 's/.generated$//' *.generated
62-
'';
57+
pythonEnv = with machnixFor.${system};
58+
mkPython {
59+
requirements =
60+
builtins.readFile (vulnerablecode-src + "/requirements.txt");
61+
};
6362

64-
vulnerablecode = poetry2nix.mkPoetryApplication rec {
65-
projectDir = mockPoetryProject; # where to find {pyproject.toml,poetry.lock}
63+
vulnerablecode = stdenv.mkDerivation {
64+
inherit version;
65+
name = "vulnerablecode-${version}";
6666
src = vulnerablecode-src;
67-
python = python38;
68-
overrides = poetry2nix.overrides.withDefaults (self: super: {
69-
pygit2 = super.pygit2.overridePythonAttrs
70-
(old: { buildInputs = old.buildInputs ++ [ libgit2-glib ]; });
71-
});
72-
73-
patchPhase = ''
74-
# Make sure "our" pycodestyle binary is used.
75-
sed -i 's/join(bin_dir, "pycodestyle")/"pycodestyle"/' vulnerabilities/tests/test_basics.py
76-
'';
77-
78-
propagatedBuildInputs = [ postgresql ];
79-
8067
dontConfigure = true; # do not use ./configure
81-
dontBuild = true;
68+
propagatedBuildInputs = [ pythonEnv postgresql ];
69+
70+
postPatch = ''
71+
# Make sure the pycodestyle binary in $PATH is used.
72+
substituteInPlace vulnerabilities/tests/test_basics.py \
73+
--replace 'join(bin_dir, "pycodestyle")' '"pycodestyle"'
74+
'';
8275

8376
installPhase = ''
8477
cp -r . $out
8578
'';
86-
87-
meta = {
88-
homepage = "https://github.com/nexB/vulnerablecode";
89-
license = lib.licenses.asl20;
90-
};
9179
};
80+
9281
};
9382

9483
# Provide a nix-shell env to work with vulnerablecode.
9584
devShell = forAllSystems (system:
9685
with nixpkgsFor.${system};
97-
mkShell rec {
98-
# will be available as env var in `nix develop`
86+
mkShell {
87+
# will be available as env var in `nix develop` / `nix-shell`.
9988
VULNERABLECODE_INSTALL_DIR = vulnerablecode;
10089
buildInputs = [ vulnerablecode ];
10190
shellHook = ''
102-
alias vulnerablecode-manage.py=${VULNERABLECODE_INSTALL_DIR}/manage.py
103-
'';
104-
91+
alias vulnerablecode-manage.py=${vulnerablecode}/manage.py
92+
'';
10593
});
10694

10795
# Provide some packages for selected system types.
@@ -114,12 +102,12 @@
114102

115103
# Tests run by 'nix flake check' and by Hydra.
116104
checks = forAllSystems (system: {
117-
inherit (self.packages.${system}) vulnerablecode;
105+
inherit (self.packages.${system})
106+
;
118107

119-
# Additional tests, if applicable.
120-
vulnerablecode-pytest = with nixpkgsFor.${system};
108+
vulnerablecode-test = with nixpkgsFor.${system};
121109
stdenv.mkDerivation {
122-
name = "vulnerablecode-test-${version}";
110+
name = "${vulnerablecode.name}-test";
123111

124112
buildInputs = [ wget vulnerablecode ];
125113

@@ -129,33 +117,28 @@
129117

130118
unpackPhase = "true";
131119

132-
# Setup postgres, run migrations, run pytset and test-run the webserver.
133-
# See ${vulnerablecode}/README.md for the original instructions.
134-
# Notes:
135-
# - $RUNDIR is used to prevent postgres from accessings its default run dir at /run/postgresql.
136-
# See also https://github.com/NixOS/nixpkgs/issues/83770#issuecomment-607992517.
137-
# - pytest can only be run with an running postgres database server.
138120
buildPhase = ''
139-
DATADIR=$(pwd)/pgdata
140-
RUNDIR=$(pwd)/run
141-
ENCODING="UTF-8"
142-
mkdir -p $RUNDIR
143-
initdb -D $DATADIR -E $ENCODING
144-
pg_ctl -D $DATADIR -o "-k $RUNDIR" -l $DATADIR/logfile start
145-
createuser --host $RUNDIR --no-createrole --no-superuser --login --inherit --createdb vulnerablecode
146-
createdb --host $RUNDIR -E $ENCODING --owner=vulnerablecode --user=vulnerablecode --port=5432 vulnerablecode
147-
(
148-
export DJANGO_DEV=1
149-
${vulnerablecode}/manage.py migrate
150-
(cd ${vulnerablecode} && pytest)
151-
${vulnerablecode}/manage.py runserver &
152-
sleep 5
153-
${wget}/bin/wget http://127.0.0.1:8000/api/
154-
kill %1 # kill webserver
155-
)
121+
source ${libSh}
122+
initPostgres $(pwd)
123+
export DJANGO_DEV=1
124+
${vulnerablecode}/manage.py migrate
125+
'';
126+
127+
doCheck = true;
128+
checkPhase = ''
129+
# Run pytest on the installed version. A running postgres
130+
# database server is needed.
131+
(cd ${vulnerablecode} && pytest)
132+
133+
# Launch the webserver and call the API.
134+
${vulnerablecode}/manage.py runserver &
135+
sleep 2
136+
wget http://127.0.0.1:8000/api/
137+
kill %1 # kill background task (i.e. webserver)
156138
'';
157139

158-
installPhase = "mkdir -p $out";
140+
installPhase =
141+
"mkdir -p $out"; # make this derivation return success
159142
};
160143
});
161144
};

etc/nix/generate-poetry-files.sh

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)