···
+
{ config, lib, pkgs, ... }:
+
cfg = config.services.jupyter;
+
# NOTE: We don't use top-level jupyter because we don't
+
# want to pass in JUPYTER_PATH but use .environment instead,
+
package = pkgs.python3.pkgs.notebook;
+
kernels = (pkgs.jupyter-kernel.create {
+
definitions = if cfg.kernels != null
+
else pkgs.jupyter-kernel.default;
+
notebookConfig = pkgs.writeText "jupyter_config.py" ''
+
c.NotebookApp.password = ${cfg.password}
+
meta.maintainers = with maintainers; [ aborsu ];
+
options.services.jupyter = {
+
enable = mkEnableOption "Jupyter development server";
+
IP address Jupyter will be listening on.
+
Port number Jupyter will be listening on.
+
notebookDir = mkOption {
+
Root directory for notebooks.
+
Name of the user used to run the jupyter service.
+
For security reason, jupyter should really not be run as root.
+
If not set (jupyter), the service will create a jupyter user with appropriate settings.
+
Name of the group used to run the jupyter service.
+
Use this if you want to create a group of users that are able to view the notebook directory's content.
+
Password to use with notebook.
+
Can be generated using:
+
In [1]: from notebook.auth import passwd
+
Out[2]: 'sha1:1b961dc713fb:88483270a63e57d18d43cf337e629539de1436ba'
+
NOTE: you need to keep the single quote inside the nix string.
+
Or you can use a python oneliner:
+
"open('/path/secret_file', 'r', encoding='utf8').read().strip()"
+
It will be interpreted at the end of the notebookConfig.
+
"'sha1:1b961dc713fb:88483270a63e57d18d43cf337e629539de1436ba'"
+
"open('/path/secret_file', 'r', encoding='utf8').read().strip()"
+
notebookConfig = mkOption {
+
type = types.nullOr (types.attrsOf(types.submodule (import ./kernel-options.nix {
+
example = literalExample ''
+
env = (pkgs.python3.withPackages (pythonPackages: with pythonPackages; [
+
displayName = "Python 3 for machine learning";
+
logo32 = "$ {env.sitePackages}/ipykernel/resources/logo-32x32.png";
+
logo64 = "$ {env.sitePackages}/ipykernel/resources/logo-64x64.png";
+
description = "Declarative kernel config
+
Kernels can be declared in any language that supports and has the required
+
dependencies to communicate with a jupyter server.
+
In python's case, it means that ipykernel package must always be included in
+
the list of packages of the targeted environment.
+
systemd.services.jupyter = {
+
description = "Jupyter development server";
+
wantedBy = [ "multi-user.target" ];
+
# TODO: Patch notebook so we can explicitly pass in a shell
+
path = [ pkgs.bash ]; # needed for sh in cell magic to work
+
JUPYTER_PATH = toString kernels;
+
ExecStart = ''${package}/bin/jupyter-notebook \
+
--port=${toString cfg.port} --port-retries 0 \
+
--notebook-dir=${cfg.notebookDir} \
+
--NotebookApp.config_file=${notebookConfig}
+
WorkingDirectory = "~";
+
(mkIf (cfg.enable && (cfg.group == "jupyter")) {
+
users.groups.jupyter = {};
+
(mkIf (cfg.enable && (cfg.user == "jupyter")) {
+
users.extraUsers.jupyter = {
+
extraGroups = [ cfg.group ];
+
home = "/var/lib/jupyter";
+
useDefaultShell = true; # needed so that the user can start a terminal.