Skip to content

Instantly share code, notes, and snippets.

@nbren12
Created January 4, 2021 23:07
Show Gist options
  • Save nbren12/81ba7810250b1b1c008c11574dc15a3b to your computer and use it in GitHub Desktop.
Save nbren12/81ba7810250b1b1c008c11574dc15a3b to your computer and use it in GitHub Desktop.
with import <nixpkgs> {};
let
python = python3.withPackages (ps: [ps.cffi]);
in
stdenv.mkDerivation {
name = "example";
inherit python;
buildInputs = [ stdenv python ];
src = ./.;
doCheck = true;
doInstall = false;
installPhase = "";
}
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
int err;
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
PyRun_SimpleString("print('Importing cffi')\n");
err = PyRun_SimpleString("import cffi");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
exit(err);
return 0;
}
all:
${CC} -lpython3.8 -I${python}/include/python3.8 main.c
check:
./a.out
install:
mkdir -p ${out}/bin
cp a.out ${out}/bin/main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment