djangonix/utils/mkSettingsModule.nix
2024-06-09 20:15:40 +02:00

70 lines
1.4 KiB
Nix

{
lib,
buildPythonPackage,
hatchling,
loadcredential,
runtimeSettings,
settings,
secrets,
mainModule,
extraConfig,
}:
let
secretsImport = lib.mapAttrsToList (k: _: "${k} = credentials.get_json(\"${k}\")") secrets;
runtimeImport = builtins.map (k: "${k} = credentials.get_json(\"${k}\")") runtimeSettings;
settingsImport = lib.mapAttrsToList (k: v: "${k} = json.loads('${builtins.toJSON v}')") settings;
prod = ''
from loadcredential import Credentials
from .mock import *
credentials = Credentials()
${lib.concatLines secretsImport}
${extraConfig}
'';
mock = ''
import json
from ${mainModule}.settings import *
from loadcredential import Credentials
credentials = Credentials()
${lib.concatLines settingsImport}
${lib.concatLines runtimeImport}
'';
in
buildPythonPackage {
name = "${mainModule}_settings";
unpackPhase = ''
cat > pyproject.toml << EOF
[project]
name = "${mainModule}_settings"
version="0.0.1"
dependencies = [
"loadcredential"
]
[build-system]
build-backend = "hatchling.build"
requires = ["hatchling"]
EOF
mkdir -p ${mainModule}_settings
touch ${mainModule}_settings/__init__.py
cat > ${mainModule}_settings/mock.py << EOF
${mock}
EOF
cat > ${mainModule}_settings/prod.py << EOF
${prod}
EOF
'';
pyproject = true;
dependencies = [ loadcredential ];
build-system = [ hatchling ];
}