From 0342a5194b62f5c5375dbae9f065cb33722dc466 Mon Sep 17 00:00:00 2001 From: Tom McLaughlin Date: Tue, 25 Jul 2023 01:22:45 -0700 Subject: jupyter-console: expose as a tool for launching kernels --- .../editors/jupyter-kernels/clojupyter/default.nix | 7 +++- .../editors/jupyter-kernels/iruby/default.nix | 47 +++++++++++++++++----- .../editors/jupyter-kernels/octave/default.nix | 7 +++- .../editors/jupyter-kernels/wolfram/default.nix | 8 +++- pkgs/applications/editors/jupyter/console.nix | 37 +++++++++++++++++ pkgs/top-level/all-packages.nix | 2 + 6 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 pkgs/applications/editors/jupyter/console.nix diff --git a/pkgs/applications/editors/jupyter-kernels/clojupyter/default.nix b/pkgs/applications/editors/jupyter-kernels/clojupyter/default.nix index 504d41768868..5acca288f963 100644 --- a/pkgs/applications/editors/jupyter-kernels/clojupyter/default.nix +++ b/pkgs/applications/editors/jupyter-kernels/clojupyter/default.nix @@ -8,8 +8,11 @@ , imagemagick }: -# To test: -# $(nix-build --no-out-link -E 'with import {}; jupyter.override { definitions = { clojure = clojupyter.definition; }; }')/bin/jupyter-notebook +# Jupyter console: +# nix run --impure --expr 'with import {}; jupyter-console.withSingleKernel clojupyter.definition' + +# Jupyter notebook: +# nix run --impure --expr 'with import {}; jupyter.override { definitions.clojure = clojupyter.definition; }' let cljdeps = import ./deps.nix { inherit pkgs; }; diff --git a/pkgs/applications/editors/jupyter-kernels/iruby/default.nix b/pkgs/applications/editors/jupyter-kernels/iruby/default.nix index 260a614fe5c0..f456d469ca36 100644 --- a/pkgs/applications/editors/jupyter-kernels/iruby/default.nix +++ b/pkgs/applications/editors/jupyter-kernels/iruby/default.nix @@ -2,16 +2,41 @@ , bundlerApp }: -bundlerApp { - pname = "iruby"; - gemdir = ./.; - exes = [ "iruby" ]; +# Jupyter console: +# nix run --impure --expr 'with import {}; jupyter-console.withSingleKernel iruby.definition' - meta = with lib; { - description = "Ruby kernel for Jupyter"; - homepage = "https://github.com/SciRuby/iruby"; - license = licenses.mit; - maintainers = [ maintainers.costrouc ]; - platforms = platforms.unix; +# Jupyter notebook: +# nix run --impure --expr 'with import {}; jupyter.override { definitions.iruby = iruby.definition; }' + +let + self = bundlerApp { + pname = "iruby"; + gemdir = ./.; + exes = [ "iruby" ]; + + passthru = { + definition = { + displayName = "IRuby"; + argv = [ + "${self}/bin/iruby" + "kernel" + "{connection_file}" + ]; + language = "ruby"; + logo32 = null; + logo64 = null; + }; + }; + + meta = { + description = "Ruby kernel for Jupyter"; + homepage = "https://github.com/SciRuby/iruby"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ costrouc thomasjm ]; + platforms = lib.platforms.unix; + }; }; -} + +in + +self diff --git a/pkgs/applications/editors/jupyter-kernels/octave/default.nix b/pkgs/applications/editors/jupyter-kernels/octave/default.nix index a9770a5c1bef..88bc1b2803bb 100644 --- a/pkgs/applications/editors/jupyter-kernels/octave/default.nix +++ b/pkgs/applications/editors/jupyter-kernels/octave/default.nix @@ -7,8 +7,11 @@ , python3 }: -# To test: -# $(nix-build -E 'with import {}; jupyter.override { definitions = { octave = octave-kernel.definition; }; }')/bin/jupyter-notebook +# Jupyter console: +# nix run --impure --expr 'with import {}; jupyter-console.withSingleKernel octave-kernel.definition' + +# Jupyter notebook: +# nix run --impure --expr 'with import {}; jupyter.override { definitions.octave = octave-kernel.definition; }' let kernel = callPackage ./kernel.nix { diff --git a/pkgs/applications/editors/jupyter-kernels/wolfram/default.nix b/pkgs/applications/editors/jupyter-kernels/wolfram/default.nix index 2d00d6e1fc2e..efa78f74bc96 100644 --- a/pkgs/applications/editors/jupyter-kernels/wolfram/default.nix +++ b/pkgs/applications/editors/jupyter-kernels/wolfram/default.nix @@ -2,8 +2,12 @@ , wolfram-engine }: -# To test: -# $(nix-build -E 'with import ./. {}; jupyter.override { definitions = { wolfram = wolfram-for-jupyter-kernel.definition; }; }')/bin/jupyter-notebook +# Jupyter console: +# nix run --impure --expr 'with import {}; jupyter-console.withSingleKernel wolfram-for-jupyter-kernel.definition' + +# Jupyter notebook: +# nix run --impure --expr 'with import {}; jupyter.override { definitions.wolfram = wolfram-for-jupyter-kernel.definition; }' + let kernel = callPackage ./kernel.nix {}; in { definition = { diff --git a/pkgs/applications/editors/jupyter/console.nix b/pkgs/applications/editors/jupyter/console.nix new file mode 100644 index 000000000000..06bc82a1780b --- /dev/null +++ b/pkgs/applications/editors/jupyter/console.nix @@ -0,0 +1,37 @@ +{ python3 +, jupyter-kernel +, lib +}: + +let + mkConsole = { + definitions ? jupyter-kernel.default + , kernel ? null + }: + (python3.buildEnv.override { + extraLibs = [ python3.pkgs.jupyter-console ]; + makeWrapperArgs = [ + "--set JUPYTER_PATH ${jupyter-kernel.create { inherit definitions; }}" + ] ++ lib.optionals (kernel != null) [ + "--add-flags --kernel" + "--add-flags ${kernel}" + ]; + }).overrideAttrs (oldAttrs: { + # To facilitate running nix run .#jupyter-console + meta = oldAttrs.meta // { mainProgram = "jupyter-console"; }; + }); + +in + +{ + # Build a console derivation with an arbitrary set of definitions, and an optional kernel to use. + # If the kernel argument is not supplied, Jupyter console will pick a kernel to run from the ones + # available on the system. + inherit mkConsole; + + # An ergonomic way to start a console with a single kernel. + withSingleKernel = definition: mkConsole { + definitions = lib.listToAttrs [(lib.nameValuePair definition.language definition)]; + kernel = definition.language; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 413c1738a557..79f9fd3b51c1 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -9663,6 +9663,8 @@ with pkgs; }; }; + jupyter-console = callPackage ../applications/editors/jupyter/console.nix { }; + jupyter-kernel = callPackage ../applications/editors/jupyter/kernel.nix { }; justify = callPackage ../tools/text/justify { }; -- cgit 1.4.1