|
| 1 | +{ |
| 2 | + description = |
| 3 | + "Vulnerablecode - A free and open vulnerabilities database and the packages they impact."; |
| 4 | + |
| 5 | + inputs.nixpkgs = { |
| 6 | + type = "github"; |
| 7 | + owner = "NixOS"; |
| 8 | + repo = "nixpkgs"; |
| 9 | + ref = "20.09"; |
| 10 | + }; |
| 11 | + |
| 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 }: |
| 20 | + let |
| 21 | + |
| 22 | + vulnerablecode-src = ./../..; |
| 23 | + |
| 24 | + # Extract version from setup.py. |
| 25 | + version = builtins.head (builtins.match ''.*version=["']?([^"',]+).*'' |
| 26 | + (builtins.readFile (vulnerablecode-src + "/setup.py"))); |
| 27 | + |
| 28 | + # Common shell code. |
| 29 | + libSh = ./lib.sh; |
| 30 | + |
| 31 | + # System types to support. |
| 32 | + supportedSystems = [ "x86_64-linux" ]; |
| 33 | + |
| 34 | + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. |
| 35 | + forAllSystems = f: |
| 36 | + nixpkgs.lib.genAttrs supportedSystems (system: f system); |
| 37 | + |
| 38 | + # Nixpkgs instantiated for supported system types. |
| 39 | + nixpkgsFor = forAllSystems (system: |
| 40 | + import nixpkgs { |
| 41 | + inherit system; |
| 42 | + overlays = [ self.overlay ]; |
| 43 | + }); |
| 44 | + |
| 45 | + # mach-nix instantiated for supported system types. |
| 46 | + machnixFor = forAllSystems (system: |
| 47 | + import machnix { |
| 48 | + pkgs = (nixpkgsFor.${system}).pkgs; |
| 49 | + python = "python38"; |
| 50 | + |
| 51 | + # Pin pypi repo to a specific commit which includes all necessary |
| 52 | + # Python deps. The default version is updated with every mach-nix |
| 53 | + # release might be be sufficient for newer releases. |
| 54 | + # The corresponding sha256 hash can be obtained with: |
| 55 | + # $ nix-prefetch-url --unpack https://github.com/DavHau/pypi-deps-db/tarball/<pypiDataRev> |
| 56 | + pypiDataRev = "c86b4490a7d838bd54a2d82730455e96c6e4eb14"; |
| 57 | + pypiDataSha256 = |
| 58 | + "0al490gi0qda1nkb9289z2msgpc633rv5hn3w5qihkl1rh88dmjd"; |
| 59 | + }); |
| 60 | + |
| 61 | + in { |
| 62 | + |
| 63 | + # A Nixpkgs overlay. |
| 64 | + overlay = final: prev: |
| 65 | + with final.pkgs; { |
| 66 | + |
| 67 | + pythonEnv = machnixFor.${system}.mkPython { |
| 68 | + requirements = |
| 69 | + builtins.readFile (vulnerablecode-src + "/requirements.txt"); |
| 70 | + }; |
| 71 | + |
| 72 | + vulnerablecode = stdenv.mkDerivation { |
| 73 | + inherit version; |
| 74 | + name = "vulnerablecode-${version}"; |
| 75 | + src = vulnerablecode-src; |
| 76 | + dontConfigure = true; # do not use ./configure |
| 77 | + propagatedBuildInputs = [ pythonEnv postgresql ]; |
| 78 | + |
| 79 | + postPatch = '' |
| 80 | + # Make sure the pycodestyle binary in $PATH is used. |
| 81 | + substituteInPlace vulnerabilities/tests/test_basics.py \ |
| 82 | + --replace 'join(bin_dir, "pycodestyle")' '"pycodestyle"' |
| 83 | + ''; |
| 84 | + |
| 85 | + installPhase = '' |
| 86 | + cp -r . $out |
| 87 | + ''; |
| 88 | + }; |
| 89 | + |
| 90 | + }; |
| 91 | + |
| 92 | + # Provide a nix-shell env to work with vulnerablecode. |
| 93 | + devShell = forAllSystems (system: |
| 94 | + with nixpkgsFor.${system}; |
| 95 | + mkShell { |
| 96 | + # will be available as env var in `nix develop` / `nix-shell`. |
| 97 | + VULNERABLECODE_INSTALL_DIR = vulnerablecode; |
| 98 | + buildInputs = [ vulnerablecode ]; |
| 99 | + shellHook = '' |
| 100 | + alias vulnerablecode-manage.py=${vulnerablecode}/manage.py |
| 101 | + ''; |
| 102 | + }); |
| 103 | + |
| 104 | + # Provide some packages for selected system types. |
| 105 | + packages = forAllSystems |
| 106 | + (system: { inherit (nixpkgsFor.${system}) vulnerablecode; }); |
| 107 | + |
| 108 | + # The default package for 'nix build'. |
| 109 | + defaultPackage = |
| 110 | + forAllSystems (system: self.packages.${system}.vulnerablecode); |
| 111 | + |
| 112 | + # Tests run by 'nix flake check' and by Hydra. |
| 113 | + checks = forAllSystems (system: { |
| 114 | + inherit (self.packages.${system}) vulnerablecode; |
| 115 | + |
| 116 | + vulnerablecode-test = with nixpkgsFor.${system}; |
| 117 | + stdenv.mkDerivation { |
| 118 | + name = "${vulnerablecode.name}-test"; |
| 119 | + |
| 120 | + buildInputs = [ wget vulnerablecode ]; |
| 121 | + |
| 122 | + # Used by pygit2. |
| 123 | + # See https://github.com/NixOS/nixpkgs/pull/72544#issuecomment-582674047. |
| 124 | + SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; |
| 125 | + |
| 126 | + unpackPhase = "true"; |
| 127 | + |
| 128 | + buildPhase = '' |
| 129 | + source ${libSh} |
| 130 | + initPostgres $(pwd) |
| 131 | + export DJANGO_DEV=1 |
| 132 | + ${vulnerablecode}/manage.py migrate |
| 133 | + ''; |
| 134 | + |
| 135 | + doCheck = true; |
| 136 | + checkPhase = '' |
| 137 | + # Run pytest on the installed version. A running postgres |
| 138 | + # database server is needed. |
| 139 | + (cd ${vulnerablecode} && pytest) |
| 140 | +
|
| 141 | + # Launch the webserver and call the API. |
| 142 | + ${vulnerablecode}/manage.py runserver & |
| 143 | + sleep 2 |
| 144 | + wget http://127.0.0.1:8000/api/ |
| 145 | + kill %1 # kill background task (i.e. webserver) |
| 146 | + ''; |
| 147 | + |
| 148 | + installPhase = |
| 149 | + "mkdir -p $out"; # make this derivation return success |
| 150 | + }; |
| 151 | + }); |
| 152 | + }; |
| 153 | +} |
0 commit comments