Skip to content

Commit

Permalink
Add flake module (#523)
Browse files Browse the repository at this point in the history
* 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.

* Move template -> template/flake-utils

* Add templates.flake-parts

* DRY welcomeText

---------

Co-authored-by: GuangTao Zhang <[email protected]>
  • Loading branch information
roberth and GTrunSec authored Nov 24, 2024
1 parent b9dcda0 commit cf2c081
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 11 deletions.
69 changes: 69 additions & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
@@ -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 .#<packageName>` 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";
};
};
}
);
}
38 changes: 27 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
};
};
}
29 changes: 29 additions & 0 deletions template/flake-parts/flake.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions template/flake-utils/kernels.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{pkgs, ...}: {
kernel.python.minimal = {
enable = true;
};
}

0 comments on commit cf2c081

Please sign in to comment.