From 9fc7d312a9a43855bbabf242499536f75ad419e3 Mon Sep 17 00:00:00 2001 From: GuangTao Zhang Date: Thu, 9 Feb 2023 00:06:42 -0800 Subject: [PATCH] Refactor: import nixpkgs via module's config.nixpkgs (#425) * imp: add passthru kernels it allows us to link the kernelPaths, for solving some tools getting the kernelInfo only from ~/.local/jupyter/kernles or another place * refactor: import nixpkgs via module's config.nixpkgs * chore: move options.poetry to types dir --------- Co-authored-by: Dan Baker --- flake.nix | 2 +- kernels/available/bash/module.nix | 4 +- kernels/available/c/module.nix | 2 +- kernels/available/elm/module.nix | 2 +- kernels/available/go/module.nix | 2 +- kernels/available/haskell/module.nix | 2 +- kernels/available/javascript/module.nix | 2 +- kernels/available/julia/module.nix | 2 +- kernels/available/nix/module.nix | 2 +- kernels/available/r/module.nix | 4 +- kernels/available/rust/module.nix | 10 +- kernels/available/scala/module.nix | 8 +- kernels/available/zsh/module.nix | 4 +- modules/default.nix | 8 +- modules/kernel.nix | 12 +-- modules/poetry.nix | 131 +---------------------- modules/types/nixpkgs.nix | 24 +++++ modules/types/poetry.nix | 135 ++++++++++++++++++++++++ template/flake.nix | 8 +- 19 files changed, 195 insertions(+), 169 deletions(-) create mode 100644 modules/types/nixpkgs.nix create mode 100644 modules/types/poetry.nix diff --git a/flake.nix b/flake.nix index 0199b8cb..bb53bf5a 100644 --- a/flake.nix +++ b/flake.nix @@ -537,7 +537,7 @@ */ mkJupyterlabEval = customModule: pkgs.lib.evalModules { - specialArgs = {inherit self system pkgs mkJupyterlab;}; + specialArgs = {inherit self system mkJupyterlab;}; modules = lib.flatten ( [./modules] ++ lib.optional (customModule != null) customModule diff --git a/kernels/available/bash/module.nix b/kernels/available/bash/module.nix index 43abbe6d..8115bd5f 100644 --- a/kernels/available/bash/module.nix +++ b/kernels/available/bash/module.nix @@ -6,8 +6,8 @@ import ./../../../modules/poetry.nix { kernelName = "bash"; requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.bashInteractive - config.nixpkgs.legacyPackages.${system}.coreutils + config.nixpkgs.bashInteractive + config.nixpkgs.coreutils ]; } args diff --git a/kernels/available/c/module.nix b/kernels/available/c/module.nix index 42811ff5..6847e335 100644 --- a/kernels/available/c/module.nix +++ b/kernels/available/c/module.nix @@ -5,7 +5,7 @@ } @ args: import ./../../../modules/poetry.nix { requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.stdenv.cc + config.nixpkgs.stdenv.cc ]; kernelName = "c"; } diff --git a/kernels/available/elm/module.nix b/kernels/available/elm/module.nix index 0fb70db6..25a2b817 100644 --- a/kernels/available/elm/module.nix +++ b/kernels/available/elm/module.nix @@ -5,7 +5,7 @@ } @ args: import ./../../../modules/poetry.nix { requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.elmPackages.elm + config.nixpkgs.elmPackages.elm ]; kernelName = "elm"; } diff --git a/kernels/available/go/module.nix b/kernels/available/go/module.nix index 61351924..9c926f6a 100644 --- a/kernels/available/go/module.nix +++ b/kernels/available/go/module.nix @@ -13,7 +13,7 @@ name, ... }: let - requiredRuntimePackages = [config.nixpkgs.legacyPackages.${system}.go]; + requiredRuntimePackages = [config.nixpkgs.go]; args = {inherit self system lib config name kernelName requiredRuntimePackages;}; kernelModule = import ./../../../modules/kernel.nix args; in { diff --git a/kernels/available/haskell/module.nix b/kernels/available/haskell/module.nix index f989628d..afd70940 100644 --- a/kernels/available/haskell/module.nix +++ b/kernels/available/haskell/module.nix @@ -14,7 +14,7 @@ ... }: let requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.haskell.compiler.${config.haskellCompiler} + config.nixpkgs.haskell.compiler.${config.haskellCompiler} ]; args = {inherit self system lib config name kernelName requiredRuntimePackages;}; kernelModule = import ./../../../modules/kernel.nix args; diff --git a/kernels/available/javascript/module.nix b/kernels/available/javascript/module.nix index 6f3ef0c8..ccac4f07 100644 --- a/kernels/available/javascript/module.nix +++ b/kernels/available/javascript/module.nix @@ -23,7 +23,7 @@ config = lib.mkIf config.enable { kernelArgs = { - ijavascript = config.nixpkgs.legacyPackages.${system}.nodePackages.ijavascript; + ijavascript = config.nixpkgs.nodePackages.ijavascript; } // kernelModule.kernelArgs; }; diff --git a/kernels/available/julia/module.nix b/kernels/available/julia/module.nix index da8b966a..fe51e7c8 100644 --- a/kernels/available/julia/module.nix +++ b/kernels/available/julia/module.nix @@ -46,7 +46,7 @@ }; julia = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.julia; + default = config.nixpkgs.julia; description = lib.mdDoc '' Julia Version ''; diff --git a/kernels/available/nix/module.nix b/kernels/available/nix/module.nix index d7e1d897..6f1b5434 100644 --- a/kernels/available/nix/module.nix +++ b/kernels/available/nix/module.nix @@ -5,7 +5,7 @@ } @ args: import ./../../../modules/poetry.nix { requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.nix + config.nixpkgs.nix ]; kernelName = "nix"; } diff --git a/kernels/available/r/module.nix b/kernels/available/r/module.nix index edcd1881..0d15267a 100644 --- a/kernels/available/r/module.nix +++ b/kernels/available/r/module.nix @@ -20,7 +20,7 @@ { rWrapper = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.rWrapper; + default = config.nixpkgs.rWrapper; defaultText = lib.literalExpression "pkgs.rWrapper"; description = lib.mdDoc '' R version from nixpkgs. @@ -29,7 +29,7 @@ rPackages = lib.mkOption { type = types.attrs; - default = config.nixpkgs.legacyPackages.${system}.rPackages; + default = config.nixpkgs.rPackages; defaultText = lib.literalExpression "pkgs.rPackages"; description = lib.mdDoc '' A set of R packages. diff --git a/kernels/available/rust/module.nix b/kernels/available/rust/module.nix index 1cc20bce..084ae330 100644 --- a/kernels/available/rust/module.nix +++ b/kernels/available/rust/module.nix @@ -14,9 +14,9 @@ ... }: let requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.cargo - config.nixpkgs.legacyPackages.${system}.gcc - config.nixpkgs.legacyPackages.${system}.binutils-unwrapped + config.nixpkgs.cargo + config.nixpkgs.gcc + config.nixpkgs.binutils-unwrapped ]; args = {inherit self system lib config name kernelName requiredRuntimePackages;}; kernelModule = import ./../../../modules/kernel.nix args; @@ -25,7 +25,7 @@ { evcxr = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.evcxr; + default = config.nixpkgs.evcxr; example = lib.literalExpression "pkgs.evcxr"; description = lib.mdDoc '' An evaluation context for Rust. @@ -48,7 +48,7 @@ kernelModule.kernelArgs // { inherit (config) evcxr rust-overlay; - pkgs = import config.nixpkgs { + pkgs = import config.nixpkgs.path { inherit system; overlays = [config.rust-overlay.overlays.default]; }; diff --git a/kernels/available/scala/module.nix b/kernels/available/scala/module.nix index 7cc4dd18..c3a5085e 100644 --- a/kernels/available/scala/module.nix +++ b/kernels/available/scala/module.nix @@ -20,7 +20,7 @@ { scala = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.scala; + default = config.nixpkgs.scala; example = lib.literalExpression "pkgs.scala"; description = lib.mdDoc '' Scala package to use with almond. @@ -29,7 +29,7 @@ coursier = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.coursier; + default = config.nixpkgs.coursier; example = lib.literalExpression "pkgs.coursier"; description = lib.mdDoc '' Coursier package to use with almond. @@ -38,7 +38,7 @@ jdk = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.jdk; + default = config.nixpkgs.jdk; example = lib.literalExpression "pkgs.jdk"; description = lib.mdDoc '' JDK package to use with almond. @@ -47,7 +47,7 @@ jre = lib.mkOption { type = types.package; - default = config.nixpkgs.legacyPackages.${system}.jre; + default = config.nixpkgs.jre; example = lib.literalExpression "pkgs.jre"; description = lib.mdDoc '' JRE package to use with almond. diff --git a/kernels/available/zsh/module.nix b/kernels/available/zsh/module.nix index db3425f4..cd50959a 100644 --- a/kernels/available/zsh/module.nix +++ b/kernels/available/zsh/module.nix @@ -5,8 +5,8 @@ } @ args: import ./../../../modules/poetry.nix { requiredRuntimePackages = [ - config.nixpkgs.legacyPackages.${system}.zsh - config.nixpkgs.legacyPackages.${system}.coreutils + config.nixpkgs.zsh + config.nixpkgs.coreutils ]; kernelName = "zsh"; } diff --git a/modules/default.nix b/modules/default.nix index 9741fdec..a6acc528 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -3,6 +3,7 @@ config, lib, mkJupyterlab, + system, ... }: let types = lib.types; @@ -27,11 +28,7 @@ in { internal = true; }; - nixpkgs = lib.mkOption { - type = types.path; - default = self.inputs.nixpkgs; - internal = true; - }; + nixpkgs = import ./types/nixpkgs.nix {inherit lib self system;}; }; imports = [ @@ -77,5 +74,6 @@ in { runtimePackages = config.jupyterlab.runtimePackages; #flakes = config.flakes; }; + _module.args.pkgs = config.nixpkgs; }; } diff --git a/modules/kernel.nix b/modules/kernel.nix index 00c60d0d..727c447b 100644 --- a/modules/kernel.nix +++ b/modules/kernel.nix @@ -54,15 +54,7 @@ in { ''; }; - nixpkgs = lib.mkOption { - type = types.path; - default = self.inputs.nixpkgs; - defaultText = lib.literalExpression "self.inputs.nixpkgs"; - example = lib.literalExpression "self.inputs.nixpkgs"; - description = lib.mdDoc '' - nixpkgs flake input to be used for this ${kernelName} kernel. - ''; - }; + nixpkgs = import ./types/nixpkgs.nix {inherit lib self system;}; kernelArgs = lib.mkOption { type = types.lazyAttrsOf types.raw; @@ -80,6 +72,6 @@ in { requiredRuntimePackages runtimePackages ; - pkgs = config.nixpkgs.legacyPackages.${system}; + pkgs = config.nixpkgs; }; } diff --git a/modules/poetry.nix b/modules/poetry.nix index 627f7c11..24678474 100644 --- a/modules/poetry.nix +++ b/modules/poetry.nix @@ -19,134 +19,7 @@ kernelModule = import ./kernel.nix args; in { options = - { - projectDir = lib.mkOption { - type = types.path; - default = self + "/kernels/available/${kernelName}"; - defaultText = lib.literalExpression "self + \"/kernels/available/${kernelName}\""; - example = lib.literalExpression "self + \"/kernels/${kernelName}\""; - description = lib.mdDoc '' - Path to the root of the poetry project that provides this ${kernelName} - kernel. - ''; - }; - - pyproject = lib.mkOption { - type = types.path; - default = config.projectDir + "/pyproject.toml"; - defaultText = lib.literalExpression "kernel.${kernelName}..projectDir + \"/pyproject.toml\""; - example = lib.literalExpression "self + \"/kernels/${kernelName}/pyproject.toml\""; - description = '' - Path to `pyproject.toml` of the poetry project that provides this - ${kernelName} kernel. - ''; - }; - - poetrylock = lib.mkOption { - type = types.path; - default = config.projectDir + "/poetry.lock"; - defaultText = lib.literalExpression "kernel.${kernelName}..projectDir + \"/poetry.lock\""; - example = lib.literalExpression "self + \"/kernels/${kernelName}/poetry.lock\""; - description = '' - Path to `poetry.lock` of the poetry project that provides this - ${kernelName} kernel. - ''; - }; - - overrides = lib.mkOption { - type = types.path; - default = self + "/kernels/available/${kernelName}/overrides.nix"; - defaultText = lib.literalExpression "self + \"/kernels/available/${kernelName}/overrides.nix\""; - example = lib.literalExpression "self + \"/kernels/${kernelName}/overrides.nix\""; - description = '' - Path to `overrides.nix` file which provides python package overrides - for this ${kernelName} kernel. - ''; - }; - - withDefaultOverrides = lib.mkOption { - type = types.bool; - default = true; - example = lib.literalExpression "false"; - description = '' - Should we use default overrides provided by `poetry2nix`. - ''; - }; - - python = lib.mkOption { - type = types.str; - default = "python3"; - example = "python310"; - description = lib.mdDoc '' - Name of the python interpreter (from nixpkgs) to be used for this - ${kernelName} kernel. - ''; - }; - - editablePackageSources = lib.mkOption { - type = types.attrsOf (types.nullOr types.path); - default = {}; - example = lib.literalExpression "{}"; - description = '' - A mapping from package name to source directory, these will be - installed in editable mode. Note that path dependencies with `develop - = true` will be installed in editable mode unless explicitly passed - to `editablePackageSources` as `null`. - ''; - }; - - extraPackages = lib.mkOption { - type = types.functionTo (types.listOf types.package); - default = ps: []; - defaultText = lib.literalExpression "ps: []"; - example = lib.literalExpression "ps: [ps.numpy]"; - description = '' - A function taking a Python package set and returning a list of extra - packages to include in the environment. This is intended for - packages deliberately not added to `pyproject.toml` that you still - want to include. An example of such a package may be `pip`. - ''; - }; - - preferWheels = lib.mkOption { - type = types.bool; - default = false; - example = lib.literalExpression "true"; - description = lib.mdDoc '' - Use wheels rather than sdist as much as possible. - ''; - }; - - groups = lib.mkOption { - type = types.listOf types.str; - default = ["dev"]; - defaultText = lib.literalExpression "[\"dev\"]"; - example = lib.literalExpression ''["dev" "doc"]''; - description = lib.mdDoc '' - Which Poetry 1.2.0+ dependency groups to install for this ${kernelName} - kernel. - ''; - }; - - poetry2nix = lib.mkOption { - type = types.path; - default = self.inputs.poetry2nix; - defaultText = lib.literalExpression "self.inputs.poetry2nix"; - example = lib.literalExpression "self.inputs.poetry2nix"; - description = lib.mdDoc '' - poetry2nix flake input to be used for this ${kernelName} kernel. - ''; - }; - - ignoreCollisions = lib.mkOption { - type = types.bool; - default = false; - example = lib.literalExpression "true"; - description = lib.mdDoc '' - Ignore file collisions inside the environment. - ''; - }; - } + import ./types/poetry.nix {inherit lib self config kernelName;} // kernelModule.options; config = lib.mkIf config.enable { @@ -163,7 +36,7 @@ groups ignoreCollisions ; - pkgs = config.nixpkgs.legacyPackages.${system}; + pkgs = config.nixpkgs; python = pkgs.${config.python}; poetry = pkgs.callPackage "${config.poetry2nix}/pkgs/poetry" {inherit python;}; poetry2nix = import "${config.poetry2nix}/default.nix" {inherit pkgs poetry;}; diff --git a/modules/types/nixpkgs.nix b/modules/types/nixpkgs.nix new file mode 100644 index 00000000..26370ad0 --- /dev/null +++ b/modules/types/nixpkgs.nix @@ -0,0 +1,24 @@ +{ + lib, + self, + system, +}: let + nixpkgsArg = x: + if (lib.hasAttr "legacyPackages" x) + then x.legacyPackages.${system} + else x; +in + lib.mkOption { + type = lib.mkOptionType { + name = "packages"; + description = "instance of nixpkgs"; + check = x: (lib.isAttrs (nixpkgsArg x)) && (lib.hasAttr "path" (nixpkgsArg x)); + }; + default = self.inputs.nixpkgs; + defaultText = lib.literalExpression "self.inputs.nixpkgs"; + example = lib.literalExpression "self.inputs.nixpkgs"; + description = lib.mdDoc '' + nixpkgs flake input to be used for jupyenv + ''; + apply = x: nixpkgsArg x; + } diff --git a/modules/types/poetry.nix b/modules/types/poetry.nix new file mode 100644 index 00000000..e953aaca --- /dev/null +++ b/modules/types/poetry.nix @@ -0,0 +1,135 @@ +{ + lib, + self, + kernelName ? "", + config, +}: let + inherit (lib) types; +in { + projectDir = lib.mkOption { + type = types.path; + default = self + "/kernels/available/${kernelName}"; + defaultText = lib.literalExpression "self + \"/kernels/available/${kernelName}\""; + example = lib.literalExpression "self + \"/kernels/${kernelName}\""; + description = lib.mdDoc '' + Path to the root of the poetry project that provides this ${kernelName} + kernel. + ''; + }; + + pyproject = lib.mkOption { + type = types.path; + default = config.projectDir + "/pyproject.toml"; + defaultText = lib.literalExpression "kernel.${kernelName}..projectDir + \"/pyproject.toml\""; + example = lib.literalExpression "self + \"/kernels/${kernelName}/pyproject.toml\""; + description = '' + Path to `pyproject.toml` of the poetry project that provides this + ${kernelName} kernel. + ''; + }; + + poetrylock = lib.mkOption { + type = types.path; + default = config.projectDir + "/poetry.lock"; + defaultText = lib.literalExpression "kernel.${kernelName}..projectDir + \"/poetry.lock\""; + example = lib.literalExpression "self + \"/kernels/${kernelName}/poetry.lock\""; + description = '' + Path to `poetry.lock` of the poetry project that provides this + ${kernelName} kernel. + ''; + }; + + overrides = lib.mkOption { + type = types.path; + default = self + "/kernels/available/${kernelName}/overrides.nix"; + defaultText = lib.literalExpression "self + \"/kernels/available/${kernelName}/overrides.nix\""; + example = lib.literalExpression "self + \"/kernels/${kernelName}/overrides.nix\""; + description = '' + Path to `overrides.nix` file which provides python package overrides + for this ${kernelName} kernel. + ''; + }; + + withDefaultOverrides = lib.mkOption { + type = types.bool; + default = true; + example = lib.literalExpression "false"; + description = '' + Should we use default overrides provided by `poetry2nix`. + ''; + }; + + python = lib.mkOption { + type = types.str; + default = "python3"; + example = "python310"; + description = lib.mdDoc '' + Name of the python interpreter (from nixpkgs) to be used for this + ${kernelName} kernel. + ''; + }; + + editablePackageSources = lib.mkOption { + type = types.attrsOf (types.nullOr types.path); + default = {}; + example = lib.literalExpression "{}"; + description = '' + A mapping from package name to source directory, these will be + installed in editable mode. Note that path dependencies with `develop + = true` will be installed in editable mode unless explicitly passed + to `editablePackageSources` as `null`. + ''; + }; + + extraPackages = lib.mkOption { + type = types.functionTo (types.listOf types.package); + default = ps: []; + defaultText = lib.literalExpression "ps: []"; + example = lib.literalExpression "ps: [ps.numpy]"; + description = '' + A function taking a Python package set and returning a list of extra + packages to include in the environment. This is intended for + packages deliberately not added to `pyproject.toml` that you still + want to include. An example of such a package may be `pip`. + ''; + }; + + preferWheels = lib.mkOption { + type = types.bool; + default = false; + example = lib.literalExpression "true"; + description = lib.mdDoc '' + Use wheels rather than sdist as much as possible. + ''; + }; + + groups = lib.mkOption { + type = types.listOf types.str; + default = ["dev"]; + defaultText = lib.literalExpression "[\"dev\"]"; + example = lib.literalExpression ''["dev" "doc"]''; + description = lib.mdDoc '' + Which Poetry 1.2.0+ dependency groups to install for this ${kernelName} + kernel. + ''; + }; + + poetry2nix = lib.mkOption { + type = types.path; + default = self.inputs.poetry2nix; + defaultText = lib.literalExpression "self.inputs.poetry2nix"; + example = lib.literalExpression "self.inputs.poetry2nix"; + description = lib.mdDoc '' + poetry2nix flake input to be used for this ${kernelName} kernel. + ''; + }; + + ignoreCollisions = lib.mkOption { + type = types.bool; + default = false; + example = lib.literalExpression "true"; + description = lib.mdDoc '' + Ignore file collisions inside the environment. + ''; + }; +} diff --git a/template/flake.nix b/template/flake.nix index f50c8706..85f825f7 100644 --- a/template/flake.nix +++ b/template/flake.nix @@ -20,7 +20,8 @@ flake-utils, nixpkgs, jupyenv, - }: + ... + } @ inputs: flake-utils.lib.eachSystem [ flake-utils.lib.system.x86_64-linux @@ -28,7 +29,10 @@ ( system: let inherit (jupyenv.lib.${system}) mkJupyterlabNew; - jupyterlab = mkJupyterlabNew (import ./kernels.nix); + jupyterlab = mkJupyterlabNew ({...}: { + nixpkgs = inputs.nixpkgs; + imports = [(import ./kernels.nix)]; + }); in rec { packages = {inherit jupyterlab;}; packages.default = jupyterlab;