From 03f3e407c2ae6691615c6c12e30c20d3bfce5948 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 28 Sep 2023 01:26:59 +0100 Subject: [PATCH 1/4] flake.nix: Add flakeModule The flakeModule attribute allows the project to be integrated easily into flakes based on flake-parts, which is a minimal set of module system options that unifies many flake integrations. --- flake-module.nix | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 8 ++++++ 2 files changed, 77 insertions(+) create mode 100644 flake-module.nix diff --git a/flake-module.nix b/flake-module.nix new file mode 100644 index 00000000..1e0aa858 --- /dev/null +++ b/flake-module.nix @@ -0,0 +1,69 @@ +top @ { + config, + flake-parts-lib, + lib, + ... +}: let + inherit (lib) mkOption types; +in { + # User options defined in perSystem below. + options.jupyenv.flake = mkOption { + internal = true; + description = "The jupyenv flake"; + }; + + options.perSystem = flake-parts-lib.mkPerSystemOption ( + { + config, + pkgs, + system, + ... + }: let + jupyenv = top.config.jupyenv.flake; + cfg = config.jupyenv; + inherit (jupyenv.lib.${system}) mkJupyterlabNew; + + jupyterlab = mkJupyterlabNew ({...}: { + nixpkgs = pkgs; + imports = [cfg.kernels]; + }); + in { + options.jupyenv = { + kernels = mkOption { + type = types.deferredModule; + description = '' + A module that defines all the kernels to be installed. + + You may reference a file, or multiple files using `imports`. + ''; + }; + pkgs = mkOption { + type = types.pkgs; + description = '' + Nixpkgs instance to use. + ''; + default = pkgs; + defaultText = lib.literalMD "`pkgs` module argument"; + }; + packageName = mkOption { + description = '' + Attribute name to use for the generated `package` and `apps` definitions. + + You will be able to run `nix run .#` to start jupyterlab. + + The default value, `"default"` also allows `nix run` without argument, but may compete with other modules' packages. + ''; + default = "default"; + }; + }; + + config = { + packages.${cfg.packageName} = jupyterlab; + apps.${cfg.packageName} = { + program = "${jupyterlab}/bin/jupyter-lab"; + type = "app"; + }; + }; + } + ); +} diff --git a/flake.nix b/flake.nix index 47948a03..58642442 100644 --- a/flake.nix +++ b/flake.nix @@ -168,6 +168,14 @@ } )) // { + # https://flake.parts/options/jupyenv + flakeModule = { + _file = "${toString ./.}/flake.nix#flakeModule"; + imports = [ + ./flake-module.nix + {jupyenv.flake = self;} + ]; + }; templates.default = { path = ./template; description = "Boilerplate for your jupyenv project"; From 1c4a83bcc77c8deb035fa2a19a6b526981700d9d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 28 Sep 2023 01:29:43 +0100 Subject: [PATCH 2/4] Move template -> template/flake-utils --- flake.nix | 2 +- template/{ => flake-utils}/flake.nix | 0 template/{ => flake-utils}/kernels.nix | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename template/{ => flake-utils}/flake.nix (100%) rename template/{ => flake-utils}/kernels.nix (100%) diff --git a/flake.nix b/flake.nix index 58642442..053b400a 100644 --- a/flake.nix +++ b/flake.nix @@ -177,7 +177,7 @@ ]; }; templates.default = { - path = ./template; + path = ./template/flake-utils; description = "Boilerplate for your jupyenv project"; welcomeText = '' You have created a jupyenv template. diff --git a/template/flake.nix b/template/flake-utils/flake.nix similarity index 100% rename from template/flake.nix rename to template/flake-utils/flake.nix diff --git a/template/kernels.nix b/template/flake-utils/kernels.nix similarity index 100% rename from template/kernels.nix rename to template/flake-utils/kernels.nix From 5b26ce9c6059dfbe4d3568581bfe8b8462412c94 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 28 Sep 2023 01:39:00 +0100 Subject: [PATCH 3/4] Add templates.flake-parts --- flake.nix | 16 +++++++++++++++- template/flake-parts/flake.nix | 29 +++++++++++++++++++++++++++++ template/flake-parts/kernels.nix | 5 +++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 template/flake-parts/flake.nix create mode 100644 template/flake-parts/kernels.nix diff --git a/flake.nix b/flake.nix index 053b400a..b014743e 100644 --- a/flake.nix +++ b/flake.nix @@ -176,7 +176,8 @@ {jupyenv.flake = self;} ]; }; - templates.default = { + templates.default = self.templates.flake-utils; + templates.flake-utils = { path = ./template/flake-utils; description = "Boilerplate for your jupyenv project"; welcomeText = '' @@ -184,6 +185,19 @@ Run `nix run` to immediately try it out. + See the jupyenv documentation for more information. + + https://jupyenv.io/documentation/getting-started/ + ''; + }; + templates.flake-parts = { + path = ./template/flake-parts; + description = "Boilerplate for your jupyenv project"; + welcomeText = '' + You have created a jupyenv template. + + Run `nix run` to immediately try it out. + See the jupyenv documentation for more information. https://jupyenv.io/documentation/getting-started/ diff --git a/template/flake-parts/flake.nix b/template/flake-parts/flake.nix new file mode 100644 index 00000000..7951b14f --- /dev/null +++ b/template/flake-parts/flake.nix @@ -0,0 +1,29 @@ +{ + description = "Your jupyenv project"; + + nixConfig.extra-substituters = [ + "https://tweag-jupyter.cachix.org" + ]; + nixConfig.extra-trusted-public-keys = [ + "tweag-jupyter.cachix.org-1:UtNH4Zs6hVUFpFBTLaA4ejYavPo5EFFqgd7G7FxGW9g=" + ]; + + inputs.flake-parts.url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + inputs.jupyenv.url = "github:tweag/jupyenv"; + + outputs = inputs @ {flake-parts, ...}: + flake-parts.lib.mkFlake {inherit inputs;} { + imports = [ + inputs.jupyenv.flakeModule + ]; + systems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"]; + perSystem = { + lib, + pkgs, + ... + }: { + jupyenv.kernels = ./kernels.nix; + }; + }; +} diff --git a/template/flake-parts/kernels.nix b/template/flake-parts/kernels.nix new file mode 100644 index 00000000..0b6572f0 --- /dev/null +++ b/template/flake-parts/kernels.nix @@ -0,0 +1,5 @@ +{pkgs, ...}: { + kernel.python.minimal = { + enable = true; + }; +} From c703e7dcb898733595799ebae0e5d824c9bebfd8 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 28 Sep 2023 04:45:33 +0100 Subject: [PATCH 4/4] DRY welcomeText --- flake.nix | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/flake.nix b/flake.nix index b014743e..14fb6599 100644 --- a/flake.nix +++ b/flake.nix @@ -58,6 +58,16 @@ # flake-utils.lib.system.x86_64-darwin ]; + welcomeText = '' + You have created a jupyenv template. + + Run `nix run` to immediately try it out. + + See the jupyenv documentation for more information. + + https://jupyenv.io/documentation/getting-started/ + ''; + kernelLib = import ./lib/kernels.nix {inherit self lib;}; in (flake-utils.lib.eachSystem SYSTEMS ( @@ -180,28 +190,12 @@ templates.flake-utils = { path = ./template/flake-utils; description = "Boilerplate for your jupyenv project"; - welcomeText = '' - You have created a jupyenv template. - - Run `nix run` to immediately try it out. - - See the jupyenv documentation for more information. - - https://jupyenv.io/documentation/getting-started/ - ''; + inherit welcomeText; }; templates.flake-parts = { path = ./template/flake-parts; description = "Boilerplate for your jupyenv project"; - welcomeText = '' - You have created a jupyenv template. - - Run `nix run` to immediately try it out. - - See the jupyenv documentation for more information. - - https://jupyenv.io/documentation/getting-started/ - ''; + inherit welcomeText; }; }; }