diff options
author | Minijackson <minijackson@riseup.net> | 2022-12-20 10:18:33 +0100 |
---|---|---|
committer | Raito Bezarius <masterancpp@gmail.com> | 2023-04-04 11:45:29 +0200 |
commit | 2cb6dc90acfa16a37c055f6786de06a031f12326 (patch) | |
tree | 2f5c588ede3d591f9cedb681c891e244d040db5c /pkgs/pkgs-lib | |
parent | bc4b08acbe7f40ecdf0cabb90d8833092e6c233d (diff) |
formats.pythonVars: init
Diffstat (limited to 'pkgs/pkgs-lib')
-rw-r--r-- | pkgs/pkgs-lib/formats.nix | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix index 13aada3681f7c..3a47d3dc849ce 100644 --- a/pkgs/pkgs-lib/formats.nix +++ b/pkgs/pkgs-lib/formats.nix @@ -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 + '') {}; + }; + } |