formats.pythonVars: init

This commit is contained in:
Minijackson
2023-04-04 11:45:29 +02:00
committed by Raito Bezarius
parent bc4b08acbe
commit 2cb6dc90ac
+35
View File
@@ -417,4 +417,39 @@ rec {
'';
};
# Outputs a succession of Python variable assignments
# Useful for many Django-based services
pythonVars = {}: {
type = with lib.types; let
valueType = nullOr(oneOf [
bool
float
int
path
str
(attrsOf valueType)
(listOf valueType)
]) // {
description = "Python value";
};
in attrsOf valueType;
generate = name: value: pkgs.callPackage ({ runCommand, python3, black }: runCommand name {
nativeBuildInputs = [ python3 black ];
value = builtins.toJSON value;
pythonGen = ''
import json
import os
with open(os.environ["valuePath"], "r") as f:
for key, value in json.load(f).items():
print(f"{key} = {repr(value)}")
'';
passAsFile = [ "value" "pythonGen" ];
} ''
cat "$valuePath"
python3 "$pythonGenPath" > $out
black $out
'') {};
};
}