From e3aed5b094c8fa959fce94c7ce653237800b3204 Mon Sep 17 00:00:00 2001 From: guangtao Date: Sun, 1 Oct 2023 20:15:49 -0700 Subject: [PATCH] refactor: fix nix-kernel --- modules/kernels/nix/default.nix | 105 ++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 12 deletions(-) diff --git a/modules/kernels/nix/default.nix b/modules/kernels/nix/default.nix index e6e3b348..963bb056 100644 --- a/modules/kernels/nix/default.nix +++ b/modules/kernels/nix/default.nix @@ -1,19 +1,100 @@ { - config, + self, system, + config, + lib, mkKernel, ... -} @ args: -import ./../../poetry.nix { - inherit mkKernel; - - argvKernelName = "nix-kernel"; - codemirrorMode = ""; - language = "nix"; +}: let + inherit (lib) types; - requiredRuntimePackages = [ - config.nixpkgs.nix - ]; kernelName = "nix"; + kernelOptions = { + config, + name, + ... + }: let + args = {inherit self system lib config name kernelName;}; + kernelModule = import ./../../kernel.nix args; + kernelFunc = { + self, + system, + # custom arguments + pkgs ? self.inputs.nixpkgs.legacyPackages.${system}, + name ? "nix", + displayName ? "Nix", + requiredRuntimePackages ? [], + runtimePackages ? [], + nix ? pkgs.nix, + }: let + allRuntimePackages = + requiredRuntimePackages + ++ runtimePackages + ++ [ + nix + ]; + + env = pkgs.python3.withPackages (ps: with ps; [nix-kernel]); + + wrappedEnv = + pkgs.runCommand "wrapper-${env.name}" + {nativeBuildInputs = [pkgs.makeWrapper];} + '' + mkdir -p $out/bin + for i in ${env}/bin/*; do + filename=$(basename $i) + ln -s ${env}/bin/$filename $out/bin/$filename + wrapProgram $out/bin/$filename \ + --set PATH "${pkgs.lib.makeSearchPath "bin" allRuntimePackages}"\ + --set NIX_PATH "nixpkgs=${pkgs.path}" + done + ''; + in { + inherit name displayName; + language = "nix"; + argv = [ + "${wrappedEnv}/bin/python" + "-m" + "nix-kernel" + "-f" + "{connection_file}" + ]; + codemirrorMode = "nix"; + logo64 = ./logo64.png; + }; + in { + options = + { + nix = lib.mkOption { + type = types.package; + default = config.nixpkgs.nix; + description = lib.mdDoc '' + Nix Version + ''; + }; + } + // kernelModule.options; + + config = lib.mkIf config.enable { + build = mkKernel (kernelFunc config.kernelArgs); + kernelArgs = + { + inherit (config) nix; + } + // kernelModule.kernelArgs; + }; + }; +in { + options.kernel.${kernelName} = lib.mkOption { + type = types.attrsOf (types.submodule kernelOptions); + default = {}; + example = lib.literalExpression '' + { + kernel.${kernelName}."example".enable = true; + } + ''; + description = lib.mdDoc '' + A ${kernelName} kernel for IPython. + ''; + }; } -args