hackens-org-configurations/machines/org/orga/mkAssets.nix
2023-12-19 13:27:58 +01:00

27 lines
1.2 KiB
Nix

{ pkgs, settings, source, app }:
let
manage-py-file = "${source}/${app}/manage.py";
python = import ./python.nix { inherit pkgs; };
static-assets = pkgs.callPackage ./static-assets.nix { inherit python source app; envPrefix = "HACKENS_ORGA_"; };
mkEnv = settings: let # make env file to source before using manage.py and other commands
lib = pkgs.lib;
mkVarVal = v: let
isHasAttr = s: lib.isAttrs v && lib.hasAttr s v;
in
if builtins.isString v then v
else if builtins.isList v && lib.any lib.strings.isConvertibleWithToString v then (lib.concatMapStringsSep "," toString v)
else if builtins.isInt v then toString v
else if builtins.isBool v then toString (if v then 1 else 0)
else if isHasAttr "_file" then "$(cat ${v._file} | xargs)"
else if isHasAttr "_raw" then v._raw
else abort "The django conf value ${lib.generators.toPretty {} v} can not be encoded.";
in lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: "export ${k}=${mkVarVal v}") settings);
envFile = pkgs.writeScript "django-${app}-env.sh" (mkEnv settings);
managePy = pkgs.writeScript "manage-${app}" ''
source ${envFile}
${python}/bin/python ${manage-py-file} $@
'';
in
{
inherit managePy static-assets envFile source python;
}