-
Notifications
You must be signed in to change notification settings - Fork 338
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
docs: add django example #445
Conversation
I'm not sure if the I'm having a bit of trouble trying to figure out how to run If interested see example GitHub Action runs here rdmolony/devenv-django#1 against branch https://github.com/rdmolony/devenv-django/tree/reproduce-github-actions-passing-on-failing-test |
I refactored to use |
e82dccf
to
2cae5ec
Compare
Adapted https://github.com/cachix/devenv/blob/dfd9c4857c4e4b910b4270c6a35af9f9ba824917/examples/python-poetry/devenv.nix so using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, i like that this adds an example for a whole framework. Though the example is quite big and I think some of the parts could potentially moved to other modules if they're needed.
I'm hoping an example configuration for running a Django application would be just a few lines (like, languages.python.django.enable and enabling postgres).
examples/devenv-django/devenv.nix
Outdated
|
||
env = { | ||
PYTHON_VERSION = python_version; | ||
DATABASE_URL = "postgresql://${db_user}@${db_host}:${db_port}/${db_name}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be a nice addition to the postgres module to automatically set the database url. Though I don't know how standardized this envvar is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of digging shows that this style of database URL is used by ...
libpq
https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING ... source https://stackoverflow.com/questions/3582552/what-is-the-format-for-the-postgresql-connection-string-urlsqlalchemy
uses pretty much the same style but often includes the database driver likepostgresql+psycopg2://...
https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls- I use it here via https://github.com/jazzband/dj-database-url which is a dependency of https://github.com/joke2k/django-environ which is used here to load secrets from a
.env
file
I don't know how it applies to other databases but I'd guess it's common
I feel like we need to expose wait_for_db from devenv itself for this example to get really small. I'd love to merge this, but I'd like to make it minimal. Can we get rid of some of the stuff that's not really required (like gitignore, CI, etc). |
Let me know if you need anything further here or upstream re |
We now have |
I simplified the example, it's much simpler via
rowan@Windows:/mnt/c/Users/New User/Code/third-party/devenv/examples/python-django$ devenv test
• Overriding .devenv to .devenv.dPJ3fscqlnMO
• Building tests ...
error:
… while evaluating the attribute 'config'
at «github:cachix/devenv-nixpkgs/06fb0f1c643aee3ae6838dda3b37ef0abc3c763b»/lib/modules.nix:322:9:
321| options = checked options;
322| config = checked (removeAttrs config [ "_module" ]);
| ^
323| _module = checked (config._module);
… while calling the 'seq' builtin
at «github:cachix/devenv-nixpkgs/06fb0f1c643aee3ae6838dda3b37ef0abc3c763b»/lib/modules.nix:322:18:
321| options = checked options;
322| config = checked (removeAttrs config [ "_module" ]);
| ^
323| _module = checked (config._module);
… while calling the 'throw' builtin
at «github:cachix/devenv-nixpkgs/06fb0f1c643aee3ae6838dda3b37ef0abc3c763b»/lib/modules.nix:298:18:
297| ''
298| else throw baseMsg
| ^
299| else null;
error:
error: The option `devenv.dotfile' does not exist. Definition values:
- In `<unknown-file>': "/mnt/c/Users/New User/Code/third-party/devenv/examples/python-django/.devenv.dPJ3fscqlnMO"
✖ Command produced the following output:
✔ Building tests in 15.2s.
Error: × Command `/nix/store/l24rmcpdnachwhz4rjwb9d89m3gxfy5n-nix-2.21-devenv/bin/nix --show-trace --extra-experimental-features nix-command --extra-experimental-features flakes --option warn-dirty
│ false --option eval-cache false --keep-going --max-jobs 2 build .#devenv.test --no-link --print-out-paths` failed with with exit code 1 I previously used |
You need to delete your |
Thanks @domenkozar, the tests pass now, however, the behaviour doesn't match what I expect. I changed (I switched • Overriding .devenv to .devenv.bwFPAwU1VW4y
• Building tests ...
• Using Cachix: devenv
✔ Building tests in 6.0s.
• Building processes ...
✔ Building processes in 3.7s.
• Starting processes ...• Building shell ...
✔ Building shell in 3.8s.
• PID is 256663
• Stop: $ devenv processes stop
✔ Starting processes in 3.8s.
• Running tests ...
• Building shell ...
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'warning: unknown setting 'upgrade-nix-store-path-url'
warning: unknown setting 'upgrade-nix-store-path-url'
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'Installing dependencies from lock file
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'
No dependencies to install or update
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'[runserver ] • Building shell ...
[postgres ] The files belonging to this database system will be owned by user "rowan".
[postgres ] This user must also own the server process.
[postgres ]
[postgres ] The database cluster will be initialized with locale "C".
[postgres ] The default text search configuration will be set to "english".
[postgres ]
[postgres ] Data page checksums are disabled.
[postgres ]
[postgres ] creating directory /home/rowan/code/devenv/examples/python-django/.devenv.bwFPAwU1VW4y/state/postgres ... ok
[postgres ] creating subdirectories ... ok
[postgres ] selecting dynamic shared memory implementation ... posix
[postgres ] selecting default max_connections ... 100
[postgres ] selecting default shared_buffers ... 128MB
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'[postgres ] selecting default time zone ... Europe/London
[postgres ] creating configuration files ... ok
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'[postgres ] running bootstrap script ... ok
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'[runserver ] • Using Cachix: devenv
evaluating derivation 'git+file:///home/rowan/code/devenv?dir=examples/python-django#devShells.x86_64-linux.default'[postgres ] performing post-bootstrap initialization ... ok
[postgres ] syncing data to disk ... ok
[postgres ]
[postgres ]
[postgres ] Success. You can now start the database server using:
[postgres ]
[postgres ] /nix/store/ph0bg8kq0g47h6rczgifrrbnag1isb56-postgresql-15.6/bin/pg_ctl -D /home/rowan/code/devenv/examples/python-django/.devenv.bwFPAwU1VW4y/state/postgres -l logfile start
[postgres ]
[postgres ] initdb: warning: enabling "trust" authentication for local connections
[postgres ] initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
[postgres ]
[postgres ] PostgreSQL initdb process complete.
[postgres ]
[postgres ]
[postgres ] PostgreSQL is setting up the initial database.
[postgres ]
✔ Building shell in 3.2s.
[postgres ] waiting for server to start....2024-04-04 08:59:21.033 GMT [261579] LOG: starting PostgreSQL 15.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 13.2.0, 64-bit
[postgres ] 2024-04-04 08:59:21.041 GMT [261579] LOG: listening on Unix socket "/tmp/devenv-0480122/postgres/.s.PGSQL.5432"
[postgres ] 2024-04-04 08:59:21.051 GMT [261601] LOG: database system was shut down at 2024-04-04 08:59:20 GMT
[postgres ] 2024-04-04 08:59:21.063 GMT [261579] LOG: database system is ready to accept connections
evaluating derivation 'flake:nixpkgs#bashInteractive'[postgres ] done
[postgres ] server started
[postgres ] Checking presence of database: db
[postgres ] 0
[postgres ] Creating database: db
evaluating derivation 'flake:nixpkgs#bashInteractive'[postgres ] CREATE DATABASE
[postgres ] CREATE ROLE
[postgres ] waiting for server to shut down....2024-04-04 08:59:21.202 GMT [261579] LOG: received fast shutdown request
[postgres ] 2024-04-04 08:59:21.206 GMT [261579] LOG: aborting any active transactions
[postgres ] 2024-04-04 08:59:21.208 GMT [261579] LOG: background worker "logical replication launcher" (PID 261604) exited with exit code 1
[postgres ] 2024-04-04 08:59:21.212 GMT [261599] LOG: shutting down
[postgres ] 2024-04-04 08:59:21.216 GMT [261599] LOG: checkpoint starting: shutdown immediate
evaluating derivation 'flake:nixpkgs#bashInteractive'[postgres ] 2024-04-04 08:59:21.384 GMT [261599] LOG: checkpoint complete: wrote 922 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.024 s, sync=0.120 s, total=0.173 s; sync files=305, longest=0.051 s, average=0.001 s; distance=4226 kB, estimate=4226 kB
[postgres ] 2024-04-04 08:59:21.389 GMT [261579] LOG: database system is shut down
[postgres ] done
[postgres ] server stopped
evaluating derivation 'flake:nixpkgs#bashInteractive'[postgres ] 2024-04-04 08:59:21.465 GMT [259877] LOG: starting PostgreSQL 15.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 13.2.0, 64-bit
[postgres ] 2024-04-04 08:59:21.466 GMT [259877] LOG: listening on IPv4 address "127.0.0.1", port 5432
evaluating derivation 'flake:nixpkgs#bashInteractive'[postgres ] 2024-04-04 08:59:21.482 GMT [259877] LOG: listening on Unix socket "/tmp/devenv-0480122/postgres/.s.PGSQL.5432"
[postgres ] 2024-04-04 08:59:21.502 GMT [262148] LOG: database system was shut down at 2024-04-04 08:59:21 GMT
[postgres ] 2024-04-04 08:59:21.512 GMT [259877] LOG: database system is ready to accept connections
warning: unknown setting 'upgrade-nix-store-path-url'
warning: unknown setting 'upgrade-nix-store-path-url'
• Setting up shell environment ...
warning: unknown setting 'upgrade-nix-store-path-url'
warning: unknown setting 'upgrade-nix-store-path-url'
• Testing ...
✔ Running tests in 5.4s.
• Stopping process with PID 256663
Stopping processes...
• Tests passed :)
[postgres ] 2024-04-04 08:59:23.189 GMT [259877] LOG: received fast shutdown request
[postgres ] 2024-04-04 08:59:23.202 GMT [259877] LOG: aborting any active transactions
[postgres ] 2024-04-04 08:59:23.205 GMT [259877] LOG: background worker "logical replication launcher" (PID 262173) exited with exit code 1
[postgres ] 2024-04-04 08:59:23.205 GMT [262146] LOG: shutting down
[postgres ] 2024-04-04 08:59:23.221 GMT [262146] LOG: checkpoint starting: shutdown immediate
[postgres ] 2024-04-04 08:59:23.236 GMT [262146] PANIC: could not rename file "pg_logical/replorigin_checkpoint.tmp" to "pg_logical/replorigin_checkpoint": No such file or directory devenv up to launch Postgres & in a separate shell run devenv shell; python manage.py test then I see my test logs which correctly show my tests have passed or failed
Am I missing something here? |
Can you fix conflicts? |
4c4c776
to
5ef0591
Compare
I rebased on |
Co-authored-by: bobvanderlinden Co-authored-by: domenkozar
d8b8054
to
03ae5d6
Compare
Co-authored-by: Domen Kožar <domen@enlambda.com>
Thank you! |
Hey there,
I created an example of using
devenv
alongsideDjango
viapoetry
including running unit tests by starting the database in the background.Let me know if you find this useful & if so, if you have any feedback
There's also a corresponding repo - https://github.com/rdmolony/devenv-django