mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-10-30 14:11:21 +00:00
formats.pythonVars: init
This commit is contained in:
parent
bc4b08acbe
commit
2cb6dc90ac
@ -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 New Issue
Block a user