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

70 lines
1.5 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 src/${mainModule}_settings
touch src/${mainModule}_settings/__init__.py
cat > src/${mainModule}_settings/mock.py << EOF
${mock}
EOF
cat > src/${mainModule}_settings/prod.py << EOF
${prod}
EOF
'';
pyproject = true;
dependencies = [ loadcredential ];
build-system = [ hatchling ];
}