From 3f36a68d94a57257ded223374de69adab74b5917 Mon Sep 17 00:00:00 2001 From: thomasjm Date: Fri, 25 Oct 2024 02:58:24 -0700 Subject: [PATCH] xeus-cling: wrap with library args and add smoke check --- .../jupyter-kernels/xeus-cling/default.nix | 114 ++++++++++++------ .../jupyter-kernels/xeus-cling/test.ipynb | 24 ++++ pkgs/top-level/all-packages.nix | 3 +- 3 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 pkgs/applications/editors/jupyter-kernels/xeus-cling/test.ipynb diff --git a/pkgs/applications/editors/jupyter-kernels/xeus-cling/default.nix b/pkgs/applications/editors/jupyter-kernels/xeus-cling/default.nix index 4158fc3dff0a2..b4f37ffd5fa29 100644 --- a/pkgs/applications/editors/jupyter-kernels/xeus-cling/default.nix +++ b/pkgs/applications/editors/jupyter-kernels/xeus-cling/default.nix @@ -1,6 +1,9 @@ { callPackage , cling , fetchurl +, jq +, makeWrapper +, python3 , stdenv }: @@ -11,48 +14,81 @@ # nix run --impure --expr 'with import {}; jupyter.override { definitions = { cpp17 = cpp17-kernel; }; }' let - xeus-cling = callPackage ./xeus-cling.nix {}; - - mkDefinition = std: - let - versionSuffix = - if std == "c++11" then " 11" - else if std == "c++14" then " 14" - else if std == "c++17" then " 17" - else if std == "c++17" then " 17" - else if std == "c++2a" then " 2a" - else throw "Unexpected C++ std for cling: ${std}"; - in + xeus-cling-unwrapped = callPackage ./xeus-cling.nix {}; + + xeus-cling = xeus-cling-unwrapped.overrideAttrs (oldAttrs: { + nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [makeWrapper]; + + # xcpp needs a collection of flags to start up properly, so wrap it by default. + # We'll provide the unwrapped version as a passthru + flags = cling.flags ++ [ + "-resource-dir" "${cling.unwrapped}" + "-L" "${cling.unwrapped}/lib" + "-l" "${cling.unwrapped}/lib/cling.so" + ]; + + fixupPhase = '' + runHook preFixup + + wrapProgram $out/bin/xcpp --add-flags "$flags" + + runHook postFixup + ''; + + doInstallCheck = true; + installCheckPhase = '' + runHook preCheck + + # Smoke check: run a test notebook using Papermill by creating a simple kernelspec + mkdir -p kernels/cpp17 + export JUPYTER_PATH="$(pwd)" + cat << EOF > kernels/cpp17/kernel.json { - displayName = "C++" + versionSuffix; - argv = [ - "${xeus-cling}/bin/xcpp" - ] - ++ cling.flags - ++ [ - "-resource-dir" "${cling.unwrapped}" - "-L" "${cling.unwrapped}/lib" - "-l" "${cling.unwrapped}/lib/cling.so" - "-std=${std}" - # "-v" - "-f" "{connection_file}" - ]; - language = "cpp"; - logo32 = fetchurl { - url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/32px-ISO_C%2B%2B_Logo.svg.png"; - hash = "sha256-cr0TB8/j2mkcFhfCkz9F7ZANOuTlWA2OcWtDcXyOjHw="; - }; - logo64 = fetchurl { - url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/64px-ISO_C%2B%2B_Logo.svg.png"; - hash = "sha256-nZtJ4bR7GmQttvqEJC9KejOxphrjjxT36L9yOIITFLk="; - }; - }; + "argv": ["$out/bin/xcpp", "-std=c++17", "-f", "{connection_file}"], + "language": "cpp17" + } + EOF + + ${python3.pkgs.papermill}/bin/papermill ${./test.ipynb} out.ipynb + result="$(cat out.ipynb | ${jq}/bin/jq -r '.cells[0].outputs[0].text[0]')" + if [[ "$result" != "Hello world." ]]; then + echo "Kernel test gave '$result'. Expected: 'Hello world.'" + exit 1 + fi + + runHook postCheck + ''; + + passthru = (oldAttrs.passthru or {}) // { + unwrapped = xeus-cling-unwrapped; + }; + }); + + mkKernelSpec = std: { + displayName = builtins.replaceStrings ["c++"] ["C++ "] std; + argv = [ + "${xeus-cling}/bin/xcpp" + "-std=${std}" + "-f" "{connection_file}" + ]; + language = "cpp"; + logo32 = fetchurl { + url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/32px-ISO_C%2B%2B_Logo.svg.png"; + hash = "sha256-+TKtwXybKw4oAHfgOsDxvL4ucItPguF76HJHdFTd3s0="; + }; + logo64 = fetchurl { + url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/64px-ISO_C%2B%2B_Logo.svg.png"; + hash = "sha256-7SjOcSaSPUHIKnjBxMdn+KSjviL69IXhX7eJsacYeGE="; + }; + }; in { - cpp11-kernel = mkDefinition "c++11"; - cpp14-kernel = mkDefinition "c++14"; - cpp17-kernel = mkDefinition "c++17"; - cpp2a-kernel = mkDefinition "c++2a"; + cpp11-kernel = mkKernelSpec "c++11"; + cpp14-kernel = mkKernelSpec "c++14"; + cpp17-kernel = mkKernelSpec "c++17"; + cpp2a-kernel = mkKernelSpec "c++2a"; + + inherit xeus-cling; } diff --git a/pkgs/applications/editors/jupyter-kernels/xeus-cling/test.ipynb b/pkgs/applications/editors/jupyter-kernels/xeus-cling/test.ipynb new file mode 100644 index 0000000000000..27e5932b8c8f9 --- /dev/null +++ b/pkgs/applications/editors/jupyter-kernels/xeus-cling/test.ipynb @@ -0,0 +1,24 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "574ed398-7bfe-4a34-a7dd-9fa85535aed2", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "std::cout << \"Hello world.\";" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++ 17", + "language": "cpp", + "name": "cpp17" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index dd74e39a5bdc6..312e8ec0b0c07 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -7247,8 +7247,7 @@ with pkgs; }; inherit (callPackage ../applications/editors/jupyter-kernels/xeus-cling { }) - cpp11-kernel cpp14-kernel cpp17-kernel cpp2a-kernel; - xeus-cling = callPackage ../applications/editors/jupyter-kernels/xeus-cling/xeus-cling.nix { }; + cpp11-kernel cpp14-kernel cpp17-kernel cpp2a-kernel xeus-cling; clojure = callPackage ../development/interpreters/clojure { # set this to an LTS version of java