|
2 | 2 | description = |
3 | 3 | "Vulnerablecode - A free and open vulnerabilities database and the packages they impact."; |
4 | 4 |
|
5 | | - # Nixpkgs / NixOS version to use. |
6 | 5 | inputs.nixpkgs = { |
7 | 6 | type = "github"; |
8 | 7 | owner = "NixOS"; |
9 | 8 | repo = "nixpkgs"; |
10 | 9 | ref = "20.09"; |
11 | 10 | }; |
12 | 11 |
|
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 }: |
14 | 20 | let |
15 | 21 |
|
16 | 22 | vulnerablecode-src = ./../..; |
|
19 | 25 | version = builtins.head (builtins.match ''.*version=["']?([^"',]+).*'' |
20 | 26 | (builtins.readFile (vulnerablecode-src + "/setup.py"))); |
21 | 27 |
|
22 | | - # From commit 7f8ae6399b02b1d508689b303f117e2f03f7854a |
23 | | - expectedRequirementstxtMd5sum = "7ea5fec4096b9c532450d68fad721017"; |
| 28 | + libSh = ./lib.sh; |
24 | 29 |
|
25 | 30 | # System types to support. |
26 | 31 | supportedSystems = [ "x86_64-linux" ]; |
|
36 | 41 | overlays = [ self.overlay ]; |
37 | 42 | }); |
38 | 43 |
|
| 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 | + |
39 | 51 | in { |
40 | 52 |
|
41 | 53 | # A Nixpkgs overlay. |
42 | 54 | overlay = final: prev: |
43 | 55 | with final.pkgs; { |
44 | 56 |
|
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 | + }; |
63 | 62 |
|
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}"; |
66 | 66 | 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 | | - |
80 | 67 | 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 | + ''; |
82 | 75 |
|
83 | 76 | installPhase = '' |
84 | 77 | cp -r . $out |
85 | 78 | ''; |
86 | | - |
87 | | - meta = { |
88 | | - homepage = "https://github.com/nexB/vulnerablecode"; |
89 | | - license = lib.licenses.asl20; |
90 | | - }; |
91 | 79 | }; |
| 80 | + |
92 | 81 | }; |
93 | 82 |
|
94 | 83 | # Provide a nix-shell env to work with vulnerablecode. |
95 | 84 | devShell = forAllSystems (system: |
96 | 85 | 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`. |
99 | 88 | VULNERABLECODE_INSTALL_DIR = vulnerablecode; |
100 | 89 | buildInputs = [ vulnerablecode ]; |
101 | 90 | shellHook = '' |
102 | | - alias vulnerablecode-manage.py=${VULNERABLECODE_INSTALL_DIR}/manage.py |
103 | | - ''; |
104 | | - |
| 91 | + alias vulnerablecode-manage.py=${vulnerablecode}/manage.py |
| 92 | + ''; |
105 | 93 | }); |
106 | 94 |
|
107 | 95 | # Provide some packages for selected system types. |
|
114 | 102 |
|
115 | 103 | # Tests run by 'nix flake check' and by Hydra. |
116 | 104 | checks = forAllSystems (system: { |
117 | | - inherit (self.packages.${system}) vulnerablecode; |
| 105 | + inherit (self.packages.${system}) |
| 106 | + ; |
118 | 107 |
|
119 | | - # Additional tests, if applicable. |
120 | | - vulnerablecode-pytest = with nixpkgsFor.${system}; |
| 108 | + vulnerablecode-test = with nixpkgsFor.${system}; |
121 | 109 | stdenv.mkDerivation { |
122 | | - name = "vulnerablecode-test-${version}"; |
| 110 | + name = "${vulnerablecode.name}-test"; |
123 | 111 |
|
124 | 112 | buildInputs = [ wget vulnerablecode ]; |
125 | 113 |
|
|
129 | 117 |
|
130 | 118 | unpackPhase = "true"; |
131 | 119 |
|
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. |
138 | 120 | 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) |
156 | 138 | ''; |
157 | 139 |
|
158 | | - installPhase = "mkdir -p $out"; |
| 140 | + installPhase = |
| 141 | + "mkdir -p $out"; # make this derivation return success |
159 | 142 | }; |
160 | 143 | }); |
161 | 144 | }; |
|
0 commit comments