formats.pythonVars: init
This commit is contained in:
parent
bc4b08acbe
commit
2cb6dc90ac
1 changed files with 35 additions and 0 deletions
|
@ -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
|
||||
'') {};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue