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 c5d35a5b..12714a03 100644 --- a/flake.nix +++ b/flake.nix @@ -63,6 +63,16 @@ "aarch64-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 ( @@ -174,18 +184,24 @@ } )) // { - templates.default = { - path = ./template; + # https://flake.parts/options/jupyenv + flakeModule = { + _file = "${toString ./.}/flake.nix#flakeModule"; + imports = [ + ./flake-module.nix + {jupyenv.flake = self;} + ]; + }; + templates.default = self.templates.flake-utils; + 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"; + inherit welcomeText; }; }; } 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/kernels.nix b/template/flake-parts/kernels.nix similarity index 100% rename from template/kernels.nix rename to template/flake-parts/kernels.nix 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/flake-utils/kernels.nix b/template/flake-utils/kernels.nix new file mode 100644 index 00000000..0b6572f0 --- /dev/null +++ b/template/flake-utils/kernels.nix @@ -0,0 +1,5 @@ +{pkgs, ...}: { + kernel.python.minimal = { + enable = true; + }; +}