Skip to content

Commit 09dcbbf

Browse files
authored
Merge pull request #635 from ngi-nix/fix-nix-setup
Fix nix setup
2 parents 4390b6a + f46de02 commit 09dcbbf

5 files changed

Lines changed: 53 additions & 66 deletions

File tree

.github/workflows/test-import-using-nix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
cd etc/nix
2828
./get-latest-pypi-deps-db.sh --in-place
2929
nix --print-build-logs flake check
30-
./test-import-using-nix.sh alpine
30+
nix-shell --run ./test-import-using-nix.sh

etc/nix/flake.lock

Lines changed: 9 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/nix/flake.nix

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
type = "github";
1414
owner = "DavHau";
1515
repo = "mach-nix";
16-
ref = "235a0a81d05a043bca2a93442f2560946266fc73";
16+
ref = "fe5255e6fd8df57e9507b7af82fc59dda9e9ff2b"; # 3.4.0
1717
};
1818

1919
outputs = { self, nixpkgs, machnix }:
@@ -29,9 +29,6 @@
2929
version = builtins.head (builtins.match ''.*version=["']?([^"',]+).*''
3030
(builtins.readFile (vulnerablecode-src + "/setup.py")));
3131

32-
# Common shell code.
33-
libSh = ./lib.sh;
34-
3532
# System types to support.
3633
supportedSystems = [ "x86_64-linux" ];
3734

@@ -57,9 +54,10 @@
5754
# mach-nix release) is usually insufficient. Use
5855
# ./get-latest-pypi-deps-db.sh to obtain the data rev & hash.
5956
pypiDataRev =
60-
"8dcec158c51f8a96f316630679222e436c1b078c"; # 2021-06-16T08:41:20Z
57+
"897a7471aa4e83aab21d2c501e00fee3f440e0fe"; # 2022-02-21T08:57:22Z
6158
pypiDataSha256 =
62-
"0499zl39aia74f0i7fkn5dsy8244dkmcw4vzd5nf4kai605j2jli";
59+
"03gnaq687gg9afb6i6czw4kzr1gbnzna15lfb26f9nszyfq3iyaj";
60+
6361
});
6462
# This wrapper allows to setup both the production as well as the
6563
# development Python environments in the same way (albeit having
@@ -69,11 +67,6 @@
6967
requirements = ''
7068
${requirements}
7169
'';
72-
# Fix an issue with an upstream dep of GitPython.
73-
# https://github.com/DavHau/mach-nix/issues/287
74-
# See https://github.com/DavHau/mach-nix/issues/318
75-
_.gitpython.propagatedBuildInputs.mod = pySelf: self: oldVal:
76-
oldVal ++ [ pySelf.typing-extensions ];
7770
};
7871

7972
in {
@@ -91,12 +84,6 @@
9184
dontBuild = true; # do not use Makefile
9285
propagatedBuildInputs = [ pythonEnv postgresql gitMinimal ];
9386

94-
postPatch = ''
95-
# Do not use absolute path.
96-
substituteInPlace vulnerablecode/settings.py \
97-
--replace 'STATIC_ROOT = "/var/vulnerablecode/static"' 'STATIC_ROOT = "./static"'
98-
'';
99-
10087
installPhase = ''
10188
cp -r . $out
10289
'';
@@ -144,25 +131,24 @@
144131
unpackPhase = "true";
145132

146133
buildPhase = ''
147-
source ${libSh}
148-
initPostgres $(pwd)
149-
export SECRET_KEY=REALLY_SECRET
150-
${vulnerablecode}/manage.py collectstatic --no-input
151-
${vulnerablecode}/manage.py migrate
134+
# Work on a local copy.
135+
cp -r ${vulnerablecode} ./vulnerablecode
136+
cd ./vulnerablecode
137+
chmod -R +w .
138+
139+
source ./etc/nix/lib.sh
140+
141+
setupDevEnv
152142
'';
153143

154144
doCheck = true;
155145
checkPhase = ''
156-
# Run pytest on the installed version. A running postgres
157-
# database server is needed.
158-
(
159-
cd ${vulnerablecode}
160-
black -l 100 --check .
161-
pytest -m "not webtest"
162-
)
146+
export PYTHON_EXE=${pythonEnvDev}/bin/python3 # use correct python
147+
make check
148+
make test
163149
164150
# Launch the webserver and call the API.
165-
${vulnerablecode}/manage.py runserver &
151+
make run &
166152
sleep 2
167153
wget http://127.0.0.1:8000/api/
168154
kill %1 # kill background task (i.e. webserver)

etc/nix/lib.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/usr/bin/env bash
22

3-
# Setup postgres; see the README for the latest instructions.
4-
#
5-
# $RUNDIR is used to prevent postgres from accessings its default run dir at
6-
# /run/postgresql. See
7-
# https://github.com/NixOS/nixpkgs/issues/83770#issuecomment-607992517
8-
function initPostgres() {
9-
ROOTDIR=$1
10-
DATADIR=$ROOTDIR/pgdata
11-
RUNDIR=$ROOTDIR/run
12-
ENCODING="UTF-8"
13-
mkdir -p "$RUNDIR"
14-
initdb -D "$DATADIR" -E $ENCODING
15-
pg_ctl -D "$DATADIR" -o "-k $RUNDIR" -l "$DATADIR/logfile" start
16-
createuser --host "$RUNDIR" --no-createrole --no-superuser --login --inherit --createdb vulnerablecode
17-
createdb --host "$RUNDIR" -E $ENCODING --owner=vulnerablecode --user=vulnerablecode --port=5432 vulnerablecode
3+
# Setup dev environment; see the README for the latest instructions.
4+
setupDevEnv() {
5+
# Make sure postgres uses a local socket file. The posgres
6+
# commands (initd,b createdb, createuser, etc.) honor these
7+
# settings.
8+
export PGHOST=$PWD
9+
export PGDATA=./pgdata
10+
# Start postgres.
11+
initdb -E utf-8
12+
pg_ctl -o "-k $PGHOST" -l ./logfile start
13+
14+
# Setup dev environment.
15+
export ACTIVATE= # no venv
16+
sed -i 's/sudo -u postgres//' Makefile # no extra user
17+
make envfile postgres
1818
}

etc/nix/test-import-using-nix.sh

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44
# Populate a test database using either the Nix installation or the local
55
# checkout.
66

7-
set -e
7+
set -exv
88

99
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
1010
DEFAULT_INSTALL_DIR=$VULNERABLECODE_INSTALL_DIR # in the Nix store, see flake.nix
1111
INSTALL_DIR=${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}
1212
ARGS=$(if [ $# -eq 0 ]; then echo "--all"; else echo "$@"; fi)
13-
export SECRET_KEY=REALLY_SECRET
14-
TEMPDIR=$(mktemp -d -p "$THIS_DIR")
15-
export TEMPDIR
1613

1714
source "$THIS_DIR/lib.sh"
1815

1916
cleanup() {
20-
pg_ctl -D "$DATADIR" stop
17+
pg_ctl stop
2118
rm -rf "$TEMPDIR"
2219
}
2320

2421
trap cleanup EXIT
2522

26-
initPostgres "$TEMPDIR"
27-
28-
"$INSTALL_DIR/manage.py" migrate
29-
"$INSTALL_DIR/manage.py" collectstatic --no-input
30-
"$INSTALL_DIR/manage.py" import $ARGS
23+
TEMPDIR=$(mktemp -d -p "$THIS_DIR")
24+
cp -r "$INSTALL_DIR" "$TEMPDIR/vulnerablecode"
25+
cd "$TEMPDIR/vulnerablecode"
26+
chmod -R +w .
27+
setupDevEnv
28+
29+
./manage.py migrate
30+
./manage.py collectstatic --no-input
31+
./manage.py import $ARGS
32+
./manage.py improve $ARGS

0 commit comments

Comments
 (0)