All Projects → DavHau → django-nixos

DavHau / django-nixos

Licence: MIT License
NixOS/NixOps configuration for Django

Programming Languages

Nix
1067 projects

Projects that are alternatives of or similar to django-nixos

Morph
NixOS deployment tool
Stars: ✭ 303 (+1342.86%)
Mutual labels:  nix, deployment, nixos
Deploy Rs
A simple multi-profile Nix-flake deploy tool.
Stars: ✭ 164 (+680.95%)
Mutual labels:  nix, deployment, nixos
nixops-tutorial
Tutorial for practical deployments with NixOps
Stars: ✭ 93 (+342.86%)
Mutual labels:  nix, nixops, nixos
nyx
⚙️Nix[OS] Configuration
Stars: ✭ 50 (+138.1%)
Mutual labels:  nix, nixos
triton
Triton Operating System
Stars: ✭ 56 (+166.67%)
Mutual labels:  nix, nixos
nix-bisect
Bisect nix builds. Status: alpha/proof of concept. You'll probably have to dig into the implementation if you want to use it. Built for personal use, lightly maintained. PRs welcome. Issues welcome, but I make no promises regarding responses or fix
Stars: ✭ 72 (+242.86%)
Mutual labels:  nix, nixos
nix-rice
A library to functionally define your configuration and theme (rice) with Nix
Stars: ✭ 43 (+104.76%)
Mutual labels:  nix, nixos
dotfiles
NixOS system config & Home-Manager user config
Stars: ✭ 43 (+104.76%)
Mutual labels:  nix, nixos
nixpkgs-python-importer
Violate Nix philosophy, install Python packages mid-session with `from nixpkgs.scipy import scipy`.
Stars: ✭ 27 (+28.57%)
Mutual labels:  nix, nixos
nix-new-rails-app
Initialize Rails applications using Nix and development environments (nix-shell)
Stars: ✭ 27 (+28.57%)
Mutual labels:  nix, nixos
nixcrates
DEPRECATED reads rust-lang/crates.io-index and outputs nix expressions into fractalide/nix-crates-index
Stars: ✭ 14 (-33.33%)
Mutual labels:  nix, nixos
nixos-on-arm
Cross Compiling NixOS to ARM as a replacement for Yocto
Stars: ✭ 129 (+514.29%)
Mutual labels:  nix, nixos
deadnix
Scan Nix files for dead code
Stars: ✭ 121 (+476.19%)
Mutual labels:  nix, nixos
homeage
runtime decrypted age secrets for nix home manager
Stars: ✭ 43 (+104.76%)
Mutual labels:  nix, nixos
nixos-tutorial
one hour, hands-on
Stars: ✭ 118 (+461.9%)
Mutual labels:  nix, nixos
nixery
Container registry which transparently builds images using the Nix package manager. Canonical repository is https://cs.tvl.fyi/depot/-/tree/tools/nixery
Stars: ✭ 1,365 (+6400%)
Mutual labels:  nix, nixos
dotfiles
No place like ~. Nix. All. The. Things.
Stars: ✭ 48 (+128.57%)
Mutual labels:  nix, nixos
dotfiles
Dotfiles for my NixOS system based on Dracula theme
Stars: ✭ 39 (+85.71%)
Mutual labels:  nix, nixos
nixos-config
My NixOS configuration
Stars: ✭ 23 (+9.52%)
Mutual labels:  nix, nixos
nixdots
I have no idea what the hell I'm doing
Stars: ✭ 46 (+119.05%)
Mutual labels:  nix, nixos

NixOS-based Django deployment

!! WARNING !! This project has not been updated for a while. You can still use this as a template, but make sure to update the nixpkgs version in nixpkgs-src.nix

This Project aims to provide a production grade NixOS configuration for Django projects. By taking your source code and some parameters as input it will return a nixos configuration which serves your Django project.

An exemplary django project with some example NixOS/NixOps configs can be found under ./examples

What you will get

  • A PostgreSQL DB with access configured for django
  • A systemd service which serves the project via gunicorn
  • A defined way of passing secrets to Django without leaking them into /nix/store
  • Your static files as a separated build artifact (by default served via whitenoise)
  • Ability to configure some common options like (allowed-hosts, port, processes, threads) through your nix config.
  • Having your manage.py globally callable via manage-projectname (only via root/sudo)

Parameters

{ # MANDATORY
  name,  # create a name for the project
  keys-file,  # path to a file containing secrets
  src,  # derivation of django source code

  # OPTIONAL
  settings, # django settings module like `myproject.settings`
  pkgs ? import ./nixpkgs-src.nix { config = {}; },  # nixpkgs
  python ? import ./python.nix { inherit pkgs; },  # python + modules
  manage-py ? "${src}/manage.py",  # path to manage.py inside src
  static-files ? (import ./static-files.nix { # derivation of static files
    inherit pkgs python src settings name manage-py;
  }),
  wsgi ? "${name}.wsgi",  # django wsgi module like `myproject.wsgi`
  processes ? 5,  # number of proccesses for gunicorn server
  threads ? 5,  # number of threads for gunicorn server
  db-name ? name,  # database name
  user ? "django",  # system user for django
  port ? 80,  # port to bind the http server
  allowed-hosts ? "*",  # string of comma separated hosts
  ...
}:

Prerequisites

Django settings must be configured to:

  • load SECRET_KEY and STATIC_ROOT from the environment:
    SECRET_KEY=environ.get('SECRET_KEY')
    STATIC_ROOT=environ.get('STATIC_ROOT')
  • load ALLOWED_HOSTS from a comma separated list environment variable:
    ALLOWED_HOSTS = list(environ.get('ALLOWED_HOSTS', default='').split(','))
  • use exactly this DATABASES configuration:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': environ.get('DB_NAME'),
            'HOST': '',
        }
    }

To serve static files out of the box, include the whitenoise middleware:

MIDDLEWARE += [ 'whitenoise.middleware.WhiteNoiseMiddleware' ]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

(See ./examples/djangoproject/djangoproject/settings_nix.py for full example)

Secrets / Keys

To pass secrets to django securely:

  1. Create a file containing your secrets as environment variables like this:
    export SECRET_KEY="foo"
    export ANOTHER_SECRET_FOR_DJANGO="bar"
    
  2. Pass the path of the file via parameter keys-file
    This file will not be managed by nix. If you are deploying to a remote host, make sure this file is available. An example on how to do this with NixOps can be found under ./examples/nixops

A systemd service running as root will later pick up that file and copy it to a destination under /run/ where only the django system user can read it. Make sure by yourself to protect the source file you uploaded to the remote host with proper permissions or use the provided NixOps example.

Examples

See Readme.md inside ./examples

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].